当前位置: 代码迷 >> Android >> 安卓教程十五 Button按钮的焦点变化
  详细解决方案

安卓教程十五 Button按钮的焦点变化

热度:55   发布时间:2016-05-01 10:33:16.0
安卓课程十五 Button按钮的焦点变化
监听单击事件,键盘事件,焦点变换事件和触碰事件。import android.os.Bundle;import android.app.Activity;import android.view.KeyEvent;import android.view.Menu;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnFocusChangeListener;import android.view.View.OnKeyListener;import android.view.View.OnTouchListener;import android.widget.Button;public class MainActivity extends Activity  implements OnClickListener,OnFocusChangeListener,OnKeyListener,OnTouchListener{	private Button btn1;	private Button btn2;			@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);		btn1 = (Button) findViewById(R.id.btn1);		btn2 = (Button) findViewById(R.id.btn2);				btn1.setOnClickListener(this);		btn1.setOnFocusChangeListener(this);		btn1.setOnKeyListener(this) ;				btn2.setOnClickListener(this);		btn2.setOnFocusChangeListener(this);		btn2.setOnKeyListener(this) ;			}	@Override	public boolean onCreateOptionsMenu(Menu menu) {		getMenuInflater().inflate(R.menu.main, menu);		return true;	}	@Override	public boolean onKey(View v, int keyCode, KeyEvent event) {		if(KeyEvent.ACTION_DOWN == event.getAction()){			System.out.println("键盘按下了");					}		return false;	}	@Override	public void onFocusChange(View v, boolean hasFocus) {		// TODO Auto-generated method stub		Button btn = (Button)v;		if(hasFocus){			btn.setBackgroundColor(RESULT_OK);		}else {			btn.setBackgroundColor(BIND_ABOVE_CLIENT);		}			}	@Override	public void onClick(View v) {		 Button btn = (Button)v;		 		 int  width = btn.getWidth();		// int windowWidth = getWindowManager().getDefaultDisplay().getSize(null);		 System.out.println(width);			}	@Override	public boolean onTouch(View v, MotionEvent event) {	 		if(MotionEvent.ACTION_UP == event.getAction()){					}		return false;	}}

监听单击事件,键盘事件,焦点变换事件和触碰事件。

  相关解决方案