Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在EditText中更改焦点时如何显示toast?_Android_Android Edittext - Fatal编程技术网

Android 在EditText中更改焦点时如何显示toast?

Android 在EditText中更改焦点时如何显示toast?,android,android-edittext,Android,Android Edittext,我正在一个活动的登录页面上工作,其中用户名和密码作为两个文本框。 当焦点从用户名更改为密码框时,我试图显示Toast消息: -如果用户名框中输入的文本有空格。 -如果没有空白,则不显示烤面包片 EditText txtEdit = (EditText) findViewById(R.id.edittxt); txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() { public void

我正在一个活动的登录页面上工作,其中用户名和密码作为两个文本框。 当焦点从用户名更改为密码框时,我试图显示Toast消息: -如果用户名框中输入的文本有空格。 -如果没有空白,则不显示烤面包片

EditText txtEdit = (EditText) findViewById(R.id.edittxt);

 txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {          

        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
               if (txtEdit.getText().Contains(" ")) {
                 Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
            }
        }
    });

参考:

您可以使用View.OnFocusChangeListener进行此操作

代码示例:

et_uname.setOnFocusChangeListener(new View.OnFocusChangeListener(){
  @Override
  public void onFocusChange(View v, boolean hasFocus) {
     if (hasFocus == false && et_uname.getText().toString() != null && et_uname.getText().toString().contains(" ")){
        Toast.makeText(getActivity(), "Alert", Toast.LENGTH_LONG).show();                   
    }
  }
};
这是为片段完成的。如果是内部活动,则可以更换

getActivity() 

。这个

这里hasFocus=true表示当前有焦点,hasFocus=false表示没有焦点。由于您需要在焦点丢失时提醒用户,因此必须为hasFOcus==false添加一个条件。

您应该为这两个编辑都实现,请参阅下面的代码:

 EditText myEdit = (EditText) findViewById(R.id.edit1);

 myEdit .setOnFocusChangeListener(new OnFocusChangeListener() {          

    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
           // Do the check for white spaces and display the Toast
        }
    }
});
 EditText myEdit = (EditText) findViewById(R.id.edit1);

 myEdit .setOnFocusChangeListener(new OnFocusChangeListener() {          

    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
           // Do the check for white spaces and display the Toast
        }
    }
});