欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Android:CheckBox控件

发布时间:2025/3/20 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Android:CheckBox控件 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1)ChexkBox继承自CompoundButton组件;

2)isChecked()--确定是否选中;setChecked(bool checked)--设置选中或取消选中;

3)监听事件:CompoundButton.OnCheckedChangeListener

使用checkbox,并实现监听测试:

1)效果:

2)源代码:

res\layout\activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><CheckBoxandroid:id="@+id/cbJava"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_marginLeft="27dp"android:text="Java Runtime 9.0" /><TextViewandroid:id="@+id/tvCheckedValue"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/cbJava"android:layout_below="@+id/cbPython"android:layout_marginLeft="14dp"android:layout_marginTop="54dp"android:text="" /><CheckBoxandroid:id="@+id/cbPython"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/cbJava"android:layout_below="@+id/cbJava"android:layout_marginTop="22dp"android:text="Python runtime2.7" /></RelativeLayout> View Code

MainActivity.java

1 package com.example.helloword; 2 3 import java.text.BreakIterator; 4 5 import android.R.bool; 6 import android.R.string; 7 import android.app.Activity; 8 import android.app.AlertDialog; 9 import android.content.DialogInterface; 10 import android.content.DialogInterface.OnClickListener; 11 import android.os.Bundle; 12 import android.renderscript.Script.KernelID; 13 import android.test.IsolatedContext; 14 import android.util.Log; 15 import android.view.KeyEvent; 16 import android.view.Menu; 17 import android.view.View; 18 import android.widget.Button; 19 import android.widget.CheckBox; 20 import android.widget.CompoundButton; 21 import android.widget.EditText; 22 import android.widget.ImageView; 23 import android.widget.RadioButton; 24 import android.widget.RadioGroup; 25 import android.widget.TextView; 26 27 public class MainActivity extends Activity { 28 private CheckBox cbJava, cbPython; 29 private TextView tvCheckedValue; 30 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.activity_main); 35 this.cbJava = (CheckBox) this.findViewById(R.id.cbJava); 36 this.cbPython = (CheckBox) this.findViewById(R.id.cbPython); 37 this.tvCheckedValue = (TextView) this.findViewById(R.id.tvCheckedValue); 38 39 CheckBoxOnCheckedChangeListener listener = new CheckBoxOnCheckedChangeListener(); 40 41 this.cbJava.setOnCheckedChangeListener(listener); 42 this.cbPython.setOnCheckedChangeListener(listener); 43 44 } 45 46 private StringBuffer stringBuffer; 47 48 private class CheckBoxOnCheckedChangeListener implements 49 CompoundButton.OnCheckedChangeListener { 50 private String checkJava = cbJava.getText().toString() + " "; 51 private String checkPython = cbPython.getText().toString() + " "; 52 private StringBuffer stringBuffer = new StringBuffer(); 53 54 @Override 55 public void onCheckedChanged(CompoundButton compoundButton, 56 boolean ischecked) { 57 switch (compoundButton.getId()) { 58 case R.id.cbJava: 59 Log.i("info", "operator " + cbJava.getText()); 60 break; 61 case R.id.cbPython: 62 Log.i("info", "operator " + cbPython.getText()); 63 break; 64 default: 65 break; 66 } 67 68 if (compoundButton == cbJava) { 69 changeValue(ischecked, checkJava); 70 } else if (compoundButton == cbPython) { 71 changeValue(ischecked, checkPython); 72 } 73 74 tvCheckedValue.setText(stringBuffer.toString()); 75 } 76 77 private void changeValue(boolean ischecked, String checkValue) { 78 int start = stringBuffer.indexOf(checkValue); 79 int end = start + checkValue.length(); 80 if (ischecked) { 81 if (start == -1) { 82 stringBuffer.append(checkValue); 83 } 84 } else { 85 if (start != -1) { 86 stringBuffer.delete(start, end); 87 } 88 } 89 } 90 } 91 92 @Override 93 public boolean onCreateOptionsMenu(Menu menu) { 94 // Inflate the menu; this adds items to the action bar if it is present. 95 getMenuInflater().inflate(R.menu.main, menu); 96 return true; 97 } 98 99 @Override 100 public boolean onKeyUp(int keyCode, KeyEvent event) { 101 // 当点击回退时,弹出该窗口(也就相当于关闭操作) 102 if (keyCode == KeyEvent.KEYCODE_BACK) { 103 new AlertDialog.Builder(this).setTitle("是否退出?") 104 .setPositiveButton("确定", new OnClickListener() { 105 @Override 106 public void onClick(DialogInterface arg0, int arg1) { 107 finish(); 108 } 109 }).setNegativeButton("取消", null).show(); 110 return true; 111 } 112 return super.onKeyUp(keyCode, event); 113 } 114 }

 

总结

以上是生活随笔为你收集整理的Android:CheckBox控件的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。