Android 程式开发:(十一)监听UI事件 —— 11.2为view注册监听

当用户与视图views进行交互的时候,views也会触发事件。举个例子,当用户点击了一个按钮,你需要为这个事件服务,只有这样,才能去执行某些适当的行为。如果想这么做的话,你就需要为视图views注册监听。

使用上一节中的例子,在activity中有两个Button。我们可以使用一个匿名的类,为按钮设置点击事件。

package net.learn2develop.UIActivity; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class UIActivityActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //---the two buttons are wired to the same event handler--- Button btn1 = (Button)findViewById(R.id.btn1); btn1.setOnClickListener(btnListener); Button btn2 = (Button)findViewById(R.id.btn2); btn2.setOnClickListener(btnListener); } //---create an anonymous class to act as a button click listener--- private OnClickListener btnListener = new OnClickListener() { public void onClick(View v) { Toast.makeText(getBaseContext(), ((Button) v).getText() + " was clicked", Toast.LENGTH_LONG).show(); } }; @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_CENTER: Toast.makeText(getBaseContext(), "Center was clicked", Toast.LENGTH_LONG).show(); break; case KeyEvent.KEYCODE_DPAD_LEFT: Toast.makeText(getBaseContext(), "Left arrow was clicked", Toast.LENGTH_LONG).show(); break; case KeyEvent.KEYCODE_DPAD_RIGHT: Toast.makeText(getBaseContext(), "Right arrow was clicked", Toast.LENGTH_LONG).show(); break; case KeyEvent.KEYCODE_DPAD_UP: Toast.makeText(getBaseContext(), "Up arrow was clicked", Toast.LENGTH_LONG).show(); break; case KeyEvent.KEYCODE_DPAD_DOWN: Toast.makeText(getBaseContext(), "Down arrow was clicked", Toast.LENGTH_LONG).show(); break; } return false; } }如果你点击了OK按钮或Cancel按钮,对应的消息就会出现在屏幕上,这也就说明了,适当的事件被出发了。



除了定义一个匿名类,也可以定一个匿名内部类去处理事件。下面的例子展示如何处理EditText的onFocusChange()方法。

//---create an anonymous inner class to act as an onfocus listener--- EditText txt1 = (EditText)findViewById(R.id.txt1); txt1.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { Toast.makeText(getBaseContext(), ((EditText) v).getId() + " has focus - " + hasFocus, Toast.LENGTH_LONG).show(); } });当EditText获取焦点,屏幕上就会打印出一行消息。

也可以使用匿名内部类,为两个Button设置监听。

//---the two buttons are wired to the same event handler--- Button btn1 = (Button)findViewById(R.id.btn1); //btn1.setOnClickListener(btnListener); btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //---do something--- } }); Button btn2 = (Button)findViewById(R.id.btn2); //btn2.setOnClickListener(btnListener); btn2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //---do something--- } });
那么,到底该使用哪种方法去为视图设置监听呢?当你需要使用一个监听器去为多个视图服务的时候,匿名类就很有用处了。如果你只需要为单一的视图设置监听,那么就使用匿名内部类吧。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值