Android常见监听事件

来源:互联网 发布:java解释器模式 编辑:程序博客网 时间:2024/06/06 01:34

一丶文章内容大纲

①EditText(编辑框)的事件监听---OnKeyListener
②RadioGroup、RadioButton(单选按钮)的事件监听---OnCheckedChangeListener
③CheckBox(多选按钮)的事件监听---OnCheckedChangeListener
④Spinner(下拉列表)的事件监听---OnItemSelectedListener
⑤Menu(菜单)的事件处理---onMenuItemSelected
⑥Dialog(对话框)的事件监听---DialogInterface.OnClickListener()


第一个例子:EditText的事件监听


  1. public class EventTest2 extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     private TextView mytext;  
  4.     private EditText edittext;  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.         mytext = (TextView)findViewById(R.id.mytext);  
  10.         edittext = (EditText)findViewById(R.id.edittext);  
  11.         /** 
  12.          * 设置当EditText为空,则提示“请输入账号” 
  13.          * 在配置文件main.xml中可以用android:hint="请输入账号"来实现 
  14.          */  
  15.         edittext.setHint("请输入账号");  
  16.         //下面为EditText事件监听  
  17.         edittext.setOnKeyListener(new EditText.OnKeyListener()  
  18.         {  
  19.   
  20.             @Override  
  21.             public boolean onKey(View arg0, int arg1, KeyEvent arg2) {  
  22.                 //得到文字,显示在TextView中  
  23.                 mytext.setText("内容:"+edittext.getText().toString());  
  24.                 return false;  
  25.             }  
  26.               
  27.         });  
  28.     }  
  29. }  

XML

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:id="@+id/mytext"  
  11.     />  
  12. <EditText  
  13.     android:id="@+id/edittext"  
  14.     android:layout_width="fill_parent"  
  15.     android:layout_height="wrap_content"  
  16.     android:textSize="10pt"  
  17. />  
  18. </LinearLayout>  

运行效果:



第二个例子:单选按钮的事件监听处理


  1. public class EventTest3 extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     private RadioGroup group;  
  4.     private RadioButton radio1,radio2,radio3,radio4;  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.           
  10.         group = (RadioGroup)findViewById(R.id.radiogroup1);  
  11.         radio1 = (RadioButton)findViewById(R.id.button1);  
  12.         radio2 = (RadioButton)findViewById(R.id.button2);  
  13.         radio3 = (RadioButton)findViewById(R.id.button3);  
  14.         radio4 = (RadioButton)findViewById(R.id.button4);  
  15.           
  16.         group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
  17.               
  18.             @Override  
  19.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  20.                 // TODO Auto-generated method stub  
  21.                 if (checkedId == radio2.getId())  
  22.                 {  
  23.                     showMessage("正确答案:" + radio2.getText()+",恭喜你,答对了");  
  24.                 }  
  25.                 else  
  26.                 {  
  27.                     showMessage("对不起,虽然很多,但不是公认的最多");  
  28.                 }  
  29.             }  
  30.         });  
  31.     }  
  32.     public void showMessage(String str)  
  33.     {  
  34.         Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);  
  35.         toast.setGravity(Gravity.TOP, 0220);  
  36.         toast.show();  
  37.     }  
  38. }  
XML

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <TextView  
  8.         android:id="@+id/mytextview"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="哪个城市的美女最多?"  
  12.     />  
  13.     <RadioGroup  
  14.         android:id="@+id/radiogroup1"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:orientation="vertical"  
  18.     >  
  19.         <RadioButton  
  20.             android:id="@+id/button1"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="wrap_content"  
  23.             android:text="杭州"  
  24.         />  
  25.         <RadioButton  
  26.             android:id="@+id/button2"  
  27.             android:layout_width="wrap_content"  
  28.             android:layout_height="wrap_content"  
  29.             android:text="重庆"  
  30.         />  
  31.         <RadioButton  
  32.             android:id="@+id/button3"  
  33.             android:layout_width="wrap_content"  
  34.             android:layout_height="wrap_content"  
  35.             android:text="成都"  
  36.         />  
  37.         <RadioButton  
  38.             android:id="@+id/button4"  
  39.             android:layout_width="wrap_content"  
  40.             android:layout_height="wrap_content"  
  41.             android:text="香港"  
  42.         />  
  43.     </RadioGroup>  
  44. </LinearLayout> 

运行效果:



第三个例子:复选框的事件处理


  1. public class EventTest4 extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     private CheckBox ch1,ch2,ch3,ch4,ch5;  
  4.     private Button mybutton;  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.           
  10.         mybutton = (Button)findViewById(R.id.mybutton);  
  11.         ch1 = (CheckBox)findViewById(R.id.check1);  
  12.         ch2 = (CheckBox)findViewById(R.id.check2);  
  13.         ch3 = (CheckBox)findViewById(R.id.check3);  
  14.         ch4 = (CheckBox)findViewById(R.id.check4);  
  15.         ch5 = (CheckBox)findViewById(R.id.check5);  
  16.           
  17.         ch1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()  
  18.         {  
  19.   
  20.             @Override  
  21.             public void onCheckedChanged(CompoundButton arg0, boolean arg1) {  
  22.                 // TODO Auto-generated method stub  
  23.                 if(ch1.isChecked())  
  24.                 {  
  25.                     showMessage("你选择了"+ch1.getText());  
  26.                 }  
  27.             }  
  28.               
  29.         });  
  30.         ch2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()  
  31.         {  
  32.   
  33.             @Override  
  34.             public void onCheckedChanged(CompoundButton arg0, boolean arg1) {  
  35.                 // TODO Auto-generated method stub  
  36.                 if(ch3.isChecked())  
  37.                 {  
  38.                     showMessage("你选择了"+ch2.getText());  
  39.                 }  
  40.             }  
  41.               
  42.         });  
  43.         ch3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()  
  44.         {  
  45.   
  46.             @Override  
  47.             public void onCheckedChanged(CompoundButton arg0, boolean arg1) {  
  48.                 // TODO Auto-generated method stub  
  49.                 if(ch3.isChecked())  
  50.                 {  
  51.                     showMessage("你选择了"+ch3.getText());  
  52.                 }  
  53.             }  
  54.               
  55.         });  
  56.         ch4.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()  
  57.         {  
  58.   
  59.             @Override  
  60.             public void onCheckedChanged(CompoundButton arg0, boolean arg1) {  
  61.                 // TODO Auto-generated method stub  
  62.                 if(ch4.isChecked())  
  63.                 {  
  64.                     showMessage("你选择了"+ch4.getText());  
  65.                 }  
  66.             }  
  67.               
  68.         });  
  69.         ch5.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()  
  70.         {  
  71.   
  72.             @Override  
  73.             public void onCheckedChanged(CompoundButton arg0, boolean arg1) {  
  74.                 // TODO Auto-generated method stub  
  75.                 if(ch5.isChecked())  
  76.                 {  
  77.                     showMessage("你选择了"+ch5.getText());  
  78.                 }  
  79.             }  
  80.               
  81.         });  
  82.           
  83.         mybutton.setOnClickListener(new Button.OnClickListener()  
  84.         {  
  85.   
  86.             @Override  
  87.             public void onClick(View arg0) {  
  88.                 // TODO Auto-generated method stub  
  89.                 int num = 0;  
  90.                 if(ch1.isChecked())  
  91.                 {  
  92.                     num++;  
  93.                 }  
  94.                 if(ch2.isChecked())  
  95.                 {  
  96.                     num++;  
  97.                 }  
  98.                 if(ch3.isChecked())  
  99.                 {  
  100.                     num++;  
  101.                 }  
  102.                 if(ch4.isChecked())  
  103.                 {  
  104.                     num++;  
  105.                 }  
  106.                 if(ch5.isChecked())  
  107.                 {  
  108.                     num++;  
  109.                 }  
  110.                   
  111.                 showMessage("谢谢参与,您一共选择了"+num+"项");  
  112.                   
  113.             }  
  114.               
  115.         });  
  116.     }  
  117.       
  118.   
  119.       
  120.     public void showMessage(String str)  
  121.     {  
  122.         Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);  
  123.         toast.setGravity(Gravity.TOP, 0220);  
  124.         toast.show();  
  125.     }  
  126. }  
XML

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <TextView    
  8.         android:layout_width="fill_parent"   
  9.         android:layout_height="wrap_content"   
  10.         android:text="你喜欢哪些智能手机系统"  
  11.         />  
  12.     <CheckBox  
  13.         android:id="@+id/check1"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="苹果 ios"  
  17.     />  
  18.     <CheckBox  
  19.         android:id="@+id/check2"  
  20.         android:layout_width="fill_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="谷歌 Android"  
  23.     />  
  24.     <CheckBox  
  25.         android:id="@+id/check3"  
  26.         android:layout_width="fill_parent"  
  27.         android:layout_height="wrap_content"  
  28.         android:text="RIM BlackBerry"  
  29.     />  
  30.     <CheckBox  
  31.         android:id="@+id/check4"  
  32.         android:layout_width="fill_parent"  
  33.         android:layout_height="wrap_content"  
  34.         android:text="微软 Windows phone 7"  
  35.     />  
  36.     <CheckBox  
  37.         android:id="@+id/check5"  
  38.         android:layout_width="fill_parent"  
  39.         android:layout_height="wrap_content"  
  40.         android:text="诺基亚 symbian"  
  41.     />  
  42.     <Button  
  43.         android:id="@+id/mybutton"  
  44.         android:layout_width="fill_parent"  
  45.         android:layout_height="wrap_content"  
  46.         android:text="确定"  
  47.     />  
  48.   
  49. </LinearLayout>

运行效果:



第四个例子:Spinner下拉菜单的事件处理


  1. public class EventTest5 extends Activity {     
  2.     /** Called when the activity is first created. */    
  3.     private static final String[] citys={"杭州","北京","成都","大连","深圳","南京"};     
  4.     private TextView text;     
  5.     private Spinner spinner;     
  6.     private ArrayAdapter<String> adapter;     
  7.     @Override    
  8.     public void onCreate(Bundle savedInstanceState) {     
  9.         super.onCreate(savedInstanceState);     
  10.         setContentView(R.layout.main);     
  11.         text=(TextView)findViewById(R.id.text);     
  12.         spinner=(Spinner)findViewById(R.id.spinner);     
  13.              
  14.         //将可选内容与ArrayAdapter连接     
  15.         adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,citys);     
  16.         //设置下拉列表风格     
  17.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);     
  18.         //将adapter添加到spinner中     
  19.         spinner.setAdapter(adapter);     
  20.         //添加Spinner事件监听     
  21.         spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()     
  22.         {     
  23.     
  24.             @Override    
  25.             public void onItemSelected(AdapterView<?> arg0, View arg1,     
  26.                     int arg2, long arg3) {     
  27.                 // TODO Auto-generated method stub     
  28.                 text.setText("你所在的城市是:"+citys[arg2]);     
  29.                 //设置显示当前选择的项     
  30.                 arg0.setVisibility(View.VISIBLE);     
  31.             }     
  32.     
  33.             @Override    
  34.             public void onNothingSelected(AdapterView<?> arg0) {     
  35.                 // TODO Auto-generated method stub     
  36.                      
  37.             }     
  38.                  
  39.         });     
  40.     }     
  41. }    

XML

  1. <?xml version="1.0" encoding="utf-8"?>     
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:orientation="vertical"    
  4.     android:layout_width="fill_parent"    
  5.     android:layout_height="fill_parent"    
  6.     >     
  7. <TextView       
  8.     android:id="@+id/text"    
  9.     android:layout_width="fill_parent"      
  10.     android:layout_height="wrap_content"      
  11.     android:text="您所在的城市"    
  12.     />     
  13. <Spinner     
  14.     android:id="@+id/spinner"    
  15.     android:layout_width="wrap_content"    
  16.     android:layout_height="wrap_content"    
  17.     android:layout_centerHorizontal="true"    
  18. />     
  19. </LinearLayout> 

运行效果:



第五个例子:Menu(菜单)的事件处理


  1. public class EventTest6 extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.     }  
  8.   
  9.     @Override  
  10.     public boolean onCreateOptionsMenu(Menu menu) {  
  11.         // TODO Auto-generated method stub  
  12.         MenuInflater inflater = getMenuInflater();  
  13.         //设置menu界面为res/menu/menu.xml  
  14.         inflater.inflate(R.menu.menu, menu);  
  15.         return true;  
  16.     }  
  17.   
  18.     @Override  
  19.     public boolean onMenuItemSelected(int featureId, MenuItem item) {  
  20.         //得到当前选中的MenuItem的ID  
  21.         int itemId = item.getItemId();  
  22.         switch(itemId)  
  23.         {  
  24.         case R.id.apple:  
  25.             Toast toast = Toast.makeText(this"这是苹果", Toast.LENGTH_SHORT);  
  26.             toast.show();  
  27.             break;  
  28.         case R.id.banana:  
  29.             Toast toast2 = Toast.makeText(this"这是香蕉", Toast.LENGTH_SHORT);  
  30.             toast2.show();  
  31.             break;  
  32.         case R.id.exit:  
  33.             EventTest6.this.finish();  
  34.             break;  
  35.         }  
  36.         return true;  
  37.     }  
  38.       
  39.       
  40. }  

XML

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:id="@+id/apple"  
  4.         android:title="苹果"  
  5.     />  
  6.     <item android:id="@+id/banana"  
  7.         android:title="香蕉"  
  8.         />  
  9.     <item android:id="@+id/exit"  
  10.         android:title="退出"  
  11.         />  
  12. </menu>  

运行效果



第六个例子:对话框的事件处理


  1. public class MainActivity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     ProgressDialog myDialog;  
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.           
  9.         Dialog dialog = new AlertDialog.Builder(MainActivity.this)  
  10.         .setTitle("登录提示")  
  11.         .setMessage("这里需要登录")  
  12.         .setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  13.               
  14.             @Override  
  15.             public void onClick(DialogInterface dialog, int which) {  
  16.                 // TODO Auto-generated method stub  
  17.                 LayoutInflater factory = LayoutInflater.from(MainActivity.this);  
  18.                 final View DialogView = factory.inflate(R.layout.dialog, null);  
  19.                 AlertDialog dlg = new AlertDialog.Builder(MainActivity.this)  
  20.                 .setTitle("登录框")  
  21.                 .setView(DialogView)  
  22.                 .setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  23.                       
  24.                     @Override  
  25.                     public void onClick(DialogInterface dialog, int whichButton) {  
  26.                         // TODO Auto-generated method stub  
  27.                         myDialog = ProgressDialog.show(MainActivity.this"请等待...""正在为你登录"true);  
  28.                         new Thread()  
  29.                         {  
  30.                             public void run()  
  31.                             {  
  32.                                 try  
  33.                                 {  
  34.                                     sleep(3000);  
  35.                                 }catch(Exception e)  
  36.                                 {  
  37.                                     e.printStackTrace();  
  38.                                 }finally  
  39.                                 {  
  40.                                     myDialog.dismiss();  
  41.                                 }  
  42.                             }  
  43.                         }.start();  
  44.                     }  
  45.                 }).setNegativeButton("取消",  
  46.                         new DialogInterface.OnClickListener() {  
  47.                               
  48.                             @Override  
  49.                             public void onClick(DialogInterface dialog, int which) {  
  50.                                 // TODO Auto-generated method stub  
  51.                                 MainActivity.this.finish();  
  52.                             }  
  53.                         }).create();  
  54.                 dlg.show();  
  55.             }  
  56.         }).setNeutralButton("退出"new DialogInterface.OnClickListener() {  
  57.               
  58.             @Override  
  59.             public void onClick(DialogInterface dialog, int which) {  
  60.                 // TODO Auto-generated method stub  
  61.                 MainActivity.this.finish();  
  62.             }  
  63.         }).create();  
  64.         dialog.show();  
  65.       
  66.     }  
  67. }  

  XML

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <TextView    
  8.         android:id="@+id/username"  
  9.         android:layout_width="wrap_content"   
  10.         android:layout_height="wrap_content"  
  11.         android:layout_marginLeft="20dip"  
  12.         android:layout_marginRight="20dip"   
  13.         android:text="账号"  
  14.         android:gravity="left"  
  15.         android:textAppearance="?android:attr/textAppearanceMedium"  
  16.     />  
  17.     <EditText  
  18.         android:id="@+id/myusername"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_width="fill_parent"  
  21.         android:layout_marginLeft="20dip"  
  22.         android:layout_marginRight="20dip"  
  23.         android:scrollHorizontally="true"  
  24.         android:autoText="false"  
  25.         android:capitalize="none"  
  26.         android:gravity="fill_horizontal"  
  27.         android:textAppearance="?android:attr/textAppearanceMedium"  
  28.     />  
  29.     <TextView  
  30.         android:id="@+id/password"  
  31.         android:layout_width="fill_parent"  
  32.         android:layout_height="wrap_content"  
  33.         android:layout_marginLeft="20dip"  
  34.         android:layout_marginRight="20dip"  
  35.         android:text="密码"  
  36.         android:gravity="left"  
  37.         android:textAppearance="?android:attr/textAppearanceMedium"  
  38.     />  
  39.     <EditText  
  40.         android:id="@+id/mypassword"  
  41.         android:layout_width="fill_parent"  
  42.         android:layout_height="wrap_content"  
  43.         android:layout_marginLeft="20dip"  
  44.         android:layout_marginRight="20dip"  
  45.         android:scrollHorizontally="true"  
  46.         android:autoText="false"  
  47.         android:capitalize="none"  
  48.         android:gravity="fill_horizontal"  
  49.         android:password="true"  
  50.     />  
  51. </LinearLayout>  

运行效果:


3 0
原创粉丝点击