Java 如何将EditText中的文本转换为整数变量
我试图从EditText中获取文本的值作为一个整数,但我发现每次插入任何值时,它都会从catch中计算初始化值,那么这个问题的解决方法是什么呢 结果变为=0Java 如何将EditText中的文本转换为整数变量,java,android,Java,Android,我试图从EditText中获取文本的值作为一个整数,但我发现每次插入任何值时,它都会从catch中计算初始化值,那么这个问题的解决方法是什么呢 结果变为=0 public Button bu,bu2,bu3,bu4,bu5; int num=0,num2=0,num3=0,result=0; public EditText on,on2,on3,on4; public TextView tex ; @Override protected void onCreate(Bundle sa
public Button bu,bu2,bu3,bu4,bu5;
int num=0,num2=0,num3=0,result=0;
public EditText on,on2,on3,on4;
public TextView tex ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_monthlycalc);
bu=(Button)findViewById(R.id.year);
bu.setOnClickListener(this);
bu2=(Button)findViewById(R.id.calc);
bu2.setOnClickListener(this);
bu3=(Button)findViewById(R.id.reset);
bu3.setOnClickListener(this);
bu4=(Button)findViewById(R.id.H1);
bu4.setOnClickListener(this);
bu5=(Button)findViewById(R.id.ron);
bu5.setOnClickListener(this);
try {
on=(EditText)findViewById(R.id.loanAm);
num=Integer.valueOf(on.getText().toString().trim());
} catch (NumberFormatException e) {
num=0;
}
try {
on2=(EditText)findViewById(R.id.intRate);
num2=Integer.valueOf(on2.getText().toString().trim());
} catch (NumberFormatException e) {
num2=0;
}
try {
on3=(EditText)findViewById(R.id.lonTerm);
num=Integer.valueOf(on3.getText().toString().trim());
} catch (NumberFormatException e) {
num3=0;
}
tex = (TextView) findViewById(R.id.monPay);
result=num+num2+num3;
@Override
public void onClick(View v)
{
if (v.getId()==R.id.calc)
tex.setText(String.valueOf(result));
}
因为您的字符串中可能有非数字的字符或空格 使用regex解析它
final Pattern pattern = Pattern.compile("\\d+"); // the regex
final ArrayList<Integer> ints = new ArrayList<Integer>(); // results
while (matcher.find()) { // for each match
ints.add(Integer.parseInt(matcher.group())); // convert to int
}
这也会删除所有选项卡
如果您没有定义默认值,请确保在单击按钮时(而不是之前)抓取您的值,因为在初始化时,按钮内没有任何值。将代码移动到
onClick
您正在尝试获取onCreate
中的值,这是在活动启动且所有EditText
都为空时发生的,请将其放入onClick
中,它将起作用:)
我已经这样做了,但在本例中,第一个数字没有添加到sumation@Shereef MarzoukI不明白,你能解释更多吗?在本例中,结果仅为num2+num3,但我没有更改任何内容,只是将其移动到onClick方法
st.replaceAll("\\s+","")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_monthlycalc);
bu=(Button)findViewById(R.id.year);
bu.setOnClickListener(this);
bu2=(Button)findViewById(R.id.calc);
bu2.setOnClickListener(this);
bu3=(Button)findViewById(R.id.reset);
bu3.setOnClickListener(this);
bu4=(Button)findViewById(R.id.H1);
bu4.setOnClickListener(this);
bu5=(Button)findViewById(R.id.ron);
on=(EditText)findViewById(R.id.loanAm);
on2=(EditText)findViewById(R.id.intRate);
on3=(EditText)findViewById(R.id.lonTerm);
tex = (TextView) findViewById(R.id.monPay);
}
@Override
public void onClick(View v)
{
try {
num=Integer.valueOf(on.getText().toString().trim());
} catch (NumberFormatException e) {
num=0;
}
try {
num2=Integer.valueOf(on2.getText().toString().trim());
} catch (NumberFormatException e) {
num2=0;
}
try {
num=Integer.valueOf(on3.getText().toString().trim());
} catch (NumberFormatException e) {
num3=0;
}
result=num+num2+num3;
if (v.getId()==R.id.calc)
tex.setText(String.valueOf(result));
}