2016-01-31 66 views
1

我有以下代碼 - 當EditText在焦點之下時,我希望提示消失,否則顯示回來。在調試中,我看到EditText的焦點已經丟失並且失去焦點(setHint代碼行被執行),但是沒有顯示提示(提示刪除但沒有以失去的焦點返回)。我仔細檢查了代碼中的其他地方,除了在下面的hasFocus以外,我沒有用其他東西覆蓋它。Android EditText設置提示以編程方式不起作用

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     if (hasFocus) { 
      editText.setHint(getString(R.string.empty_string)); 
     } else { 
      editText.setHint(getString(R.string.hint_string)); 
     } 
    } 
}); 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="56dp" 
    android:hint="@string/hint_string" 
    android:textSize="22sp" 
    android:textColor="@color/white" 
    android:textColorHint="@color/white" 
    android:fontFamily="sans-serif-light" 
    android:layout_marginLeft="7dp" 
    android:layout_marginRight="7dp" 
    android:paddingLeft="24dp" 
    android:paddingRight="24dp" 
    android:elevation="3dp" 
    android:textCursorDrawable="@null" 
    android:background="@color/primary_accent" 
    android:layout_below="@id/main_activity_toolbar" 
    android:layout_gravity="center_horizontal" /> 

回答

1

希望這有助於:

 editText.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       editText.setHint(""); 
       return false; 
      } 

     }); 

     editText.setOnFocusChangeListener(new OnFocusChangeListener() { 

      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       if (!hasFocus){ 
        editText.setHint("Hint"); 
       } 
      } 
     }); 
+0

感謝,就是從我做過什麼區別? – michael

+0

@michael我用onTouchListener檢查edittext是否有效。 – AKSiddique

1

嘗試......

< EditText 
    android:id="@+id/show_time_search_text" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_marginLeft="10dp" 
    android:layout_weight="1" 
    android:background="@color/full.tranparent" 
    android:cursorVisible="false" 
    android:hint="Your hint text" 
    android:inputType="text" 
    android:textColor="@color/black" 
    android:textColorHint="#c6c6c6" 
    android:textSize="14.2dp" /> 
+0

謝謝,但仍然無法正常工作 – michael

+0

那麼,當你鍵入內容時,提示並沒有消失? – Ritesh

+0

它消失了,問題是當編輯文本失去了重點 - 提示不回來 – michael

相關問題