我的学校有一个项目,我从EditText
获取值并对其进行计算,问题是我无法从EditText
检索值。这是我的代码:
public class MainActivity extends ActionBarActivity {
int valuearg1, valuearg2;
float valuearg3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText arg1 = (EditText) findViewById(R.id.editArg1);
EditText arg2 = (EditText) findViewById(R.id.editArg2);
EditText arg3 = (EditText) findViewById(R.id.editArg3);
try {
String arg1value = arg1.getText().toString();
valuearg1 = Integer.parseInt(arg1value);
String arg2value = arg2.getText().toString();
valuearg2 = Integer.parseInt(arg2value);
String arg1value3 = arg3.getText().toString();
valuearg3 = Float.parseFloat(arg1value3);
final AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setMessage("Values are :" + valuearg1 + "\n"
+ valuearg2 + "\n"
+ valuearg3)
.setNegativeButton(R.string.okay, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "test toast", Toast.LENGTH_LONG).show();
}
});
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
builder1.show();
}
});
}
catch (Exception e)
{
Toast.makeText(this, "bug detected", Toast.LENGTH_LONG).show();
}
}
提前感谢您的帮助。
答案 0 :(得分:0)
尝试插入
builder1.setMessage
在OnClick块中。
答案 1 :(得分:0)
当然你不能,你正试图在它们被创造之后获得它们的价值。你实际想要做的是将对话创建到一个不同的函数,而不是onCreate()。
答案 2 :(得分:0)
创建活动后会立即读取这些值,因为您正在onCreate()
中读取值。你应该做的是删除所有计算代码形式onCreate()
,并简单地声明arg1,arg2,arg3和按钮。然后该按钮的onClick()
应调用compute()或某些此类函数,在单击按钮后读取这些EditText
中的值,计算答案然后显示。
答案 3 :(得分:0)
当您获取值时,EditText框中没有值,因为您在创建活动时获得了值。
您需要在调用按钮单击事件时获取值。
这样的事情会起作用:
public class MainActivity extends ActionBarActivity {
int valuearg1, valuearg2;
float valuearg3;
EditText arg1, arg2, arg3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arg1 = (EditText) findViewById(R.id.editArg1);
arg2 = (EditText) findViewById(R.id.editArg2);
arg3 = (EditText) findViewById(R.id.editArg3);
try {
final AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setMessage("Values are :" + valuearg1 + "\n"
+ valuearg2 + "\n"
+ valuearg3)
.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "test toast", Toast.LENGTH_LONG).show();
}
});
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String arg1value = arg1.getText().toString();
valuearg1 = Integer.parseInt(arg1value);
String arg2value = arg2.getText().toString();
valuearg2 = Integer.parseInt(arg2value);
String arg1value3 = arg3.getText().toString();
valuearg3 = Float.parseFloat(arg1value3);
builder1.setMessage("Values are :" + valuearg1 + "\n"
+ valuearg2 + "\n"
+ valuearg3)
.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "test toast", Toast.LENGTH_LONG).show();
}
});
builder1.show();
}
});
}
catch (Exception e)
{
Toast.makeText(this, "bug detected", Toast.LENGTH_LONG).show();
}
}