我希望逐一阅读Edit Text
中的数字,每当我点击按钮时,我会在下次点击时获得一个数字和下一个数字。
public class addition extends AppCompatActivity {
EditText editText1,editText2;
Button button;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.additionop);
button=(Button)findViewById(R.id.calculate);
editText1=(EditText)findViewById(R.id.firstnum);
editText2=(EditText)findViewById(R.id.secondnum);
textView=(TextView)findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num1= Integer.parseInt(editText1.getText().toString());
int num2=Integer.parseInt(editText2.getText().toString());
答案 0 :(得分:0)
在班级
添加这些变量public int index = 0;
EditText[] editTexts = new EditText[2]; // as per your size
初始化后,在onCreate
editTexts[0] = editText1;
editTexts[1] = editText2;
在onClick
执行此操作
int num = Integer.parseInt(editTexts[index++].getText().toString());
注意:这样您就可以拥有任意数量的EditText
。
答案 1 :(得分:0)
你可以试试这样的东西
public class addition extends AppCompatActivity {
EditText editText1,editText2;
Button button;
TextView textView;
boolean EditText1, EditText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.additionop);
button=(Button)findViewById(R.id.calculate);
editText1=(EditText)findViewById(R.id.firstnum);
editText2=(EditText)findViewById(R.id.secondnum);
textView=(TextView)findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!EditText1){
int num1= Integer.parseInt(editText1.getText().toString());
EditText1 = true;
}
else if(!EditText2){
int num2=Integer.parseInt(editText2.getText().toString());
EditText2 = true;
}
}
});
}
}
答案 2 :(得分:0)
private boolean shouldShowNum1 = true;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int number;
if ( shouldShowNum1 ) {
shouldShowNum1 = false;
number = Integer.parseInt(editText1.getText().toString());
} else {
shouldShowNum1 = false;
number = Integer.parseInt(editText2.getText().toString());
}
// do whatever you want with number
}
}
答案 3 :(得分:0)
如果你想要从最后一个到第一个的数字,那就这样做:
$row
类似于从第一个到最后一个获得数字。
答案 4 :(得分:0)
如果你想从1 editTextbox逐个读取数字,请使用:
int buttonClicked = 0;
int pos = 0;
int[] num = new int[];
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = editText1.getText().toString(); //Get text from editText box
//Check if editText contains any text
if(!text.equals("")
{
int length = editText.getText().toString().length(); //Get the lenght of the entered string
//Check if end of string has been reached
if(buttonClicked <= length-1){
int number = Character.getNumericValue(text.charAt(buttonClicked)); //Get the next character
//Check if character is a number, if not keep looking until the end of the string
while(number == -1 && buttonClicked <= length-1){
number = Character.getNumericValue(text.charAt(buttonClicked)); //Get the next character
buttonClicked++; //Increment buttonClicked
}
//Check if (last) character is a number
if(number != -1){
num[pos] = number; //Save the number in the array
pos++; //Increment the array position
buttonClicked++ //Increment buttonClicked
}
}
//End of string has been reached
else{
//Maybe reset everything
pos = 0;
buttonClicked = 0;
}
}
}
}
现在:如果您在editText
框中输入 178abc896 。
比输出结果如下:
第一次点击 - num [0] 包含: 1
第二次点击 - num [1] 包含: 7
第三次点击 - num [2] 包含: 8
第4次点击 - num [3] 包含: 8
第5次点击 - num [4] 包含: 9
第6次点击 - num [5] 包含: 6