zoukankan      html  css  js  c++  java
  • 【风马一族_Android】多选按钮的监控事件

      1 import android.app.Activity;
      2 import android.os.Bundle;
      3 import android.text.TextUtils;
      4 import android.view.View;
      5 import android.widget.Button;
      6 import android.widget.CheckBox;
      7 import android.widget.CompoundButton;
      8 import android.widget.EditText;
      9 import android.widget.TextView;
     10 import android.widget.Toast;
     11 
     12 import com.sowsceo.rnadomsows.randomsows.R;
     13 
     14 import java.util.ArrayList;
     15 import java.util.Collections;
     16 import java.util.List;
     17 import java.util.Random;
     18 
     19 public class NumberActivity extends Activity {
     20 
     21     private CheckBox checkBoxNumber, checkBoxEnglish, checkBoxCharacter;
     22     
     23     /**
     24      * 有限的,让用户输入产生随机数的长度
     25      */
     26     private Button bt_definite;
     27     private EditText et_lengthVariable;
     28 
     29     /**
     30      * 长度不定,产生随机数
     31      */
     32     private Produce produce;
     33     private MultipleSelectionButtons multipleChecked;
     34     private Random random;
     35 
     36     /**
     37      * 传递数字的长度
     38      */
     39     private int numberLength;
     40 
     41     /**
     42      * 000: 从左到右算,第一个是数字、第二个是字母,第三个是特殊字符
     43      * <p>
     44      * 0:表示没有选中
     45      * 1:表示选中
     46      * <p>
     47      * 例如:101:在产生随机数据中,数据由数字和特殊字符组成,
     48      * 但是数字或特殊字符的数量不小于1个,不达到总长度
     49      * <p>
     50      * 001:在产生随机数据中,数据全部由字母组成
     51      * <p>
     52      * 总长度:如果要产生9个长度的随机数据,总长度就是9个。
     53      */
     54     private int[] identifier = {0, 0, 0};
     55 
     56     @Override
     57     protected void onCreate(Bundle savedInstanceState) {
     58         super.onCreate(savedInstanceState);
     59         setContentView(R.layout.activity_number);
     60 
     61         random = new Random();
     62 
     63         //组件的初始化
     64         init();
     65 
     66         //给组件提供监控事件
     67         monitor();
     68     }
     69 
     70     private void monitor() {
     71         checkBoxNumber.setOnCheckedChangeListener(multipleChecked);
     72         checkBoxEnglish.setOnCheckedChangeListener(multipleChecked);
     73         checkBoxCharacter.setOnCheckedChangeListener(multipleChecked);
     74 
     75     }
     76 
     77     /**
     78      * 初始化组件、方法、变量
     79      */
     80     private void init() {
     81         checkBoxNumber = (CheckBox) findViewById(R.id.checkBoxNumber);
     82         checkBoxEnglish = (CheckBox) findViewById(R.id.checkBoxEnglish);
     83         checkBoxCharacter = (CheckBox) findViewById(R.id.checkBoxCharacter);
     84         et_lengthVariable = (EditText) findViewById(R.id.et_lengthVariable);
     85         checkBox_capital = (CheckBox) findViewById(R.id.checkBox_capital);
     86         checkBox_lowercase = (CheckBox) findViewById(R.id.checkBox_lowercase);
     87 
     88         tv_Display = (TextView) findViewById(R.id.tv_Number);
     89 
     90         produce = new Produce();
     91         multipleChecked = new MultipleSelectionButtons();
     92     }
     93 
     94     public class Produce implements View.OnClickListener {
     95 
     96         int number;
     97 
     98         String strNumber;
     99 
    100         StringBuffer sb = new StringBuffer();
    101 
    102         @Override
    103         public void onClick(View view) {
    104             switch (view.getId()) {
    105                 case R.id.bt_Number6:
    106                     numberLength = 6;
    107                     break;
    108                 case R.id.bt_Number9:
    109                     numberLength = 9;
    110                     break;
    111                 case R.id.bt_Number12:
    112                     numberLength = 12;
    113                     break;
    114                 case R.id.bt_Number15:
    115                     numberLength = 15;
    116                     break;
    117                 case R.id.bt_definite:
    118                     String strLength = et_lengthVariable.getText().toString().trim();
    119 
    120                     if (TextUtils.isEmpty(strLength)) {
    121                         Toast.makeText(NumberActivity.this, "输入值不能为空", Toast.LENGTH_SHORT).show();
    122                     } else {
    123                         numberLength = Integer.parseInt(strLength);
    124                         if (numberLength < 1 || numberLength > 50) {
    125                             Toast.makeText(NumberActivity.this, "数字的长度只能是从1到50", Toast.LENGTH_SHORT).show();
    126                             numberLength = 0;
    127                         }
    128                     }
    129                     break;
    130             }
    131 
    132             String str = String.valueOf(identifier[0] * 100 + identifier[1] * 10 + identifier[2]);
    133 
    134             /**
    135              * 由于手机的性能原因,需要限制一次产生的随机数据的大小
    136              */
    137             for (int i = 0; i < numberLength; i++) {
    138                 switch (str) {
    139                     case "100":
    140                         strNumber = String.valueOf(random.nextInt(10));
    141                         break;
    142                     case "110":
    143 
    144                         break;
    145                     case "101":
    146                         break;
    147                     case "111":
    148                         break;
    149                     case "010":
    150                         byte b=65;
    151                         String s=String.valueOf((char) b);
    152                         System.out.println(s);
    153                         break;
    154                     case "011":
    155                         break;
    156                     case "001":
    157                         break;
    158                     case "000":
    159                         Toast.makeText(NumberActivity.this, "请最少选择一个", Toast.LENGTH_SHORT).show();
    160                         break;
    161                 }
    162                 sb.append(strNumber);
    163             }
    164 
    165             tv_Display.setText(sb.append(strNumber));
    166             sb = new StringBuffer("
    ");
    167         }
    168     }
    169 
    170     /**
    171      * identifier[]:数组有三个数,第一位表示 数字,第二位表示 字母,第三位 特殊字符
    172      *
    173      * 多选按钮的监听事件
    174      * CompoundButton compoundButton 判断是哪个多选按钮
    175      * boolean isChecked 多选按钮的
    176      */
    177     public class MultipleSelectionButtons implements CompoundButton.OnCheckedChangeListener {
    178         @Override
    179         public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
    180             switch (compoundButton.getId()) {
    181                 case R.id.checkBoxNumber:
    182                     if (isChecked)
    183                         identifier[0] = 1;
    184                     else
    185                         identifier[0] = 0;
    186                     break;
    187                 case R.id.checkBoxEnglish:
    188                     if (isChecked)
    189                         identifier[1] = 1;
    190                     else
    191                         identifier[1] = 0;
    192                     break;
    193                 case R.id.checkBoxCharacter:
    194                     if (isChecked)
    195                         identifier[2] = 1;
    196                     else
    197                         identifier[2] = 0;
    198                     break;
    199             }
    200         }
    201     }
    202 }
    203     
    每天完成一件事。 不管是为了什么。
  • 相关阅读:
    【题解】 bzoj1207: [HNOI2004]打鼹鼠 (动态规划)
    【题解】 bzoj1088: [SCOI2005]扫雷Mine (神奇的做法)
    【题解】 bzoj4472: [Jsoi2015]salesman (动态规划)
    【题解】 bzoj4033: [HAOI2015]树上染色* (动态规划)
    【题解】 [HNOI/AHOI2018]道路 (动态规划)
    炫酷的英文字体分享
    艾伦·麦席森·图灵
    历史上最知名的15位计算机科学家
    浏览器首页被改为2345之解决方法
    linux命令缩写及全称
  • 原文地址:https://www.cnblogs.com/sows/p/5601078.html
Copyright © 2011-2022 走看看