简体   繁体   English

如何显示 EditText 中的文本?

[英]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?从 EditText 获取文本,但如何让它与 TextView 一起显示? Also, how will the code change if the TextView is in a different Activity?另外,如果 TextView 位于不同的 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:但是我在这一行收到错误“未定义字符串类型的方法 setText(String)”:

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); 而不是渲染你然后做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. 要做到这一点,你需要从编辑视图中获取文本并设置文本视图:这是使用预定义的getter和setter的小菜一碟。

You need to inflate the text view as you have done for EditText, then use the .setText method 您需要像对EditText一样对文本视图进行膨胀,然后使用.setText方法

If the textview is in a new activity you need to send over the info in the intent for example: 如果textview在新活动中,则需要通过意图中的信息发送,例如:

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 中创建显示文本

<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: 回答问题1:

Use TextView.setText(CharSequence text, TextView.BufferType type) to make your text view display the text you want. 使用TextView.setText(CharSequence text, TextView.BufferType type)使文本视图显示所需的文本。

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. 目前你正在将字符串设置为另一个字符串,并且没有像string的settext那样可用的函数。 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. 此外,如果您希望在运行中进行此操作(当用户放入文本时更新textview),请使用文本观察器。

define an event handler for the edittext, whenever its text changes extract it and do display.setText() 为edittext定义一个事件处理程序,每当它的文本发生更改时提取它并执行display.setText()

I hope that helps 我希望有所帮助

how do I get it to display with a TextView? 如何使用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): 下面是在键盘上按下输入键时更新显示 (示例中使用的TextView )和输入内容(在您的示例中使用的EditText )的代码(我看到您在对另一个答案的注释中请求了这个):

    // 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. 上面的代码在单个Activity中。

Also, how will the code change if the TextView is in a different Activity? 此外,如果TextView位于不同的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: 如果显示 (示例中的TextView )在另一个Activity中,那么更新它的一种快捷方法是将输入值(示例中的EditText )存储为SharedPreference,并在值中显示 Activity的onResume

// 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 您应该使用代码常量替换我的代码中的所有硬编码字符串文字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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