android各种控件的事件监听及举例

下面是各种常用控件的事件监听的使用

①EditText(编辑框)的事件监听---OnKeyListener

②RadioGroup、RadioButton(单选按钮)的事件监听---OnCheckedChangeListener

③CheckBox(多选按钮)的事件监听---OnCheckedChangeListener

④Spinner(下拉列表)的事件监听---OnItemSelectedListener

⑤Menu(菜单)的事件处理---onMenuItemSelected

⑥Dialog(对话框)的事件监听---DialogInterface.OnClickListener()

至于Button的事件监听,可以在上一篇博客中看到。

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


[java] view plain copy print ?
  1. "FONT-SIZE: 18px">package org.hualang.eventtest2;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.KeyEvent;  
  6. import android.view.View;  
  7. import android.widget.EditText;  
  8. import android.widget.TextView;  
  9.   
  10. public class EventTest2 extends Activity {  
  11.     /** Called when the activity is first created. */  
  12.     private TextView mytext;  
  13.     private EditText edittext;  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         mytext = (TextView)findViewById(R.id.mytext);  
  19.         edittext = (EditText)findViewById(R.id.edittext);  
  20.         /** 
  21.          * 设置当EditText为空,则提示“请输入账号” 
  22.          * 在配置文件main.xml中可以用android:hint="请输入账号"来实现 
  23.          */  
  24.         edittext.setHint("请输入账号");  
  25.         //下面为EditText事件监听  
  26.         edittext.setOnKeyListener(new EditText.OnKeyListener()  
  27.         {  
  28.   
  29.             @Override  
  30.             public boolean onKey(View arg0, int arg1, KeyEvent arg2) {  
  31.                 //得到文字,显示在TextView中  
  32.                 mytext.setText("内容:"+edittext.getText().toString());  
  33.                 return false;  
  34.             }  
  35.               
  36.         });  
  37.     }  
  38. }  
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

[java] view plain copy print ?
  1. "FONT-SIZE: 18px">"1.0" encoding="utf-8"?>  
  2. "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.     android:layout_width="fill_parent"   
  8.     android:layout_height="wrap_content"   
  9.     android:id="@+id/mytext"  
  10.     />  
  11.     android:id="@+id/edittext"  
  12.     android:layout_width="fill_parent"  
  13.     android:layout_height="wrap_content"  
  14.     android:textSize="10pt"  
  15. />  
  16.   




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

[java] view plain copy print ?
  1. "COLOR: #000000">package org.hualang.eventtest;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Gravity;  
  6. import android.widget.RadioButton;  
  7. import android.widget.RadioGroup;  
  8. import android.widget.Toast;  
  9.   
  10. public class EventTest3 extends Activity {  
  11.     /** Called when the activity is first created. */  
  12.     private RadioGroup group;  
  13.     private RadioButton radio1,radio2,radio3,radio4;  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.           
  19.         group = (RadioGroup)findViewById(R.id.radiogroup1);  
  20.         radio1 = (RadioButton)findViewById(R.id.button1);  
  21.         radio2 = (RadioButton)findViewById(R.id.button2);  
  22.         radio3 = (RadioButton)findViewById(R.id.button3);  
  23.         radio4 = (RadioButton)findViewById(R.id.button4);  
  24.           
  25.         group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
  26.               
  27.             @Override  
  28.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  29.                 // TODO Auto-generated method stub  
  30.                 if (checkedId == radio2.getId())  
  31.                 {  
  32.                     showMessage("正确答案:" + radio2.getText()+",恭喜你,答对了");  
  33.                 }  
  34.                 else  
  35.                 {  
  36.                     showMessage("对不起,虽然很多,但不是公认的最多");  
  37.                 }  
  38.             }  
  39.         });  
  40.     }  
  41.     public void showMessage(String str)  
  42.     {  
  43.         Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);  
  44.         toast.setGravity(Gravity.TOP, 0220);  
  45.         toast.show();  
  46.     }  
  47. }  
  48. "FONT-SIZE: 24px; COLOR: #ff0000">main.xml  
  49. class=java name="code">"1.0" encoding="utf-8"?>  
  50. "http://schemas.android.com/apk/res/android"  
  51.     android:orientation="vertical"  
  52.     android:layout_width="fill_parent"  
  53.     android:layout_height="fill_parent"  
  54.     >  
  55.     
  56.         android:id="@+id/mytextview"  
  57.         android:layout_width="fill_parent"  
  58.         android:layout_height="wrap_content"  
  59.         android:text="哪个城市的美女最多?"  
  60.     />  
  61.     
  62.         android:id="@+id/radiogroup1"  
  63.         android:layout_width="wrap_content"  
  64.         android:layout_height="wrap_content"  
  65.         android:orientation="vertical"  
  66.     >  
  67.         
  68.             android:id="@+id/button1"  
  69.             android:layout_width="wrap_content"  
  70.             android:layout_height="wrap_content"  
  71.             android:text="杭州"  
  72.         />  
  73.         
  74.             android:id="@+id/button2"  
  75.             android:layout_width="wrap_content"  
  76.             android:layout_height="wrap_content"  
  77.             android:text="重庆"  
  78.         />  
  79.         
  80.             android:id="@+id/button3"  
  81.             android:layout_width="wrap_content"  
  82.             android:layout_height="wrap_content"  
  83.             android:text="成都"  
  84.         />  
  85.         
  86.             android:id="@+id/button4"  
  87.             android:layout_width="wrap_content"  
  88.             android:layout_height="wrap_content"  
  89.             android:text="香港"  
  90.         />  
  91.       
  92.   
  93. "COLOR: #ff0000">第三个例子:复选框的事件处理  
  94. package org.hualang.eventtest4;  
  95.   
  96. import android.app.Activity;  
  97. import android.os.Bundle;  
  98. import android.view.Gravity;  
  99. import android.view.View;  
  100. import android.widget.Button;  
  101. import android.widget.CheckBox;  
  102. import android.widget.CompoundButton;  
  103. import android.widget.CompoundButton.OnCheckedChangeListener;  
  104. import android.widget.Toast;  
  105.   
  106. public class EventTest4 extends Activity {  
  107.     /** Called when the activity is first created. */  
  108.     private CheckBox ch1,ch2,ch3,ch4,ch5;  
  109.     private Button mybutton;  
  110.     @Override  
  111.     public void onCreate(Bundle savedInstanceState) {  
  112.         super.onCreate(savedInstanceState);  
  113.         setContentView(R.layout.main);  
  114.           
  115.         mybutton = (Button)findViewById(R.id.mybutton);  
  116.         ch1 = (CheckBox)findViewById(R.id.check1);  
  117.         ch2 = (CheckBox)findViewById(R.id.check2);  
  118.         ch3 = (CheckBox)findViewById(R.id.check3);  
  119.         ch4 = (CheckBox)findViewById(R.id.check4);  
  120.         ch5 = (CheckBox)findViewById(R.id.check5);  
  121.           
  122.         ch1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()  
  123.         {  
  124.   
  125.             @Override  
  126.             public void onCheckedChanged(CompoundButton arg0, boolean arg1) {  
  127.                 // TODO Auto-generated method stub  
  128.                 if(ch1.isChecked())  
  129.                 {  
  130.                     showMessage("你选择了"+ch1.getText());  
  131.                 }  
  132.             }  
  133.               
  134.         });  
  135.         ch2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()  
  136.         {  
  137.   
  138.             @Override  
  139.             public void onCheckedChanged(CompoundButton arg0, boolean arg1) {  
  140.                 // TODO Auto-generated method stub  
  141.                 if(ch3.isChecked())  
  142.                 {  
  143.                     showMessage("你选择了"+ch2.getText());  
  144.                 }  
  145.             }  
  146.               
  147.         });  
  148.         ch3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()  
  149.         {  
  150.   
  151.             @Override  
  152.             public void onCheckedChanged(CompoundButton arg0, boolean arg1) {  
  153.                 // TODO Auto-generated method stub  
  154.                 if(ch3.isChecked())  
  155.                 {  
  156.                     showMessage("你选择了"+ch3.getText());  
  157.                 }  
  158.             }  
  159.               
  160.         });  
  161.         ch4.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()  
  162.         {  
  163.   
  164.             @Override  
  165.             public void onCheckedChanged(CompoundButton arg0, boolean arg1) {  
  166.                 // TODO Auto-generated method stub  
  167.                 if(ch4.isChecked())  
  168.                 {  
  169.                     showMessage("你选择了"+ch4.getText());  
  170.                 }  
  171.             }  
  172.               
  173.         });  
  174.         ch5.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()  
  175.         {  
  176.   
  177.             @Override  
  178.             public void onCheckedChanged(CompoundButton arg0, boolean arg1) {  
  179.                 // TODO Auto-generated method stub  
  180.                 if(ch5.isChecked())  
  181.                 {  
  182.                     showMessage("你选择了"+ch5.getText());  
  183.                 }  
  184.             }  
  185.               
  186.         });  
  187.           
  188.         mybutton.setOnClickListener(new Button.OnClickListener()  
  189.         {  
  190.   
  191.             @Override  
  192.             public void onClick(View arg0) {  
  193.                 // TODO Auto-generated method stub  
  194.                 int num = 0;  
  195.                 if(ch1.isChecked())  
  196.                 {  
  197.                     num++;  
  198.                 }  
  199.                 if(ch2.isChecked())  
  200.                 {  
  201.                     num++;  
  202.                 }  
  203.                 if(ch3.isChecked())  
  204.                 {  
  205.                     num++;  
  206.                 }  
  207.                 if(ch4.isChecked())  
  208.                 {  
  209.                     num++;  
  210.                 }  
  211.                 if(ch5.isChecked())  
  212.                 {  
  213.                     num++;  
  214.                 }  
  215.                   
  216.                 showMessage("谢谢参与,您一共选择了"+num+"项");  
  217.                   
  218.             }  
  219.               
  220.         });  
  221.     }  
  222.       
  223.   
  224.       
  225.     public void showMessage(String str)  
  226.     {  
  227.         Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);  
  228.         toast.setGravity(Gravity.TOP, 0220);  
  229.         toast.show();  
  230.     }  
  231. }  
  232. "FONT-SIZE: 24px; COLOR: #ff0000">main.xml  
  233. "1.0" encoding="utf-8"?>  
  234. "http://schemas.android.com/apk/res/android"  
  235.     android:orientation="vertical"  
  236.     android:layout_width="fill_parent"  
  237.     android:layout_height="fill_parent"  
  238.     >  
  239.     
  240.         android:layout_width="fill_parent"   
  241.         android:layout_height="wrap_content"   
  242.         android:text="你喜欢哪些智能手机系统"  
  243.         />  
  244.     
  245.         android:id="@+id/check1"  
  246.         android:layout_width="fill_parent"  
  247.         android:layout_height="wrap_content"  
  248.         android:text="苹果 ios"  
  249.     />  
  250.     
  251.         android:id="@+id/check2"  
  252.         android:layout_width="fill_parent"  
  253.         android:layout_height="wrap_content"  
  254.         android:text="谷歌 Android"  
  255.     />  
  256.     
  257.         android:id="@+id/check3"  
  258.         android:layout_width="fill_parent"  
  259.         android:layout_height="wrap_content"  
  260.         android:text="RIM BlackBerry"  
  261.     />  
  262.     
  263.         android:id="@+id/check4"  
  264.         android:layout_width="fill_parent"  
  265.         android:layout_height="wrap_content"  
  266.         android:text="微软 Windows phone 7"  
  267.     />  
  268.     
  269.         android:id="@+id/check5"  
  270.         android:layout_width="fill_parent"  
  271.         android:layout_height="wrap_content"  
  272.         android:text="诺基亚 symbian"  
  273.     />  
  274.     
  275.         android:id="@+id/mybutton"  
  276.         android:layout_width="fill_parent"  
  277.         android:layout_height="wrap_content"  
  278.         android:text="确定"  
  279.     />  
  280.   
  281.   
  282.   
  283. "COLOR: #ff0000">第四个例子:Spinner下拉菜单的事件处理  
  284. package org.hualang.eventtest5;  
  285.   
  286. import android.app.Activity;     
  287. import android.os.Bundle;     
  288. import android.view.View;     
  289. import android.widget.AdapterView;     
  290. import android.widget.ArrayAdapter;     
  291. import android.widget.Spinner;     
  292. import android.widget.TextView;     
  293.     
  294. public class EventTest5 extends Activity {     
  295.     /** Called when the activity is first created. */    
  296.     private static final String[] citys={"杭州","北京","成都","大连","深圳","南京"};     
  297.     private TextView text;     
  298.     private Spinner spinner;     
  299.     private ArrayAdapter adapter;     
  300.     @Override    
  301.     public void onCreate(Bundle savedInstanceState) {     
  302.         super.onCreate(savedInstanceState);     
  303.         setContentView(R.layout.main);     
  304.         text=(TextView)findViewById(R.id.text);     
  305.         spinner=(Spinner)findViewById(R.id.spinner);     
  306.              
  307.         //将可选内容与ArrayAdapter连接     
  308.         adapter=new ArrayAdapter(this,android.R.layout.simple_spinner_item,citys);     
  309.         //设置下拉列表风格     
  310.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);     
  311.         //将adapter添加到spinner中     
  312.         spinner.setAdapter(adapter);     
  313.         //添加Spinner事件监听     
  314.         spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()     
  315.         {     
  316.     
  317.             @Override    
  318.             public void onItemSelected(AdapterView arg0, View arg1,     
  319.                     int arg2, long arg3) {     
  320.                 // TODO Auto-generated method stub     
  321.                 text.setText("你所在的城市是:"+citys[arg2]);     
  322.                 //设置显示当前选择的项     
  323.                 arg0.setVisibility(View.VISIBLE);     
  324.             }     
  325.     
  326.             @Override    
  327.             public void onNothingSelected(AdapterView arg0) {     
  328.                 // TODO Auto-generated method stub     
  329.                      
  330.             }     
  331.                  
  332.         });     
  333.     }     
  334. }    
  335.   
  336. "FONT-SIZE: 24px; COLOR: #ff0000">main.xml  
  337. "1.0" encoding="utf-8"?>     
  338. "http://schemas.android.com/apk/res/android"    
  339.     android:orientation="vertical"    
  340.     android:layout_width="fill_parent"    
  341.     android:layout_height="fill_parent"    
  342.     >     
  343.     android:id="@+id/text"    
  344.     android:layout_width="fill_parent"      
  345.     android:layout_height="wrap_content"      
  346.     android:text="您所在的城市"    
  347.     />     
  348.     android:id="@+id/spinner"    
  349.     android:layout_width="wrap_content"    
  350.     android:layout_height="wrap_content"    
  351.     android:layout_centerHorizontal="true"    
  352. />     
  353.   
  354.   
  355. "COLOR: #ff0000">第五个例子:Menu(菜单)的事件处理   

  356.   
  357. class=java name="code">package org.hualang.eventtest6;  
  358.   
  359. import android.app.Activity;  
  360. import android.os.Bundle;  
  361. import android.view.Menu;  
  362. import android.view.MenuInflater;  
  363. import android.view.MenuItem;  
  364. import android.widget.Toast;  
  365.   
  366. public class EventTest6 extends Activity {  
  367.     /** Called when the activity is first created. */  
  368.     @Override  
  369.     public void onCreate(Bundle savedInstanceState) {  
  370.         super.onCreate(savedInstanceState);  
  371.         setContentView(R.layout.main);  
  372.     }  
  373.   
  374.     @Override  
  375.     public boolean onCreateOptionsMenu(Menu menu) {  
  376.         // TODO Auto-generated method stub  
  377.         MenuInflater inflater = getMenuInflater();  
  378.         //设置menu界面为res/menu/menu.xml  
  379.         inflater.inflate(R.menu.menu, menu);  
  380.         return true;  
  381.     }  
  382.   
  383.     @Override  
  384.     public boolean onMenuItemSelected(int featureId, MenuItem item) {  
  385.         //得到当前选中的MenuItem的ID  
  386.         int itemId = item.getItemId();  
  387.         switch(itemId)  
  388.         {  
  389.         case R.id.apple:  
  390.             Toast toast = Toast.makeText(this"这是苹果", Toast.LENGTH_SHORT);  
  391.             toast.show();  
  392.             break;  
  393.         case R.id.banana:  
  394.             Toast toast2 = Toast.makeText(this"这是香蕉", Toast.LENGTH_SHORT);  
  395.             toast2.show();  
  396.             break;  
  397.         case R.id.exit:  
  398.             EventTest6.this.finish();  
  399.             break;  
  400.         }  
  401.         return true;  
  402.     }  
  403.       
  404.       
  405. }
      
  406. "FONT-SIZE: 24px; COLOR: #ff0000">main.xml
      
  407. "1.0" encoding="utf-8"?>
      
  408. "http://schemas.android.com/apk/res/android">
      
  409.     "@+id/apple"
      
  410.         android:title="苹果"
      
  411.     />
      
  412.     "@+id/banana"
      
  413.         android:title="香蕉"
      
  414.         />
      
  415.     "@+id/exit"
      
  416.         android:title="退出"
      
  417.         />
      

  418.   
  419. "COLOR: #ff0000">第六个例子:对话框的事件处理
      
  420. package org.hualang.dialog;
      

  421.   
  422. import android.app.Activity;
      
  423. import android.app.AlertDialog;
      
  424. import android.app.Dialog;
      
  425. import android.app.ProgressDialog;
      
  426. import android.content.DialogInterface;
      
  427. import android.content.DialogInterface.OnClickListener;
      
  428. import android.os.Bundle;
      
  429. import android.view.LayoutInflater;
      
  430. import android.view.View;
      

  431.   
  432. public class MainActivity extends Activity {
      
  433.     /** Called when the activity is first created. */
      
  434.     ProgressDialog myDialog;
      
  435.     @Override
      
  436.     public void onCreate(Bundle savedInstanceState) {
      
  437.         super.onCreate(savedInstanceState);
      
  438.         setContentView(R.layout.main);
      
  439.         
      
  440.         Dialog dialog = new AlertDialog.Builder(MainActivity.this)
      
  441.         .setTitle("登录提示")
      
  442.         .setMessage("这里需要登录")
      
  443.         .setPositiveButton("确定"new DialogInterface.OnClickListener() {
      
  444.             
      
  445.             @Override
      
  446.             public void onClick(DialogInterface dialog, int which) {
      
  447.                 // TODO Auto-generated method stub
      
  448.                 LayoutInflater factory = LayoutInflater.from(MainActivity.this);
      
  449.                 final View DialogView = factory.inflate(R.layout.dialog, null);
      
  450.                 AlertDialog dlg = new AlertDialog.Builder(MainActivity.this)
      
  451.                 .setTitle("登录框")
      
  452.                 .setView(DialogView)
      
  453.                 .setPositiveButton("确定"new DialogInterface.OnClickListener() {
      
  454.                     
      
  455.                     @Override
      
  456.                     public void onClick(DialogInterface dialog, int whichButton) {
      
  457.                         // TODO Auto-generated method stub
      
  458.                         myDialog = ProgressDialog.show(MainActivity.this"请等待...""正在为你登录"true);
      
  459.                         new Thread()
      
  460.                         {
      
  461.                             public void run()
      
  462.                             {
      
  463.                                 try
      
  464.                                 {
      
  465.                                     sleep(3000);
      
  466.                                 }catch(Exception e)
      
  467.                                 {
      
  468.                                     e.printStackTrace();
      
  469.                                 }finally
      
  470.                                 {
      
  471.                                     myDialog.dismiss();
      
  472.                                 }
      
  473.                             }
      
  474.                         }.start();
      
  475.                     }
      
  476.                 }).setNegativeButton("取消",
      
  477.                         new DialogInterface.OnClickListener() {
      
  478.                             
      
  479.                             @Override
      
  480.                             public void onClick(DialogInterface dialog, int which) {
      
  481.                                 // TODO Auto-generated method stub
      
  482.                                 MainActivity.this.finish();
      
  483.                             }
      
  484.                         }).create();
      
  485.                 dlg.show();
      
  486.             }
      
  487.         }).setNeutralButton("退出"new DialogInterface.OnClickListener() {
      
  488.             
      
  489.             @Override
      
  490.             public void onClick(DialogInterface dialog, int which) {
      
  491.                 // TODO Auto-generated method stub
      
  492.                 MainActivity.this.finish();
      
  493.             }
      
  494.         }).create();
      
  495.         dialog.show();
      
  496.     
      
  497.     }
      
  498. }
      
  499. "FONT-SIZE: 24px; COLOR: #ff0000">xml文件
      
  500. "1.0" encoding="utf-8"?>
      
  501. "http://schemas.android.com/apk/res/android"
      
  502.     android:orientation="vertical"
      
  503.     android:layout_width="fill_parent"
      
  504.     android:layout_height="fill_parent"
      
  505.     >
      
  506.       
  507.         android:id="@+id/username"
      
  508.         android:layout_width="wrap_content" 
      
  509.         android:layout_height="wrap_content"
      
  510.         android:layout_marginLeft="20dip"
      
  511.         android:layout_marginRight="20dip" 
      
  512.         android:text="账号"
      
  513.         android:gravity="left"
      
  514.         android:textAppearance="?android:attr/textAppearanceMedium"
      
  515.     />
      
  516.         
  517.           android:id="@+id/myusername"
      
  518.           android:layout_height="wrap_content"
      
  519.           android:layout_width="fill_parent"
      
  520.           android:layout_marginLeft="20dip"
      
  521.           android:layout_marginRight="20dip"
      
  522.           android:scrollHorizontally="true"
      
  523.           android:autoText="false"
      
  524.           android:capitalize="none"
      
  525.           android:gravity="fill_horizontal"
      
  526.           android:textAppearance="?android:attr/textAppearanceMedium"
      
  527.       />
      
  528.         
  529.           android:id="@+id/password"
      
  530.           android:layout_width="fill_parent"
      
  531.           android:layout_height="wrap_content"
      
  532.           android:layout_marginLeft="20dip"
      
  533.           android:layout_marginRight="20dip"
      
  534.           android:text="密码"
      
  535.           android:gravity="left"
      
  536.           android:textAppearance="?android:attr/textAppearanceMedium"
      
  537.       />
      
  538.         
  539.           android:id="@+id/mypassword"
      
  540.           android:layout_width="fill_parent"
      
  541.           android:layout_height="wrap_content"
      
  542.           android:layout_marginLeft="20dip"
      
  543.           android:layout_marginRight="20dip"
      
  544.           android:scrollHorizontally="true"
      
  545.           android:autoText="false"
      
  546.           android:capitalize="none"
      
  547.           android:gravity="fill_horizontal"
      
  548.           android:password="true"
      
  549.       />
      

  550.   
  551.   
  552.   
  553. "COLOR: #ff0000">
      

  554.   
  555.   
  556.   
  557.   
  558. "FONT-SIZE: 18px">
      

  559.   
  560. "FONT-SIZE: 18px">
      

  561.   
  562. "FONT-SIZE: 18px">
      

  563.   
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

    
    
    
    
[java] view plain copy print ?
  1. "1.0" encoding="utf-8"?>  
  2. "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.     
  8.         android:id="@+id/mytextview"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="哪个城市的美女最多?"  
  12.     />  
  13.     
  14.         android:id="@+id/radiogroup1"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:orientation="vertical"  
  18.     >  
  19.         
  20.             android:id="@+id/button1"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="wrap_content"  
  23.             android:text="杭州"  
  24.         />  
  25.         
  26.             android:id="@+id/button2"  
  27.             android:layout_width="wrap_content"  
  28.             android:layout_height="wrap_content"  
  29.             android:text="重庆"  
  30.         />  
  31.         
  32.             android:id="@+id/button3"  
  33.             android:layout_width="wrap_content"  
  34.             android:layout_height="wrap_content"  
  35.             android:text="成都"  
  36.         />  
  37.         
  38.             android:id="@+id/button4"  
  39.             android:layout_width="wrap_content"  
  40.             android:layout_height="wrap_content"  
  41.             android:text="香港"  
  42.         />  
  43.       
  44.   
  45. "COLOR: #ff0000">第三个例子:复选框的事件处理  
  46. package org.hualang.eventtest4;  
  47.   
  48. import android.app.Activity;  
  49. import android.os.Bundle;  
  50. import android.view.Gravity;  
  51. import android.view.View;  
  52. import android.widget.Button;  
  53. import android.widget.CheckBox;  
  54. import android.widget.CompoundButton;  
  55. import android.widget.CompoundButton.OnCheckedChangeListener;  
  56. import android.widget.Toast;  
  57.   
  58. public class EventTest4 extends Activity {  
  59.     /** Called when the activity is first created. */  
  60.     private CheckBox ch1,ch2,ch3,ch4,ch5;  
  61.     private Button mybutton;  
  62.     @Override  
  63.     public void onCreate(Bundle savedInstanceState) {  
  64.         super.onCreate(savedInstanceState);  
  65.         setContentView(R.layout.main);  
  66.           
  67.         mybutton = (Button)findViewById(R.id.mybutton);  
  68.         ch1 = (CheckBox)findViewById(R.id.check1);  
  69.         ch2 = (CheckBox)findViewById(R.id.check2);  
  70.         ch3 = (CheckBox)findViewById(R.id.check3);  
  71.         ch4 = (CheckBox)findViewById(R.id.check4);  
  72.         ch5 = (CheckBox)findViewById(R.id.check5);  
  73.           
  74.         ch1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()  
  75.         {  
  76.   
  77.             @Override  
  78.             public void onCheckedChanged(CompoundButton arg0, boolean arg1) {  
  79.                 // TODO Auto-generated method stub   
  80.                 if(ch1.isChecked())  
  81.                 {  
  82.                     showMessage("你选择了"+ch1.getText());  
  83.                 }  
  84.             }  
  85.               
  86.         });  
  87.         ch2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()  
  88.         {  
  89.   
  90.             @Override  
  91.             public void onCheckedChanged(CompoundButton arg0, boolean arg1) {  
  92.                 // TODO Auto-generated method stub   
  93.                 if(ch3.isChecked())  
  94.                 {  
  95.                     showMessage("你选择了"+ch2.getText());  
  96.                 }  
  97.             }  
  98.               
  99.         });  
  100.         ch3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()  
  101.         {  
  102.   
  103.             @Override  
  104.             public void onCheckedChanged(CompoundButton arg0, boolean arg1) {  
  105.                 // TODO Auto-generated method stub   
  106.                 if(ch3.isChecked())  
  107.                 {  
  108.                     showMessage("你选择了"+ch3.getText());  
  109.                 }  
  110.             }  
  111.               
  112.         });  
  113.         ch4.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()  
  114.         {  
  115.   
  116.             @Override  
  117.             public void onCheckedChanged(CompoundButton arg0, boolean arg1) {  
  118.                 // TODO Auto-generated method stub   
  119.                 if(ch4.isChecked())  
  120.                 {  
  121.                     showMessage("你选择了"+ch4.getText());  
  122.                 }  
  123.             }  
  124.               
  125.         });  
  126.         ch5.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()  
  127.         {  
  128.   
  129.             @Override  
  130.             public void onCheckedChanged(CompoundButton arg0, boolean arg1) {  
  131.                 // TODO Auto-generated method stub   
  132.                 if(ch5.isChecked())  
  133.                 {  
  134.                     showMessage("你选择了"+ch5.getText());  
  135.                 }  
  136.             }  
  137.               
  138.         });  
  139.           
  140.         mybutton.setOnClickListener(new Button.OnClickListener()  
  141.         {  
  142.   
  143.             @Override  
  144.             public void onClick(View arg0) {  
  145.                 // TODO Auto-generated method stub   
  146.                 int num = 0;  
  147.                 if(ch1.isChecked())  
  148.                 {  
  149.                     num++;  
  150.                 }  
  151.                 if(ch2.isChecked())  
  152.                 {  
  153.                     num++;  
  154.                 }  
  155.                 if(ch3.isChecked())  
  156.                 {  
  157.                     num++;  
  158.                 }  
  159.                 if(ch4.isChecked())  
  160.                 {  
  161.                     num++;  
  162.                 }  
  163.                 if(ch5.isChecked())  
  164.                 {  
  165.                     num++;  
  166.                 }  
  167.                   
  168.                 showMessage("谢谢参与,您一共选择了"+num+"项");  
  169.                   
  170.             }  
  171.               
  172.         });  
  173.     }  
  174.       
  175.   
  176.       
  177.     public void showMessage(String str)  
  178.     {  
  179.         Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);  
  180.         toast.setGravity(Gravity.TOP, 0220);  
  181.         toast.show();  
  182.     }  
  183. }  
  184. "FONT-SIZE: 24px; COLOR: #ff0000">main.xml  
  185. "1.0" encoding="utf-8"?>  
  186. "http://schemas.android.com/apk/res/android"  
  187.     android:orientation="vertical"  
  188.     android:layout_width="fill_parent"  
  189.     android:layout_height="fill_parent"  
  190.     >  
  191.     
  192.         android:layout_width="fill_parent"   
  193.         android:layout_height="wrap_content"   
  194.         android:text="你喜欢哪些智能手机系统"  
  195.         />  
  196.     
  197.         android:id="@+id/check1"  
  198.         android:layout_width="fill_parent"  
  199.         android:layout_height="wrap_content"  
  200.         android:text="苹果 ios"  
  201.     />  
  202.     
  203.         android:id="@+id/check2"  
  204.         android:layout_width="fill_parent"  
  205.         android:layout_height="wrap_content"  
  206.         android:text="谷歌 Android"  
  207.     />  
  208.     
  209.         android:id="@+id/check3"  
  210.         android:layout_width="fill_parent"  
  211.         android:layout_height="wrap_content"  
  212.         android:text="RIM BlackBerry"  
  213.     />  
  214.     
  215.         android:id="@+id/check4"  
  216.         android:layout_width="fill_parent"  
  217.         android:layout_height="wrap_content"  
  218.         android:text="微软 Windows phone 7"  
  219.     />  
  220.     
  221.         android:id="@+id/check5"  
  222.         android:layout_width="fill_parent"  
  223.         android:layout_height="wrap_content"  
  224.         android:text="诺基亚 symbian"  
  225.     />  
  226.     
  227.         android:id="@+id/mybutton"  
  228.         android:layout_width="fill_parent"  
  229.         android:layout_height="wrap_content"  
  230.         android:text="确定"  
  231.     />  
  232.   
  233.   
  234.   
  235. "COLOR: #ff0000">第四个例子:Spinner下拉菜单的事件处理  
  236. package org.hualang.eventtest5;  
  237.   
  238. import android.app.Activity;     
  239. import android.os.Bundle;     
  240. import android.view.View;     
  241. import android.widget.AdapterView;     
  242. import android.widget.ArrayAdapter;     
  243. import android.widget.Spinner;     
  244. import android.widget.TextView;     
  245.     
  246. public class EventTest5 extends Activity {     
  247.     /** Called when the activity is first created. */    
  248.     private static final String[] citys={"杭州","北京","成都","大连","深圳","南京"};     
  249.     private TextView text;     
  250.     private Spinner spinner;     
  251.     private ArrayAdapter adapter;     
  252.     @Override    
  253.     public void onCreate(Bundle savedInstanceState) {     
  254.         super.onCreate(savedInstanceState);     
  255.         setContentView(R.layout.main);     
  256.         text=(TextView)findViewById(R.id.text);     
  257.         spinner=(Spinner)findViewById(R.id.spinner);     
  258.              
  259.         //将可选内容与ArrayAdapter连接      
  260.         adapter=new ArrayAdapter(this,android.R.layout.simple_spinner_item,citys);     
  261.         //设置下拉列表风格      
  262.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);     
  263.         //将adapter添加到spinner中      
  264.         spinner.setAdapter(adapter);     
  265.         //添加Spinner事件监听      
  266.         spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()     
  267.         {     
  268.     
  269.             @Override    
  270.             public void onItemSelected(AdapterView arg0, View arg1,     
  271.                     int arg2, long arg3) {     
  272.                 // TODO Auto-generated method stub      
  273.                 text.setText("你所在的城市是:"+citys[arg2]);     
  274.                 //设置显示当前选择的项      
  275.                 arg0.setVisibility(View.VISIBLE);     
  276.             }     
  277.     
  278.             @Override    
  279.             public void onNothingSelected(AdapterView arg0) {     
  280.                 // TODO Auto-generated method stub      
  281.                      
  282.             }     
  283.                  
  284.         });     
  285.     }     
  286. }    
  287.   
  288. "FONT-SIZE: 24px; COLOR: #ff0000">main.xml  
  289. "1.0" encoding="utf-8"?>     
  290. "http://schemas.android.com/apk/res/android"    
  291.     android:orientation="vertical"    
  292.     android:layout_width="fill_parent"    
  293.     android:layout_height="fill_parent"    
  294.     >     
  295.     android:id="@+id/text"    
  296.     android:layout_width="fill_parent"      
  297.     android:layout_height="wrap_content"      
  298.     android:text="您所在的城市"    
  299.     />     
  300.     android:id="@+id/spinner"    
  301.     android:layout_width="wrap_content"    
  302.     android:layout_height="wrap_content"    
  303.     android:layout_centerHorizontal="true"    
  304. />     
  305.   
  306.   
  307. "COLOR: #ff0000">第五个例子:Menu(菜单)的事件处理   

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

main.xml


             android:title="苹果"
    />
             android:title="香蕉"
        />
             android:title="退出"
        />

第六个例子:对话框的事件处理
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();
    
    }
}
xml文件

    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
             android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:text="账号"
        android:gravity="left"
        android:textAppearance="?android:attr/textAppearanceMedium"
    />
                 android:id="@+id/myusername"
          android:layout_height="wrap_content"
          android:layout_width="fill_parent"
          android:layout_marginLeft="20dip"
          android:layout_marginRight="20dip"
          android:scrollHorizontally="true"
          android:autoText="false"
          android:capitalize="none"
          android:gravity="fill_horizontal"
          android:textAppearance="?android:attr/textAppearanceMedium"
      />
                 android:id="@+id/password"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginLeft="20dip"
          android:layout_marginRight="20dip"
          android:text="密码"
          android:gravity="left"
          android:textAppearance="?android:attr/textAppearanceMedium"
      />
                 android:id="@+id/mypassword"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginLeft="20dip"
          android:layout_marginRight="20dip"
          android:scrollHorizontally="true"
          android:autoText="false"
          android:capitalize="none"
          android:gravity="fill_horizontal"
          android:password="true"
      />




                                    

你可能感兴趣的:(Android)