xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.helloword.MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#000000"
android:id="@+id/textView"
android:gravity="center"/>
</LinearLayout>
MainActivity源码
package com.example.administrator.helloword;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText ed;
private TextView showResult_tv;
private String getResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed = (EditText)findViewById(R.id.editText);
showResult_tv = (TextView)findViewById(R.id.textView);
showResult_tv.setHint("没有内容");
//设置文本改变监听
ed.addTextChangedListener(tw);
}
//文本改变监听器
TextWatcher tw = new TextWatcher() {
//文字改变前的监听
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
//文本改变时的监听
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (ed.getText().toString() == null || ed.getText().toString() == "") {
showResult_tv.setText("");
} else {
showResult_tv.setText(ed.getText().toString());
}
}
//文字改变后的监听
@Override
public void afterTextChanged(Editable editable) {
}
};
}