Jak mogę ustawić tekst EditText?
Jak mogę ustawić tekst EditText?
Odpowiedzi:
Jeśli sprawdzisz dokumenty EditText, znajdziesz setText()metodę. Zajmuje a Stringi a TextView.BufferType. Na przykład:
EditText editText = (EditText)findViewById(R.id.edit_text);
editText.setText("Google is your friend.", TextView.BufferType.EDITABLE);
To również dziedziczy TextViews” setText(CharSequence)i setText(int)metod, dzięki czemu można ustawić go jak zwykły TextView:
editText.setText("Hello world!");
editText.setText(R.string.hello_world);
EditText.BufferType.EDITABLE?
EditTextrozciąga się TextView; TextView.BufferType.EDITABLEjest poprawną stałą.
setText(CharSequence)ze swojej superklasy TextView. Więc nie jestem pewien, dlaczego jest to najbardziej przychylna i akceptowana odpowiedź?
String string="this is a text";
editText.setText(string)
Znalazłem String jako przydatną pośrednią podklasę CharSequence
http://developer.android.com/reference/android/widget/TextView.html znajdź setText (tekst CharSequence)
http://developer.android.com/reference/java/lang/CharSequence.html
String text = "Example";
EditText edtText = (EditText) findViewById(R.id.edtText);
edtText.setText(text);
Sprawdź to EditText akceptuj tylko wartości String, jeśli to konieczne, przekonwertuj je na ciąg.
Jeśli int, double, long value, wykonaj:
String.value(value);
Użyj +, operatora konkatenacji ciągów:
ed = (EditText) findViewById (R.id.box);
int x = 10;
ed.setText(""+x);
albo użyj
String.valueOf(int):
ed.setText(String.valueOf(x));
albo użyj
Integer.toString(int):
ed.setText(Integer.toString(x));
To jest rozwiązanie w Kotlinie
val editText: EditText = findViewById(R.id.main_et_name)
editText.setText("This is a text.")
editTextObject.setText(CharSequence)
http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence )
Musisz:
EditText in the xml file EditText w działaniuEditTextRozwiązanie w Android Java:
Uruchom swój EditText, identyfikator przychodzi do twojego xml id.
EditText myText = (EditText)findViewById(R.id.my_text_id);w metodzie OnCreate, po prostu ustaw tekst według zdefiniowanej nazwy.
String text = "here put the text that you want"użyj metody setText z twojego editText.
myText.setText(text); //variable from point 2Jeśli chcesz ustawić tekst w xmlpliku w czasie projektowania , po prostu android:text="username"dodaj tę właściwość.
<EditText
android:id="@+id/edtUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="username"/>
Jeśli chcesz programowo ustawić tekst w Javie
EditText edtUsername = findViewById(R.id.edtUsername);
edtUsername.setText("username");
i kotlintak samo jak java przy użyciu getter / setter
edtUsername.setText("username")
Ale jeśli chcesz korzystać .textz zasady, to
edtUsername.text = Editable.Factory.getInstance().newEditable("username")
ponieważ EditText.textwymaga editablena początku nieString