• Android:CheckBox控件


    1)ChexkBox继承自CompoundButton组件;

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

    3)监听事件:CompoundButton.OnCheckedChangeListener

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

    1)效果:

    2)源代码:

    reslayoutactivity_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" >
    
    
        <CheckBox
            android: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" />
    
        <TextView
            android: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="" />
    
        <CheckBox
            android: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 }
  • 相关阅读:
    IntelliJ Idea 常用快捷键列表
    管理Django1.9静态文件static
    django模板中如何导入js、css等外部文件
    Struts2+hibernate 结合,实现登陆校验
    python统计文档中词频
    Hibernate连接各种数据库的配置
    hibernateDAO层基本的增删改查
    vue2.0+element+node+webpack搭建的一个简单的后台管理界面
    WEB 前端开发插件整理
    机器学习之条件随机场(八)
  • 原文地址:https://www.cnblogs.com/yy3b2007com/p/7767899.html
Copyright © 2020-2023  润新知