简体   繁体   中英

How do I display text from an EditText?

Ok, I'm using the code:

EditText editText = (EditText) findViewById(R.id.editText1);
String value = editText.getText().toString();

to get text from an EditText, but how do I get it to display with a TextView? Also, how will the code change if the TextView is in a different Activity?

Like this:

在此处输入图片说明

Well, I tried doing this:

    EditText input = (EditText) findViewById(R.id.editText1);
    String thing = input.getText().toString();

    TextView display = (TextView) findViewById(R.id.textView1);
    String text = display.getText().toString();

    text.setText(thing);

But I get an error "The method setText(String) is undefined for the type String" on this line:

text.setText(thing);

You can eg install a text change listener

See eg here for how to add it to the edit text . Instead of rendering you would then do textField.setText(text);

To do this you need to get the text from the edit view and set the text view: This is a piece of cake using the predefined getters and setters.

You need to inflate the text view as you have done for EditText, then use the .setText method

If the textview is in a new activity you need to send over the info in the intent for example:

startActivity(new Intent(AndroidActivity.this, Activity2.class).putExtra("user", user.getText().toString()));

Then in your new acitivty it can be retrieved as

String s = getIntent().getStringExtra("user")

you can use this code for create display text in EditText

<EditText
    android:text="Your name"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="15dp"
    android:layout_width="380dp"
    android:layout_height="wrap_content"/>

result EditText

Answer to question 1:

Use TextView.setText(CharSequence text, TextView.BufferType type) to make your text view display the text you want.

for resolving this particular error use

display.setText(thing);

currently you are setting the string to another string and also there is no such function available as settext for string. Also if you want this to happen on the fly(update textview when user is putting in the text) use a text watcher for that.

define an event handler for the edittext, whenever its text changes extract it and do display.setText()

I hope that helps

how do I get it to display with a TextView?

Here is code which will update display (the TextView used in your example) with the contents of input (the EditText used in your example) whenever enter key is pressed on the keyboard (I saw you requested this in a comment to another Answer):

    // get reference to the TextView
    final TextView display = (TextView) findViewById(R.id.textView1);

    // get reference to EditText and set listener to update TextView
    final EditText input = (EditText) findViewById(R.id.editText1);
    input.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_ENTER)) {
                  display.setText(input.getText());
                  return true;
                }
                return false;
        }
    });

Code above is inside of a single Activity.

Also, how will the code change if the TextView is in a different Activity?

If display (the TextView in your example) is in another Activity, then one quick way to update it is to store the value of input (the EditText in your example) as a SharedPreference and have display Activity's onResume read in the value:

// Activity with display
public class DisplayActivity extends Activity {
    private TextView display;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // get reference to the TextView
        display = (TextView) findViewById(R.id.textView1);
    }

    public void onResume() {
        super.onResume();
        String inputsValue = getSharedPreferences("myprefs", 0)
                .getString("input", "" /** Default value*/);

        display.setText(inputsValue);
    }
}

// Activity with input
public class InputActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // get reference to EditText and store in SharedPreference
        final EditText input = (EditText) findViewById(R.id.editText1);
        input.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                        (keyCode == KeyEvent.KEYCODE_ENTER)) {
                      getSharedPreferences("myprefs", 0).edit()
                          .putString("input", input.getText().toString()).commit();
                      return true;
                }
                return false;
            }
        });
    }
}

You should replace all hardcoded string literals in my code with a code constant

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