我创建了一个简单的隐藏程序,它可以获取用户的输入并选择要隐藏的号码类型。单击“确定”按钮并在同一 Activity 中返回结果。它看起来如下图所示。但是,当我单击按钮返回值时,它无法工作或返回结果并显示程序,不幸的是,程序已停止?我怎样才能将它正确地传递给textview?y在同一 Activity 的结果(textview)中。它看起来像这张图片中的那样。
下面是我的代码
public class activity_tv9 extends tv9 {
private int mb;
private int tb;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tv9_act);
TextView tv = (TextView)findViewById(R.id.textView3);
EditText editview = (EditText)findViewById(R.id.editText1);
RadioButton rdb1 = (RadioButton)findViewById(R.id.radio0);
RadioButton rdb2 = (RadioButton)findViewById(R.id.radio1);
final RadioGroup rdg = (RadioGroup)findViewById(R.id.radioGroup1);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int value =rdg.getCheckedRadioButtonId();
rd=(RadioButton)findViewById(value);
if(rdg.getCheckedRadioButtonId()==-1){
Toast.makeText(activity_tv9.this,"Please select radio button",
Toast.LENGTH_LONG).show();
}
else {
switch (rd.getText().toString()){
case "MB":
double mb = Integer.parseInt(editview.getText().toString());
double val1 = mb*1024;
tv.setText(String.valueOf(val1));
break;
case "TB":
double tb = Integer.parseInt(editview.getText().toString());
double val2 = tb*1024;
tv.setText(String.valueOf(val2));
break;
default:
Toast.makeText(activity_tv9.this,"No input",
Toast.LENGTH_LONG).show();
}
}
}
});
}
最佳答案
请将 Integer.parseInt(editview.getText().toString())
更改为 Double.parseDouble(editview.getText().toString())
期望来自 editview
的 double
值:-
double mb = Double.parseDouble(editview.getText().toString());
double val1 = mb*1024;
tv.setText(String.valueOf(val1));
希望这能成功;
关于java - 无法将数据结果传递到android中的textview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44470409/