English 中文(简体)
当用户使用空字段提交时如何发出警报
原标题:How to alert when user submits with empty fields
  • 时间:2011-02-16 08:22:19
  •  标签:
  • android

我想在代码中添加一个警告,这样当用户在没有填写字段的情况下单击按钮时,就会出现错误消息。请提出一些措施。

这是我的代码:

    Button button1 = (Button) findViewById(R.id.widget45);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

          tv= name.getText().toString();
           tv1 = pass.getText().toString();
           x.setText(tv);
           y.setText(tv1);
       }
    });


}

}

问题回答

你可以这样做:

Button button1 = (Button) findViewById(R.id.widget45);
button1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

      tv= name.getText().toString();
      tv1 = pass.getText().toString();
      if( tv.length() == 0 || tv1.length() == 0 ) {
          Toast.makeText(v.getContext(), R.string.input_not_filled, Toast.LENGTH_SHORT).show();
          return;
      }
      x.setText(tv);
      y.setText(tv1);
   }
});

这将检查是否有任何返回字符串为空。如果是,则用字符串.xml中保存的名为input_not_filled的文本烤一条小消息1秒钟。当然也不会处理setText。当然,您可以独立检查元素。

我认为这是最简单的方法。

干杯

好的,尝试使用AlertDialog。检查字段是否为空,如果为空,

final AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext())
                    .create();
alertDialog.setTitle("Warning");
alertDialog.setMessage("Please fill required fields");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    alertDialog.dismiss();
                    //Warn user
                }
            });
alertDialog.show();

尽管我认为使用Toast+Animation会更好,正如谷歌的例子所示。无需打扰用户。

嗨,欢迎来到Stackoverflow。

此网站旨在帮助您发现错误,并在其他用户的帮助下进行修复。因此,如果你不把每个a、i、e、o或u都放出来,这也是有帮助的。

现在来谈谈你的问题。

要检查字段是否为空,请按照以下方式进行测试:

tv = name.getText().toString();
if(tv.trim().equals("")) {
    // text is empty

    showDialog(EMPTY_TEXT_ALERT);
}

现在,您已经调用了showDialog,您必须在活动类中覆盖onCreateDialog(int id)。您还必须定义我在此处使用的EMPTY_TEXT_ALERT变量。

private final static int EMPTY_TEXT_ALERT = 0;

@Override
protected Dialog onCreateDialog(int id) {
    switch(id) {
        case EMPTY_TEXT_ALERT: {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Your error message")
                   .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                       @Override
                       public void onClick(DialogInterface dialog, int which) {
                           dialog.cancel();
                       }
             });

             return builder.create();
        }
    }

    return null;
}

这样,使用适当的id调用showDialog将显示对话框,然后单击将创建的OK按钮将关闭对话框。

这很简单,效果很好

EditText ed=(EditText)findViewById(R.id.ed1);
String var=ed.getText().toString();
if(var.length()==0) {
   ed.setError("Your message");
}
else {
   // continue
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签