显示警告对话框是文本字段为空

时间:2013-01-24 11:20:52

标签: android

我正在尝试制作一个简单的Android应用程序,用户在三个不同的文本字段中输入名称,高度和重量。按下按钮后,输入应显示在底部的文本视图中,高度将从米英寸和重量从千克转换为磅。问题是如果按下按钮并且应用程序崩溃的字段中没有文本!如何通过显示警告对话框来解决此问题,告诉用户在按下按钮之前输入文本。我是android的新手,所以任何帮助都将受到高度赞赏。

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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");
    }

答案 2 :(得分: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");
}