我試圖在單擊按鈕以指示用戶填寫空白字段時顯示消息。目前,如果該字段爲空,則會崩潰/強制關閉應用程序。我試圖做下面的代碼,並沒有成功。原來我沒有那裏的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