Làm cách nào tôi có thể đặt văn bản của EditText?
Làm cách nào tôi có thể đặt văn bản của EditText?
Câu trả lời:
Nếu bạn kiểm tra tài liệu cho EditText, bạn sẽ tìm thấy một setText()phương pháp. Nó mất trong một Stringvà một TextView.BufferType. Ví dụ:
EditText editText = (EditText)findViewById(R.id.edit_text);
editText.setText("Google is your friend.", TextView.BufferType.EDITABLE);
Nó cũng kế thừa TextViewcác phương thức setText(CharSequence)và setText(int)phương thức, vì vậy bạn có thể đặt nó giống như thông thường TextView:
editText.setText("Hello world!");
editText.setText(R.string.hello_world);
EditText.BufferType.EDITABLE?
EditTextkéo dài TextView; TextView.BufferType.EDITABLElà hằng số đúng.
setText(CharSequence), từ siêu lớp của nó TextView. Vì vậy, tôi không thực sự chắc chắn tại sao đây là câu trả lời được tán thành và chấp nhận nhất?
String string="this is a text";
editText.setText(string)
Tôi đã tìm thấy String là một lớp con gián tiếp hữu ích của CharSequence
http://developer.android.com/reference/android/widget/TextView.html tìm setText (văn bản CharSequence)
http://developer.android.com/reference/java/lang/CharSequence.html
String text = "Example";
EditText edtText = (EditText) findViewById(R.id.edtText);
edtText.setText(text);
Kiểm tra xem nó EditTextchỉ chấp nhận các giá trị Chuỗi nếu cần thiết chuyển đổi nó thành chuỗi.
Nếu int, double, value value, do:
String.value(value);
Sử dụng +, toán tử nối chuỗi:
ed = (EditText) findViewById (R.id.box);
int x = 10;
ed.setText(""+x);
hoặc dùng
String.valueOf(int):
ed.setText(String.valueOf(x));
hoặc dùng
Integer.toString(int):
ed.setText(Integer.toString(x));
Đây là giải pháp trong Kotlin
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 )
Bạn cần phải:
EditText in the xml file EditTexttrong hoạt độngEditTextGiải pháp trong Android Java:
Bắt đầu EditText của bạn, ID được chuyển đến id xml của bạn.
EditText myText = (EditText)findViewById(R.id.my_text_id);trong Phương thức OnCreate của bạn, chỉ cần đặt văn bản theo tên được xác định.
String text = "here put the text that you want"sử dụng phương thức setText từ editText của bạn.
myText.setText(text); //variable from point 2Nếu bạn muốn đặt văn bản tại thời điểm thiết kế trong xmltệp, chỉ cần android:text="username"thêm thuộc tính này.
<EditText
android:id="@+id/edtUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="username"/>
Nếu bạn muốn đặt văn bản theo chương trình trong Java
EditText edtUsername = findViewById(R.id.edtUsername);
edtUsername.setText("username");
và kotlingiống như java sử dụng getter / setter
edtUsername.setText("username")
Nhưng nếu bạn muốn sử dụng .texttừ nguyên tắc thì
edtUsername.text = Editable.Factory.getInstance().newEditable("username")
bởi vì EditText.textyêu cầu một editablelúc đầu khôngString