日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android:CheckBox控件

發布時間:2025/3/20 Android 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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控件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。