我试图在单击按钮以指示用户填写空白字段时显示消息。目前,如果该字段为空,则会崩溃/强制关闭应用程序。我试图做下面的代码,并没有成功。原来我没有那里的if/else,我只是跑了计算器();方法和下面的imm代码。要求用户填写空白的“EditText”字段的Toast消息
有人可以指引我进入正确的方向吗?
public void onClick(View v)
{
if ((EditText)findViewById(R.id.amount1)== null)
{
Context context = getApplicationContext();
CharSequence text = "Enter a number";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else
{
calculator();
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
Im相当肯定,这是不好的代码:
if ((EditText)findViewById(R.id.amount1)== null)
只是不知道如何字是我想要的方式。
太棒了,这工作完美。感谢大家的投入! – Xarddrax 2011-05-17 21:50:52