main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <EditText android:id="@+id/edit" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
package org.lxh.demo; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnFocusChangeListener; import android.view.View.OnKeyListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Hello extends Activity { <span style="white-space:pre"> </span>private TextView txt=null; <span style="white-space:pre"> </span>private EditText edit=null; <span style="white-space:pre"> </span>public void onCreate(Bundle savedInstanceState) { <span style="white-space:pre"> </span>super.onCreate(savedInstanceState); // 生命周期方法 <span style="white-space:pre"> </span>super.setContentView(R.layout.main); // 设置要使用的布局管理器 <span style="white-space:pre"> </span>this.edit=(EditText)super.findViewById(R.id.edit); <span style="white-space:pre"> </span>this.txt=(TextView)super.findViewById(R.id.text); <span style="white-space:pre"> </span>this.edit.setOnKeyListener(new OnKeyListenerImpl()); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>private class OnKeyListenerImpl implements OnKeyListener{ <span style="white-space:pre"> </span>public boolean onKey(View arg0, int arg1, KeyEvent arg2) { <span style="white-space:pre"> </span>String str=Hello.this.edit.getText().toString(); <span style="white-space:pre"> </span>Hello.this.txt.setText(str); <span style="white-space:pre"> </span>return false; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>} }
运行如下:
原文地址:http://blog.csdn.net/yayun0516/article/details/44084903