development

EditText에서 텍스트를 설정하는 방법

big-blog 2020. 8. 3. 17:18
반응형

EditText에서 텍스트를 설정하는 방법


EditText의 텍스트를 어떻게 설정합니까?


당신이 문서를 확인하면 EditText, 당신은 찾을 setText()방법을. a String와 a를 TextView.BufferType받습니다. 예를 들면 다음과 같습니다.

EditText editText = (EditText)findViewById(R.id.edit_text);
editText.setText("Google is your friend.", TextView.BufferType.EDITABLE);

또한 TextViewsetText(CharSequence)setText(int)메소드를 상속 하므로 일반처럼 설정 할 수 있습니다 TextView.

editText.setText("Hello world!");
editText.setText(R.string.hello_world);

String string="this is a text";
editText.setText(string)

String이 CharSequence의 유용한 간접 서브 클래스라는 것을 알았습니다.

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

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


String text = "Example";
EditText edtText = (EditText) findViewById(R.id.edtText);
edtText.setText(text);

EditText필요한 경우 문자열 값만 승인 하십시오 . 문자열로 변환하십시오.

int, double, long 값이면 다음을 수행하십시오.

String.value(value);

문자열 연결 연산자 +를 사용하십시오.

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

또는 사용

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

또는 사용

Integer.toString(int):
ed.setText(Integer.toString(x));

당신은 설정할 수 있습니다 android:text="your text";

<EditText
    android:id="@+id/editTextName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/intro_name"/>

editTextObject.setText(CharSequence)

http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence )


다음을 수행해야합니다.

  1. 선언 EditText in the xml file
  2. EditText활동에서 찾기
  3. 텍스트를 EditText

이것이 Kotlin의 솔루션입니다

val editText: EditText = findViewById(R.id.main_et_name)
    editText.setText("This is a text.")

Android Java의 솔루션 :

  1. EditText를 시작하십시오 .ID는 xml id에 있습니다.

    EditText myText = (EditText)findViewById(R.id.my_text_id);
    
  2. OnCreate 메소드에서 정의 된 이름으로 텍스트를 설정하십시오.

    String text = "here put the text that you want"
    
  3. editText에서 setText 메소드를 사용하십시오.

    myText.setText(text); //variable from point 2
    

참고 URL : https://stackoverflow.com/questions/4590957/how-to-set-text-in-an-edittext

반응형