【问题标题】:displaying an alert dialog is a text field is empty显示警报对话框是文本字段为空
【发布时间】:2013-01-24 19:31:53
【问题描述】:

如果用户在三个不同的文本字段中输入姓名、身高和体重,我正在尝试制作一个简单的 Android 应用程序。一旦按下按钮,输入应显示在底部的文本视图中,高度从米英寸转换,重量从公斤转换为磅。问题是如果按下按钮并且应用程序粉碎的字段中没有文本!!!我如何通过显示一个警告对话框告诉用户在按下按钮之前输入文本来解决这个问题。我是 android 新手,因此我们将不胜感激任何帮助。

【问题讨论】:

标签: android


【解决方案1】:

您可以使用此方法来查找用户是否在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
}

【讨论】:

    【解决方案2】:
     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");
        }
    

    【讨论】:

      【解决方案3】:
      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");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        • 2020-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多