如何在TextView中显示EditText中的用户输入字符串?

时间:2015-07-16 11:36:06

标签: android

我是Android编程新手。我一直在收到错误,无法找到任何显示输入字符串的解决方案。

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

    final EditText namex = (EditText) findViewById(R.id.username);
    namex.getText().toString();

    final EditText passwordx = (EditText)findViewById(R.id.password);
    passwordx.getText().toString();


    Button button = (Button)findViewById(R.id.signin);
    button.setOnClickListener(
        new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView name = (TextView)findViewById(R.id.Dispname);
                name.setText(namex);
                TextView password = (TextView)findViewById(R.id.Disppassword);
                password.setText(passwordx);
            }
        }

    );


}

非常感谢。提前谢谢!

2 个答案:

答案 0 :(得分:0)

您正在做的是传递EditText数据类型参数而不是String,所以,试试这个

name.setText(namex.getText().toString());
password.setText(passwordx.getText().toString());

答案 1 :(得分:0)

更改以下代码,您将EditText的引用设置为TextView

的文字
 Button button = (Button)findViewById(R.id.signin);
    button.setOnClickListener(
        new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView name = (TextView)findViewById(R.id.Dispname);
                name.setText(namex.getText().toString());
                TextView password = (TextView)findViewById(R.id.Disppassword);
                password.setText(passwordx.getText().toString());
            }
        }

    );