首页 电脑 电脑学堂 查看内容

Android 控件:CheckBox

2015-7-23 18:20 2107 1

摘要: 首先,在布局文件中注册CheckBox activity_main.xml view plaincopyLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layo ...
关键词: nbsp android CheckBox layout println System content listener import sleepBox





首先,在布局文件中注册CheckBox

activity_main.xml

  1. <LinearLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     tools:context=".MainActivity">  
  8.   
  9.     <TextView android:text="@string/hello_world"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content" />  
  12.   
  13.     <CheckBox  
  14.         android:id="@+id/eatID"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="吃饭"  
  18.         />  
  19.     <CheckBox  
  20.         android:id="@+id/sleepID"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="睡觉"/>  
  24.   
  25.     <CheckBox  
  26.         android:id="@+id/dotaID"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:text="dota"/>  
  30. </LinearLayout>  

MainActivity.java


CheckBox 即可以绑定 view.OnClickListener,也可以绑定 CompoundButton.OnCheckedChangeListener.

  1. package com.example.litingdong.checkboxtest;  
  2.   
  3. import android.support.v7.app.ActionBarActivity;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.view.MenuItem;  
  7. import android.view.View;  
  8. import android.widget.CheckBox;  
  9. import android.widget.CompoundButton;  
  10.   
  11.   
  12. public class MainActivity extends ActionBarActivity {  
  13.   
  14.     private CheckBox eatBox;  
  15.     private CheckBox sleepBox;  
  16.     private CheckBox dotaBox;  
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.         eatBox=(CheckBox)findViewById(R.id.eatID);  
  22.         sleepBox=(CheckBox)findViewById(R.id.sleepID);  
  23.         dotaBox=(CheckBox)findViewById(R.id.dotaID);  
  24.         //OnClickListener  
  25.         OnButtonClickListener listener=new OnButtonClickListener();  
  26.         listener = new OnButtonClickListener();  
  27.         eatBox.setOnClickListener(listener);  
  28.         sleepBox.setOnClickListener(listener);  
  29.         dotaBox.setOnClickListener(listener);  
  30.   
  31.         //OnCheckedChangedListener  
  32.         CheckBoxListener changeListener =new CheckBoxListener();  
  33.         eatBox.setOnCheckedChangeListener(changeListener);  
  34.         sleepBox.setOnCheckedChangeListener(changeListener);  
  35.         dotaBox.setOnCheckedChangeListener(changeListener);  
  36.     }  
  37.       
  38.     class OnButtonClickListener implements View.OnClickListener{  
  39.         @Override  
  40.         public void onClick(View v) {  
  41.             CheckBox box=(CheckBox)v;  
  42.   
  43.             if (box.getId()==R.id.eatID) {  
  44.                 System.out.println("eatBox");  
  45.             }else if(box.getId()==R.id.sleepID){  
  46.                 System.out.println("sleepBox");  
  47.   
  48.             }else{  
  49.                 System.out.println("dotaBox");  
  50.             }  
  51.   
  52.             if (box.isChecked()){  
  53.                 System.out.println("Checked!");  
  54.             }else{  
  55.                 System.out.println("UnChecked!");  
  56.             }  
  57.         }  
  58.     }  
  59.   
  60.     class CheckBoxListener implements CompoundButton.OnCheckedChangeListener{  
  61.   
  62.         @Override  
  63.         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  64.             if (buttonView.getId()==R.id.eatID){  
  65.                 System.out.println("eatBox");  
  66.             }else if (buttonView.getId()==R.id.sleepID){  
  67.                 System.out.println("sleepBox");  
  68.             }else{  
  69.                 System.out.println("dotaBox");  
  70.             }  
  71.   
  72.             if (isChecked){  
  73.                 System.out.println("Checked!");  
  74.             }else{  
  75.                 System.out.println("UnChecked!");  
  76.             }  
  77.         }  
  78.     }  
  79.     @Override  
  80.     public boolean onCreateOptionsMenu(Menu menu) {  
  81.         // Inflate the menu; this adds items to the action bar if it is present.  
  82.         getMenuInflater().inflate(R.menu.menu_main, menu);  
  83.         return true;  
  84.     }  
  85.   
  86.     @Override  
  87.     public boolean onOptionsItemSelected(MenuItem item) {  
  88.         // Handle action bar item clicks here. The action bar will  
  89.         // automatically handle clicks on the Home/Up button, so long  
  90.         // as you specify a parent activity in AndroidManifest.xml.  
  91.         int id = item.getItemId();  
  92.   
  93.         //noinspection SimplifiableIfStatement  
  94.         if (id == R.id.action_settings) {  
  95.             return true;  
  96.         }  
  97.   
  98.         return super.onOptionsItemSelected(item);  
  99.     }  
  100. }  

logcat:

 





声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手
1

鲜花

鸡蛋

刚表态过的朋友 (1 人)

没看懂。
2015-7-31 09:58

返回顶部