【风马一族_Android】多选按钮的监控事件

时间:2024-01-15 13:51:14
 import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; import com.sowsceo.rnadomsows.randomsows.R; import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random; public class NumberActivity extends Activity { private CheckBox checkBoxNumber, checkBoxEnglish, checkBoxCharacter; /**
* 有限的,让用户输入产生随机数的长度
*/
private Button bt_definite;
private EditText et_lengthVariable; /**
* 长度不定,产生随机数
*/
private Produce produce;
private MultipleSelectionButtons multipleChecked;
private Random random; /**
* 传递数字的长度
*/
private int numberLength; /**
* 000: 从左到右算,第一个是数字、第二个是字母,第三个是特殊字符
* <p>
* 0:表示没有选中
* 1:表示选中
* <p>
* 例如:101:在产生随机数据中,数据由数字和特殊字符组成,
* 但是数字或特殊字符的数量不小于1个,不达到总长度
* <p>
* 001:在产生随机数据中,数据全部由字母组成
* <p>
* 总长度:如果要产生9个长度的随机数据,总长度就是9个。
*/
private int[] identifier = {0, 0, 0}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_number); random = new Random(); //组件的初始化
init(); //给组件提供监控事件
monitor();
} private void monitor() {
checkBoxNumber.setOnCheckedChangeListener(multipleChecked);
checkBoxEnglish.setOnCheckedChangeListener(multipleChecked);
checkBoxCharacter.setOnCheckedChangeListener(multipleChecked); } /**
* 初始化组件、方法、变量
*/
private void init() {
checkBoxNumber = (CheckBox) findViewById(R.id.checkBoxNumber);
checkBoxEnglish = (CheckBox) findViewById(R.id.checkBoxEnglish);
checkBoxCharacter = (CheckBox) findViewById(R.id.checkBoxCharacter);
et_lengthVariable = (EditText) findViewById(R.id.et_lengthVariable);
checkBox_capital = (CheckBox) findViewById(R.id.checkBox_capital);
checkBox_lowercase = (CheckBox) findViewById(R.id.checkBox_lowercase); tv_Display = (TextView) findViewById(R.id.tv_Number); produce = new Produce();
multipleChecked = new MultipleSelectionButtons();
} public class Produce implements View.OnClickListener { int number; String strNumber; StringBuffer sb = new StringBuffer(); @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_Number6:
numberLength = 6;
break;
case R.id.bt_Number9:
numberLength = 9;
break;
case R.id.bt_Number12:
numberLength = 12;
break;
case R.id.bt_Number15:
numberLength = 15;
break;
case R.id.bt_definite:
String strLength = et_lengthVariable.getText().toString().trim(); if (TextUtils.isEmpty(strLength)) {
Toast.makeText(NumberActivity.this, "输入值不能为空", Toast.LENGTH_SHORT).show();
} else {
numberLength = Integer.parseInt(strLength);
if (numberLength < 1 || numberLength > 50) {
Toast.makeText(NumberActivity.this, "数字的长度只能是从1到50", Toast.LENGTH_SHORT).show();
numberLength = 0;
}
}
break;
} String str = String.valueOf(identifier[0] * 100 + identifier[1] * 10 + identifier[2]); /**
* 由于手机的性能原因,需要限制一次产生的随机数据的大小
*/
for (int i = 0; i < numberLength; i++) {
switch (str) {
case "100":
strNumber = String.valueOf(random.nextInt(10));
break;
case "110": break;
case "101":
break;
case "111":
break;
case "010":
byte b=65;
String s=String.valueOf((char) b);
System.out.println(s);
break;
case "011":
break;
case "001":
break;
case "000":
Toast.makeText(NumberActivity.this, "请最少选择一个", Toast.LENGTH_SHORT).show();
break;
}
sb.append(strNumber);
} tv_Display.setText(sb.append(strNumber));
sb = new StringBuffer("\n");
}
} /**
* identifier[]:数组有三个数,第一位表示 数字,第二位表示 字母,第三位 特殊字符
*
* 多选按钮的监听事件
* CompoundButton compoundButton 判断是哪个多选按钮
* boolean isChecked 多选按钮的
*/
public class MultipleSelectionButtons implements CompoundButton.OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
switch (compoundButton.getId()) {
case R.id.checkBoxNumber:
if (isChecked)
identifier[0] = 1;
else
identifier[0] = 0;
break;
case R.id.checkBoxEnglish:
if (isChecked)
identifier[1] = 1;
else
identifier[1] = 0;
break;
case R.id.checkBoxCharacter:
if (isChecked)
identifier[2] = 1;
else
identifier[2] = 0;
break;
}
}
}
}