Buna benzeyen bir editText nasıl oluşturabilirim?Android EditText farklı yüzer etiket ve yer tutucu ile
cevap
Sen olarak aşağıda düzen xml dosyasına aşağıda kullanabilirsiniz. görünüm odaklı değilken TextInputEditText
den android:hint="Placeholder"
TextInputLayout
den android:hint="Label"
ile aynı anda görünür olduğunu
<?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>
<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>
dikkat edin. Bu etiketi göstermek ve gizlemek için java kodunuza fazladan kontrol edebilirsiniz. Veya TextInputLayout
'dan android:hint="Placeholder"
'u bırakın.
TextInputLayout
için
android:theme="@style/TextLabel
kullanarak tema ayarlamak gerekir ve renk vurgusu var ayarlayın. Eğer EDITTEXT odağı daha sonra textinputlayout kullanırsanız
<style name="TextLabel" parent="TextAppearance.AppCompat.Light">
<item name="colorAccent">@color/yourColor</item>
</style>
, herhangi yer tutucu alamadım.
Düzen:
<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>
Sen EDITTEXT bir odak değişikliği dinleyici belirlemek zorunda.
Java:
usernameTxt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
usernameTxt.setHint("Label");
} else {
usernameTxt.setHint("Placeholder");
}
}
});
Sen TextInputLayout
ve EditText
kullanarak bunu yapabilirsiniz. TextInputLayout
için android:hint="Label"
her zaman ipuçları Label
göstermek için
<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>
1. Add özellik:
İşte XML olduğunu.
2.
ProgramlıEditText
ipuçları EditText
odaklı olsun sadece Placeholder
ayarlayın. Faaliyetinizdeki hatları aşağıda ekleyin:
.........
.................
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("");
}
}
});
.........
..................
ÇIKIŞ:
Umut bu yardımcı olacaktır ~
Sen KOTLIN içinde (kod aşağıdaki kullanabilirsiniz). 200 ms gecikme sonrasında (çakışan ipucu ve yer tutucuyu önlemek için) yer tutucu gösterecektir.
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 = ""
}
}
}
}
ve sonra düzen xml sınıfında
:<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>
etiket tutmak ve odaklanmamış halde ekran görüntüsü gibi yer tutucu bir yolu var mı? –
Evet, mümkün. Ebeveyn/kapsayıcı düzenine android: focusableInTouchMode = "true" 'kullanın. – FAT