简体   繁体   English

在Android中显示Toast消息时出现问题

[英]Issue in displaying toast message in android

According to my requirement in android, if the user presses only "space" and clicks "post" button a toast should be displayed. 根据我在android中的要求,如果用户仅按下“空格”并单击“发布”按钮,则应显示一个祝酒词。 So i have the code as below, but the toast doesnt get displayed. 所以我有下面的代码,但吐司没有得到显示。 please can anybody tell why it is not displaying ? 请谁能告诉我为什么它不显示?

   mEditText.addTextChangedListener(mTextEditorWatcher);    
  Button button = (Button) findViewById(R.id.button1);

 button.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        String result =mEditText.getText().toString();
         if(result.contains ("\\s"))
           Toast.makeText(getApplicationContext(), "Space ", Toast.LENGTH_LONG).show();
    }
});
}

I dont think contains get you the best result, try this: 我认为包含不会为您带来最佳效果,请尝试以下操作:

if(result.equals(" "))
    Toast.makeText(getApplicationContext(), "Space ", Toast.LENGTH_LONG).show();

Looks like you are trying to use regex with String.contains method. 看起来您正在尝试将正则表达式与String.contains方法一起使用。 String.contains method works with String . String.contains方法可用于String It doesn't work with regex . 它不适用于regex

See this question for more details: How to use regex in String.contains() method in Java 有关更多详细信息,请参见此问题: 如何在Java中的String.contains()方法中使用正则表达式

By the way, you should not check for just one space. 顺便说一句,您不应该只检查一个空格。 You should handle any number of spaces. 您应该处理任意数量的空格。

if(result.length()!=0 && result.trim().equals(""))
    ShowToast();

Try this.. 尝试这个..

if(result.contains (" "))
           Toast.makeText(getApplicationContext(), "Space ", Toast.LENGTH_LONG).show();

You have a two way of solutions 您有两种解决方案

1.You should check this condition by " " --> if(result.contains ("\\s")) 1.您应该通过“”-> if(result.contains(“ \\ s”))检查此条件

Example: 例:

if(result.contains(" "))

2.You Should Check with Pattern 2.你应该检查模式

Example: 例:

String result =ed.getText().toString();
       Pattern pattern = Pattern.compile("\\s");
   Matcher matcher = pattern.matcher(result);
   boolean found = matcher.find();
   if(found)
         Toast.makeText(getApplicationContext(), "Space ", Toast.LENGTH_LONG).show();

Please use any of these solutions.It will work. 请使用以下任何一种解决方案。

I changed the if condition to space and i am getting a Toast message successfully. 我将if条件更改为space,并且我成功收到Toast消息。

public class MainActivity extends Activity implements OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button) findViewById(R.id.Button1);
    final EditText mEditText = (EditText) findViewById(R.id.EditText1);

    button.setOnClickListener(new View.OnClickListener() {

       public void onClick(View v) {
           // TODO Auto-generated method stub
           String result =mEditText.getText().toString();
           Log.e("Test Message", result);
           if(result.contains (" "))
             Toast.makeText(getApplicationContext(), "Space ", Toast.LENGTH_LONG).show();
       }
   });
}

Please try and let me know if you are still facing the issue ... 如果您仍然遇到问题,请尝试让我知道...

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM