我正在编写程序以使用edittext获取用户输入并将textview转换为显示该文本。
输入是数字,我已经正确地将String转换为整数值,唯一的问题是我必须使用try catch块来捕获数字格式错误。问题是我必须在try catch块中包含用于将TextView设置为输入的按钮,以便按钮知道用户设置的值是什么。
try{
EditText hour = (EditText) findViewById(R.id.hourin);
EditText min = (EditText) findViewById(R.id.minin);
final TextView FinalTime = (TextView) findViewById (R.id.FinalTimeDisplay);
String hoursString = hour.getText().toString();
String minsString = min.getText().toString();
int hours = Integer.parseInt(hoursString);
int mins = Integer.parseInt(minsString);
final String time = (hours + ":" + mins);
Button Submit = (Button) findViewById(R.id.submit);
Submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FinalTime.setText(time);
}
});
} catch (NumberFormatException e){
}
答案 0 :(得分:0)
Button Submit = (Button) findViewById(R.id.button1);
Submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
int hours = 0;
int mins = 0;
int breaktimes = 0;
hours = Integer.parseInt(hour.getText().toString());
mins = Integer.parseInt(min.getText().toString());
breaktimes = Integer.parseInt(breaktime.getText().toString());
mins = (mins + breaktimes);
if (mins >= 60){
hours = hours + 1;
mins = mins - 60;
}
if (hours > 12){
hours = hours - 12;
}
if (mins < 10){
FinalTime.setText(hours +":" +"0"+ mins);
}else{
FinalTime.setText(hours + ":" +mins);
}
}catch (NumberFormatException e){
}
}
});
答案 1 :(得分:-1)
也许,您需要在public void onClick()
顶部添加 @Override 装饰器?!
@Override
public void onClick(View v) {
FinalTime.setText(time);
}