最近在写一个安卓程序,要用到在keydown事件中与layout进行交互,为了不忘记了,特把这一功能记录如下:
001 package irdc.ex05_20;
002
003 import android.app.Activity;
004 import android.os.Bundle;
005 import android.util.DisplayMetrics;
006 import android.view.KeyEvent;
007 import android.view.View;
008 import android.widget.AbsoluteLayout;
009 import android.widget.Button;
010 import android.widget.Toast;
011
012 public class EX05_20 extends Activity
013 {
014 private Button mButton01;
015 private int intWidth, intHeight, intButtonX, intButtonY;
016
017 /* 存储屏幕的分辨率 */
018 private int intScreenX, intScreenY;
019
020 /* 按钮位移的平移量 */
021 private int intShift = 2;
022
023 /** Called when the activity is first created. */
024 @Override
025 public void onCreate(Bundle savedInstanceState)
026 {
027 super.onCreate(savedInstanceState);
028 setContentView(R.layout.main);
029
030 //一般的结构描述信息显示,如它的大小,密度和字体缩放。
031 // 要访问DisplayMetrics成员,初始化一个这样的对象:
032 DisplayMetrics dm = new DisplayMetrics();
033 getWindowManager().getDefaultDisplay().getMetrics(dm);
034
035 /* 取得屏幕解析像素 */
036 intScreenX = dm.widthPixels;
037 intScreenY = dm.heightPixels;
038
039 /* 定义按钮的宽x高 */
040 intWidth = 80;
041 intHeight = 40;
042
043 mButton01 =(Button) findViewById(R.id.myButton1);
044
045 /* 初始化按钮位置居中 */
046 RestoreButton();
047
048 /* 当点击按钮,还原初始位置 */
049 mButton01.setOnClickListener(new Button.OnClickListener()
050 {
051 @Override
052 public void onClick(View v)
053 {
054 // TODO Auto-generated method stub
055 RestoreButton();
056 }
057 });
058 }
059
060 @Override
061 public boolean onKeyDown(int keyCode, KeyEvent event)
062 {
063 // TODO Auto-generated method stub
064 switch(keyCode)
065 {
066 /* 中间按键 */
067 case KeyEvent.KEYCODE_DPAD_CENTER:
068 /* keyCode=23 */
069 RestoreButton();
070 break;
071 /* 上按键 */
072 case KeyEvent.KEYCODE_DPAD_UP:
073 /* keyCode=19 */
074 MoveButtonUp();
075 break;
076 /* 下按键 */
077 case KeyEvent.KEYCODE_DPAD_DOWN:
078 /* keyCode=20 */
079 MoveButtonDown();
080 break;
081 /* 左按键 */
082 case KeyEvent.KEYCODE_DPAD_LEFT:
083 /* keyCode=21 */
084 MoveButtonLeft();
085 break;
086 /* 右按键 */
087 case KeyEvent.KEYCODE_DPAD_RIGHT:
088 /* keyCode=22 */
089 MoveButtonRight();
090 break;
091 }
092 return super.onKeyDown(keyCode, event);
093 }
094
095 /* 还原按钮位置的事件处理 */
096 public void RestoreButton()
097 {
098 intButtonX = ((intScreenX-intWidth)/2);
099 intButtonY = ((intScreenY-intHeight)/2);
100 mMakeTextToast
101 (
102 "("+
103 Integer.toString(intButtonX)+
104 ","+
105 Integer.toString(intButtonY)+")",true
106 );
107
108 /* 以setLayoutParams方法,重新安排Layout上的位置 */
109 mButton01.setLayoutParams
110 (
111 new AbsoluteLayout.LayoutParams
112 (intWidth,intHeight,intButtonX,intButtonY)
113 );
114 }
115
116 /* 点击DPAD上按键时事件处理 */
117 public void MoveButtonUp()
118 {
119 intButtonY = intButtonY-intShift;
120 /* 预防按钮到达下边界时的处理 */
121 if(intButtonY<0)
122 {
123 intButtonY = 0;
124 }
125 mButton01.setLayoutParams
126 (
127 new AbsoluteLayout.LayoutParams
128 (intWidth,intHeight,intButtonX,intButtonY)
129 );
130 }
131
132 /* 点击DPAD下按键时事件处理 */
133 public void MoveButtonDown()
134 {
135 intButtonY = intButtonY+intShift;
136 /* 预防按钮到达下边界时的处理 */
137 if(intButtonY>(intScreenY-intHeight))
138 {
139 intButtonY = intScreenX-intHeight;
140 }
141 mButton01.setLayoutParams
142 (
143 new AbsoluteLayout.LayoutParams
144 (intWidth,intHeight,intButtonX,intButtonY)
145 );
146 }
147
148 /* 点击DPAD左按键时事件处理 */
149 public void MoveButtonLeft()
150 {
151 intButtonX = intButtonX-intShift;
152 /* 预防按钮到达左边界时的处理 */
153 if(intButtonX<0)
154 {
155 intButtonX = 0;
156 }
157 mButton01.setLayoutParams
158 (
159 new AbsoluteLayout.LayoutParams
160 (intWidth,intHeight,intButtonX,intButtonY)
161 );
162 }
163
164 /* 点击DPAD右按键时事件处理 */
165 public void MoveButtonRight()
166 {
167 intButtonX = intButtonX+intShift;
168 /* 预防按钮到达右边界时的处理 */
169 if(intButtonX>(intScreenX-intWidth))
170 {
171 intButtonX = intScreenX-intWidth;
172 }
173 mButton01.setLayoutParams
174 (
175 new AbsoluteLayout.LayoutParams
176 (intWidth,intHeight,intButtonX,intButtonY)
177 );
178 }
179
180 //显示信息
181 public void mMakeTextToast(String str, boolean isLong)
182 {
183 if(isLong==true)
184 {
185 Toast.makeText(EX05_20.this, str, Toast.LENGTH_LONG).show();
186 }
187 else
188 {
189 Toast.makeText(EX05_20.this, str, Toast.LENGTH_SHORT).show();
190 }
191 }
192 }