多选按钮的实例

来源:互联网 发布:python to datetime 编辑:程序博客网 时间:2024/06/05 20:08
[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/textVeiw1"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:background="#FF0000"  
  12.         android:text="第一个TextView控件" />  
  13.   
  14.     <TextView  
  15.         android:id="@+id/textView2"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:background="#00FF00"  
  19.         android:text="第二个TextView" />  
  20.   
  21.     <TextView  
  22.         android:id="@+id/textView3"  
  23.         android:layout_width="match_parent"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_margin="40dp"  
  26.         android:background="#FF0000"  
  27.         android:text="hello world" />  
  28.   
  29.     <TextView  
  30.         android:id="@+id/textView4"  
  31.         android:layout_width="match_parent"  
  32.         android:layout_height="wrap_content"  
  33.         android:layout_marginTop="30dp"  
  34.         android:background="#00FF00"  
  35.         android:paddingTop="30dp"  
  36.         android:text="hello world" />  
  37.   
  38.     <CheckBox  
  39.         android:id="@+id/oneCheckBox"  
  40.         android:layout_width="match_parent"  
  41.         android:layout_height="wrap_content"   
  42.         android:text="one"/>  
  43.   
  44.     <CheckBox  
  45.         android:id="@+id/twoCheckBox"  
  46.         android:layout_width="match_parent"  
  47.         android:layout_height="wrap_content"  
  48.         android:text="two" />  
  49.   
  50.     <CheckBox  
  51.         android:id="@+id/ThreeCheckBox"  
  52.         android:layout_width="match_parent"  
  53.         android:layout_height="wrap_content"  
  54.         android:text="three" />  
  55.   
  56.     <CheckBox  
  57.         android:id="@+id/allCheckBox"  
  58.         android:layout_width="match_parent"  
  59.         android:layout_height="wrap_content"   
  60.         android:text="all"/>  
  61.   
  62. </LinearLayout>  


[java] view plaincopy
  1. package com.xiong.fisrt_android;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.CheckBox;  
  10. import android.widget.CompoundButton;  
  11. import android.widget.CompoundButton.OnCheckedChangeListener;  
  12. import android.widget.TextView;  
  13.   
  14. public class MainActivity extends Activity {  
  15.   
  16.     private TextView textView;  
  17.     private Button button;  
  18.     private int count = 0;  
  19.     private CheckBox oneCheckBox;  
  20.     private CheckBox twoCheckBox;  
  21.     private CheckBox threeCheckBox;  
  22.     private CheckBox allCheckBox;  
  23.   
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.fist_linearlayout);  
  28. /*      textView = (TextView) findViewById(R.id.textView); 
  29.         button = (Button) findViewById(R.id.button); 
  30.         textView.setText("hello  Android!!!"); 
  31.         textView.setBackgroundColor(Color.BLUE); 
  32.         ButtoneListener buttoneListener = new ButtoneListener();// 生成监听对象 
  33.         button.setOnClickListener(buttoneListener);// 按钮绑定一个监听器 
  34. */    
  35.         oneCheckBox=(CheckBox)findViewById(R.id.oneCheckBox);  
  36.         twoCheckBox=(CheckBox)findViewById(R.id.twoCheckBox);  
  37.         threeCheckBox=(CheckBox)findViewById(R.id.ThreeCheckBox);  
  38.         allCheckBox=(CheckBox)findViewById(R.id.allCheckBox);  
  39.         AllCheckBoxClass allCheckBoxClass=new AllCheckBoxClass();  
  40.         allCheckBox.setOnCheckedChangeListener(allCheckBoxClass);  
  41.         }  
  42.   
  43.     @Override  
  44.     public boolean onCreateOptionsMenu(Menu menu) {  
  45.         // Inflate the menu; this adds items to the action bar if it is present.  
  46.         getMenuInflater().inflate(R.menu.main, menu);  
  47.         return true;  
  48.     }  
  49.   
  50.     class ButtoneListener implements OnClickListener// 创建一个类实现监听事件的接口  
  51.     {  
  52.   
  53.         @Override  
  54.         public void onClick(View arg0) {  
  55.             // TODO Auto-generated method stub  
  56.             count++;  
  57.             textView.setText(Integer.toString(count));  
  58.   
  59.         }  
  60.   
  61.     }  
  62.     class AllCheckBoxClass implements OnCheckedChangeListener  
  63.     {  
  64.   
  65.         @Override  
  66.         public void onCheckedChanged(CompoundButton buttonView,  
  67.                 boolean isChecked) {  
  68.             // TODO Auto-generated method stub  
  69.             oneCheckBox.setChecked(isChecked);  
  70.             twoCheckBox.setChecked(isChecked);  
  71.             threeCheckBox.setChecked(isChecked);  
  72.               
  73.         }  
  74.   
  75.   
  76.           
  77.     }  
  78.   
  79.   
  80. }  
0 0