简体   繁体   English

如何在 TextView 中显示 EditText 字段文本

[英]How to display the EditText fields text in the TextView

Of late I was just trying to create a mems geneator app, yes I had been following the new bostons and in that buckey created them using 2 fragments and here i just want to do in a single main activity, but I just can't figure out how to retrieve the text from the edit text field and set the text of Text view to it.最近我只是想创建一个 mems 生成器应用程序,是的,我一直在关注新波士顿,在那个 buckey 中使用 2 个片段创建了它们,在这里我只想在一个主要活动中做,但我就是想不通了解如何从编辑文本字段中检索文本并将文本视图的文本设置为它。 I know it's pretty a newbie question but still I don't know how to code it so please help...我知道这是一个新手问题,但我仍然不知道如何编码,所以请帮助...

I had just imported some widgets,views,etc and done some modified the on create function and my on create function is:我刚刚导入了一些小部件、视图等,并对 on create 函数进行了一些修改,我的 on create 函数是:

public class MainActivity extends AppCompatActivity {

public static EditText topText;
public static EditText bottomtext;
public static TextView top;
public static TextView bottom;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    topText = (EditText) findViewById(R.id.topedit);
    bottomtext = (EditText) findViewById(R.id.bottomedit);
    top = (TextView) findViewById(R.id.top);
    bottom = (TextView) findViewById(R.id.bottom);
    Button myButton = (Button) findViewById(R.id.button);
    myButton.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View V) {
                    top.setText((CharSequence) topText);
                    bottom.setText((CharSequence) bottomtext);
                }
            }
    );
}

Simply do this:只需这样做:

if(topText.getText()!=null){
top.setText(topText.getText().toString());
}
if(bottomtext.getText()!=null){
bottom.setText(bottomtext.getText().toString());
}

Try this to get text of the EditText field:试试这个来获取 EditText 字段的文本:

CharSequence text = topText.getText();

And set the text above for the textView:并为 textView 设置上面的文本:

top.setText(text);

Use this short example to understand the concept使用这个简短的例子来理解这个概念

store=ret.getText().toString(); store=ret.getText().toString();

show.setText(store); show.setText(store);

Explanation解释

ret: is the name of the edit text field; ret:是编辑文本字段的名称;

store: is used to hold anything gotten from ret (text field) store: 用于保存从 ret (text field) 得到的任何东西

show.setText(store) displays the result in the textview show.setText(store) 在 textview 中显示结果

This question has already been answered:这个问题已经回答了:

"Use getText() on your EditText object". “在 EditText 对象上使用 getText()”。

Get Value of a Edit Text field 获取编辑文本字段的值

Next time do a quick search ;)下次做一个快速搜索;)

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

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