简体   繁体   中英

Show String from EditText

I want to show TextView editable like the app "Google Keep" but

  EditText text = (EditText)findViewById(R.id.edittext);
  String value = text.getText().toString();

didn't work

Hello I've made a example that I think you can use:

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:orientation="vertical"
>

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:textColor="#FF0000"
    android:text="@string/hello_world" />

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/getInfoButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="GET INFO" 
    android:layout_gravity="center_horizontal"
    />

</LinearLayout>

MainActivity

package com.example.testedittext;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

private TextView info;
private EditText input;
private Button getInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    info = (TextView) findViewById(R.id.textView);
    input = (EditText)findViewById(R.id.editText);
    getInfo = (Button)findViewById(R.id.getInfoButton);
    getInfo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String inputText = input.getText().toString();
            info.setText(inputText);
        }
    });

}


}

Here is the output:

编辑文字范例

Hope this helps, Cheers

That is the correct syntax for getting the text in string format of an edittext.

The value of String "value" is actually "" right now because you called

text.getText().toString(); 

IMMEDIATELY after EditText text was instantiated. As you can imagine, the moment it was created, there was no text inside it, so that's why "value" has an empty string.

If you want to retrieve the value of the edittext at a specific call, I'd recommend adding a button in your xml layout, and in your code, add this:

Button button = (Button) findViewById(R.id.somebutton);
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
     text.getText().toString();         
}

});

This will get the current String value of the edittext when you click on the button.

You have to use the value String somewhere else it will tell you that its not used. Then you have to use that piece of code at a point of time where you have had a change to set the value of that ediettext.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM