How To Set Text In An EditText

2019-01-05 00:27发布

How can I set the text of an EditText?

10条回答
放荡不羁爱自由
2楼-- · 2019-01-05 00:53

You need to:

  1. Declare the EditText in the xml file
  2. Find the EditText in the activity
  3. Set the text in the EditText
查看更多
成全新的幸福
3楼-- · 2019-01-05 00:55
String string="this is a text";
editText.setText(string)

I have found String to be a useful Indirect Subclass of CharSequence

http://developer.android.com/reference/android/widget/TextView.html find setText(CharSequence text)

http://developer.android.com/reference/java/lang/CharSequence.html

查看更多
地球回转人心会变
4楼-- · 2019-01-05 00:56

Use +, the string concatenation operator:

 ed = (EditText) findViewById (R.id.box);
    int x = 10;
    ed.setText(""+x);

or use

String.valueOf(int):
ed.setText(String.valueOf(x));

or use

Integer.toString(int):
ed.setText(Integer.toString(x));
查看更多
▲ chillily
6楼-- · 2019-01-05 01:02

You can set android:text="your text";

<EditText
    android:id="@+id/editTextName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/intro_name"/>
查看更多
虎瘦雄心在
7楼-- · 2019-01-05 01:06

This is the solution in Kotlin

val editText: EditText = findViewById(R.id.main_et_name)
    editText.setText("This is a text.")
查看更多
登录 后发表回答