启用fullsreen模式时显示/隐藏editText的提示文本

时间:2016-08-16 23:03:48

标签: android android-edittext

在全屏模式下打开edittext时如何显示提示文字? 我可以使用OnFocusChangeListener显示提示文本。

 editChangeName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            editChangeName.setHint(b ? getResources().getString(R.string.name) : "");
        }
    });

但是

关闭edittext后如何隐藏提示文本?

1 个答案:

答案 0 :(得分:0)

   // assuming you have a EditText view with id 
   View viewById = getView().findViewById(R.id.editTextId);
   if(viewById!=null) {
           //  set listener to this view "many views could share one listener"
           viewById.setOnFocusChangeListener(new View.OnFocusChangeListener() {
               @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    // if view match id &&  has no focus 
                    if(v.getId() == R.id.editTextId && !hasFocus) 
                             // clear hint 
                             ((EditText) v).setHint(null);
                    // else show hint 
                }
          });
   }

hasFocus< - 如名字所示

  • 有(true) - >秀
  • 没有(假) - >隐藏

如果您不确定按ID分配的是您分配给编辑文本的那个,请进行检查(仔细检查不会受到影响)

  if (EditText.class.isAssignableFrom(v.getClass()))
     // do cast  
     EditText editText = (EditText) v;

此代码是正确的。但。关闭softkeyboard后如何隐藏hinttext ... edittext有焦点 - Eugenu Yansky

Detecting when user has dismissed the soft keyboard

How to remove focus without setting focus to another control?

How to clear focus and remove keyboard on Android?