package com.example.activity_edittext;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView mTextView1,mTextView2;
private EditText mEditText1,mEditText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取还需要放在下面的;
mTextView1 = (TextView)findViewById(R.id.mTextView1);
mTextView2 = (TextView)findViewById(R.id.mTextView2);
mEditText1 = (EditText)findViewById(R.id.mEditText1);
mEditText2 = (EditText)findViewById(R.id.mEditText2);
mTextView1.setText("账号内容是:");
mTextView2.setText("密码内容是:");
mEditText1.setHint("请输入账号");
mEditText2.setHint("请输入密码");
mEditText1.setOnKeyListener(new EditText.OnKeyListener()
{
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
//得到文字,将其显示在TextView当中
mTextView1.setText("账号内容是:"+mEditText1.getText().toString());
return false;
}
});
mEditText2.setOnKeyListener(new EditText.OnKeyListener()
{
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
//得到文字,将其显示在TextView当中
mTextView2.setText("密码内容是:"+mEditText2.getText().toString());
return false;
}
});
}
}
当在文本编辑框中输入内容之后,按下回车键就会发现TextView发生了变化。