如果用户在三个不同的文本字段中输入姓名、身高和体重,我正在尝试制作一个简单的 android 应用程序。一旦按下按钮,输入应显示在底部的文本视图中,高度从米英寸转换,重量从公斤转换为磅。问题是如果按下按钮并且应用程序粉碎的字段中没有文本!!!我如何通过显示一个警告对话框告诉用户在按下按钮之前输入文本来解决这个问题。我是 android 新手,因此我们将不胜感激任何帮助。
问问题
3556 次
3 回答
2
可以使用这个方法来查找用户是否在Edittext上输入了文字
在按钮的Onclick中,放入如下代码
if(name.getText().toString().equals("")) //name is the name of the Edittext in code
{
Toast.makeText(getBaseContext(), "You didn't enter the Name", Toast.LENGTH_SHORT).show();
}
else if(height.getText().toString().equals("")) //height is the name of the Edittext in code
{
Toast.makeText(getBaseContext(), "You didn't enter the Height", Toast.LENGTH_SHORT).show();
}
else if(weight.getText().toString().equals("")) //weight is the name of the Edittext in code
{
Toast.makeText(getBaseContext(), "You didn't enter the Weight", Toast.LENGTH_SHORT).show();
}
else
{
// put your your code
}
于 2013-01-24T11:25:53.463 回答
1
String UserName = username_edittext.getText().toString().trim();
String UserPassword = password_edittext.getText().toString().trim();
if (!(UserName.equals("") && UserPassword.equals(""))) {
Log.i(TAG, "enter uname and pass are empty");
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle("Alert");
// Setting Dialog Message
alertDialog.setMessage("Enter Value");
if(status != null)
// Setting alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dissmiss();
}
});
// Showing Alert Message
alertDialog.show();
} else {
Log.i(TAG, "Continue");
}
于 2013-01-24T11:31:53.587 回答
0
String UserName = username_edittext.getText().toString().trim();
String UserPassword = password_edittext.getText().toString().trim();
if (!(UserName.equals("") && UserPassword.equals(""))) {
Log.i(TAG, "enter uname and pass are empty");
CreateAlertDialog();
} else {
Log.i(TAG, "Continue");
}
于 2013-01-24T11:24:20.270 回答