5
A
回答
0
您可以如下使用以下佈局的xml文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:textColor="@color/wallet_holo_blue_light" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/editText2"
android:hint="Placeholder" />
</LinearLayout>
2
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Label">
<android.support.design.widget.TextInputEditText
android:hint="Placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
注意android:hint="Placeholder"
從TextInputEditText
在同一時間android:hint="Label"
從TextInputLayout
時可見視圖不集中。你可以在你的java代碼中做一些額外的檢查來顯示和隱藏標籤。或者只需從TextInputLayout
開始android:hint="Placeholder"
。
要更改顏色,您需要使用android:theme="@style/TextLabel
設置TextInputLayout
的主題,並設置顏色重音。
<style name="TextLabel" parent="TextAppearance.AppCompat.Light">
<item name="colorAccent">@color/yourColor</item>
</style>
0
如果您在edittext的焦點上使用textinputlayout,那麼您沒有得到任何佔位符。
佈局:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/username_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
你要設置的EditText的焦點變化監聽器。
的Java:
usernameTxt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
usernameTxt.setHint("Label");
} else {
usernameTxt.setHint("Placeholder");
}
}
});
6
可以使用TextInputLayout
和EditText
做到這一點。
這裏是你的XML:
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Label">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
添加屬性android:hint="Label"
到TextInputLayout
顯示其提示Label
始終。
2.編程設置EditText
提示Placeholder
只有當EditText
得到關注。
添加以下線在你的活動:
.........
.................
final EditText editText = (EditText) findViewById(R.id.edit_text);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
editText.setHint("Placeholder");
} else {
editText.setHint("");
}
}
});
.........
..................
OUTPUT:
希望這將有助於〜
2
您可以使用下面的代碼(在科特林)。它將在延遲200毫秒後顯示佔位符(以避免重疊提示和佔位符)。
class PlaceholderEditText : TextInputEditText {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
private val placeholder = hint
init {
hint = ""
onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
postDelayed({ hint = placeholder }, 200)
} else {
hint = ""
}
}
}
}
,然後在佈局XML類:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ALWAYS VISIBLE LABEL">
<com.myapp.widget.PlaceholderEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="DISAPPEARING PLACEHOLDER" />
</android.support.design.widget.TextInputLayout>
相關問題
- 1. 浮動佔位符
- 2. EditText浮動標籤重力
- 3. Android - 更改TextInputLayout的位置浮動標籤(底部浮動標籤)?
- 4. 去掉佔位符內的xml標籤
- 5. Tensorflow,帶有Estimator(model_fn)的佔位符?
- 6. 不能嵌套佔位符捕獲相同的佔位符'
- 7. 佔位符VS標籤用於HTML5
- 8. 使用HTML5佔位符等標籤
- 9. 隱藏的標籤,但顯示佔位符在同一時間?
- 10. Android HTML佔位符
- 11. Spring MVC jsp標籤佔位符標籤值
- 12. 如何創建一個帶有佔位符動畫的表單?
- 13. UITextView標籤中的佔位符不會消失
- 14. 不同顏色的佔位符
- 15. 如何從UISearchBar移動佔位符標籤
- 16. 佔位符不是在目標Maven的
- 17. 動態更改標籤和佔位符取決於加載的html
- 18. 帶有深色文字的佔位符(不暈)
- 19. 你如何製作佔位符和標籤轉換?
- 20. 在所有標籤上應用佔位符?
- 21. 如何在OnFocusChangeListeners更新UI線程之前讓EditText浮動標籤浮動?
- 22. PHP替換字符串標籤/佔位符/標記與動態值
- 23. 造型MD-無浮佔位符
- 24. 不同的標籤圖標和主要活動在android 7
- 25. pymssql和佔位符
- 26. 帶有文本佔位符的Phonegap和Jquery移動輸入號碼
- 27. 如何在wordpress comments.php中獲取佔位符而不是標籤?
- 28. asp標籤在asp.net內容佔位符中不被識別
- 29. 的Android的EditText怪異浮動光標下的白盒
- 30. 帶有佔位符右對齊的UITextField不能正確地向右移動
有沒有一種方法,以保持標籤和佔位符像對焦狀態的截圖? –
是可能的。使用'android:focusableInTouchMode =「true」'爲父/容器佈局。 – FAT