HandleActivity.java
public class HandleActivity extends FragmentActivity {
private Context context;// 上下文
private TextView textView;
private EditText edt;
private String string;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 设置吧显示标题栏--需要在创建视图之前设置
setContentView(R.layout.activity_handle);
context = this;// 上下文赋值
Init();
}
private void Init() {
textView = (TextView) this.findViewById(R.id.txt);
edt = (EditText) this.findViewById(R.id.editText1);
textView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
Message msg = new Message();
string = edt.getText().toString();
//msg.obj = string;
msg.what = 1;//标记
handel.sendMessage(msg);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
});
}
// 更新UI--内部类
private Handler handel = new Handler() {
public void handleMessage(Message msg) {
// super.handleMessage(msg);
switch (msg.what) {
case 1:
Toast.makeText(context, string, 0).show();
break;
default:
break;
}
}
};
}
activity_handle.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HandleActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:layout_marginTop="100dp"
android:gravity="center"
android:text="请输入内容后点击" />
</RelativeLayout>