Android学习之焦点事件
MyFocusDemo.java:
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnFocusChangeListener; import android.widget.EditText; import android.widget.TextView; public class MyFocusDemo extends Activity { private EditText edit = null; // 在此组件上设置焦点事件 private TextView txt = null; // 用于信息提示 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); this.edit = (EditText) super.findViewById(R.id.edit); // 取得组件 this.txt = (TextView) super.findViewById(R.id.txt); // 取得组件 this.edit.setOnClickListener(new OnClickListenerImpl()); // 设置单击事件 this.edit.setOnFocusChangeListener(new OnFocusChangeListenerImpl()); } private class OnClickListenerImpl implements OnClickListener { public void onClick(View v) { MyFocusDemo.this.edit.setText(""); // 清空文本 } } private class OnFocusChangeListenerImpl implements OnFocusChangeListener { public void onFocusChange(View v, boolean hasFocus) { // 表示操作的组件,而hasFocus表示是否获得焦点 if (hasFocus) { // 已经获得了焦点 MyFocusDemo.this.txt.setText("文本输入组件获得焦点。"); } else { if (MyFocusDemo.this.edit.getText().length() > 0) { // 现在有数据 MyFocusDemo.this.txt.setText("文本输入组件失去焦点,输入内容合法。"); } else { MyFocusDemo.this.txt.setText("文本输入组件失去焦点,输入内容不能为空。"); } } } } }
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:id="@+id/edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请输入查询内容"/> <EditText android:id="@+id/msg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="www.mldnjava.cn"/> <TextView android:id="@+id/txt" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28