事件监听篇---第二篇
下面是各种常用控件的事件监听的使用
①EditText(编辑框)的事件监听---OnKeyListener
②RadioGroup、RadioButton(单选按钮)的事件监听---OnCheckedChangeListener
③CheckBox(多选按钮)的事件监听---OnCheckedChangeListener
④Spinner(下拉列表)的事件监听---OnItemSelectedListener
⑤Menu(菜单)的事件处理---onMenuItemSelected
⑥Dialog(对话框)的事件监听---DialogInterface.OnClickListener()
第一个例子:EditText的事件监听
package org.hualang.eventtest2; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class EventTest2 extends Activity { /** Called when the activity is first created. */ private TextView mytext; private EditText edittext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mytext = (TextView)findViewById(R.id.mytext); edittext = (EditText)findViewById(R.id.edittext); /** * 设置当EditText为空,则提示“请输入账号” * 在配置文件main.xml中可以用android:hint="请输入账号"来实现 */ edittext.setHint("请输入账号"); //下面为EditText事件监听 edittext.setOnKeyListener(new EditText.OnKeyListener() { @Override public boolean onKey(View arg0, int arg1, KeyEvent arg2) { //得到文字,显示在TextView中 mytext.setText("内容:"+edittext.getText().toString()); return false; } }); } }
main.xml
运行结果如下:
第二个例子:单选按钮的事件监听处理
package org.hualang.eventtest; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class EventTest3 extends Activity { /** Called when the activity is first created. */ private RadioGroup group; private RadioButton radio1,radio2,radio3,radio4; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); group = (RadioGroup)findViewById(R.id.radiogroup1); radio1 = (RadioButton)findViewById(R.id.button1); radio2 = (RadioButton)findViewById(R.id.button2); radio3 = (RadioButton)findViewById(R.id.button3); radio4 = (RadioButton)findViewById(R.id.button4); group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if (checkedId == radio2.getId()) { showMessage("正确答案:" + radio2.getText()+",恭喜你,答对了"); } else { showMessage("对不起,虽然很多,但不是公认的最多"); } } }); } public void showMessage(String str) { Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP, 0, 220); toast.show(); } }
main.xml
运行结果如下:
第三个例子:复选框的事件处理
package org.hualang.eventtest4; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.Toast; public class EventTest4 extends Activity { /** Called when the activity is first created. */ private CheckBox ch1,ch2,ch3,ch4,ch5; private Button mybutton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mybutton = (Button)findViewById(R.id.mybutton); ch1 = (CheckBox)findViewById(R.id.check1); ch2 = (CheckBox)findViewById(R.id.check2); ch3 = (CheckBox)findViewById(R.id.check3); ch4 = (CheckBox)findViewById(R.id.check4); ch5 = (CheckBox)findViewById(R.id.check5); ch1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub if(ch1.isChecked()) { showMessage("你选择了"+ch1.getText()); } } }); ch2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub if(ch3.isChecked()) { showMessage("你选择了"+ch2.getText()); } } }); ch3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub if(ch3.isChecked()) { showMessage("你选择了"+ch3.getText()); } } }); ch4.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub if(ch4.isChecked()) { showMessage("你选择了"+ch4.getText()); } } }); ch5.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub if(ch5.isChecked()) { showMessage("你选择了"+ch5.getText()); } } }); mybutton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub int num = 0; if(ch1.isChecked()) { num++; } if(ch2.isChecked()) { num++; } if(ch3.isChecked()) { num++; } if(ch4.isChecked()) { num++; } if(ch5.isChecked()) { num++; } showMessage("谢谢参与,您一共选择了"+num+"项"); } }); } public void showMessage(String str) { Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP, 0, 220); toast.show(); } }
运行结果:
第四个例子:Spinner下拉菜单的事件处理
package org.hualang.eventtest5; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; public class EventTest5 extends Activity { /** Called when the activity is first created. */ private static final String[] citys={"杭州","北京","成都","大连","深圳","南京"}; private TextView text; private Spinner spinner; private ArrayAdapteradapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text=(TextView)findViewById(R.id.text); spinner=(Spinner)findViewById(R.id.spinner); //将可选内容与ArrayAdapter连接 adapter=new ArrayAdapter (this,android.R.layout.simple_spinner_item,citys); //设置下拉列表风格 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //将adapter添加到spinner中 spinner.setAdapter(adapter); //添加Spinner事件监听 spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub text.setText("你所在的城市是:"+citys[arg2]); //设置显示当前选择的项 arg0.setVisibility(View.VISIBLE); } @Override public void onNothingSelected(AdapterView> arg0) { // TODO Auto-generated method stub } }); } }
main.xml
运行结果如下:
第五个例子:Menu(菜单)的事件处理
package org.hualang.eventtest6; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.Toast; public class EventTest6 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub MenuInflater inflater = getMenuInflater(); //设置menu界面为res/menu/menu.xml inflater.inflate(R.menu.menu, menu); return true; } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { //得到当前选中的MenuItem的ID int itemId = item.getItemId(); switch(itemId) { case R.id.apple: Toast toast = Toast.makeText(this, "这是苹果", Toast.LENGTH_SHORT); toast.show(); break; case R.id.banana: Toast toast2 = Toast.makeText(this, "这是香蕉", Toast.LENGTH_SHORT); toast2.show(); break; case R.id.exit: EventTest6.this.finish(); break; } return true; } }
res/menu/menu.xml
运行结果如下:
第六个例子:对话框的事件处理
package org.hualang.dialog; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; public class MainActivity extends Activity { /** Called when the activity is first created. */ ProgressDialog myDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Dialog dialog = new AlertDialog.Builder(MainActivity.this) .setTitle("登录提示") .setMessage("这里需要登录") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub LayoutInflater factory = LayoutInflater.from(MainActivity.this); final View DialogView = factory.inflate(R.layout.dialog, null); AlertDialog dlg = new AlertDialog.Builder(MainActivity.this) .setTitle("登录框") .setView(DialogView) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int whichButton) { // TODO Auto-generated method stub myDialog = ProgressDialog.show(MainActivity.this, "请等待...", "正在为你登录", true); new Thread() { public void run() { try { sleep(3000); }catch(Exception e) { e.printStackTrace(); }finally { myDialog.dismiss(); } } }.start(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub MainActivity.this.finish(); } }).create(); dlg.show(); } }).setNeutralButton("退出", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub MainActivity.this.finish(); } }).create(); dialog.show(); } }
res/layout/dialog.xml
运行结果: