之前写过一篇博客,简单的介绍了Android 隐藏EditText的焦点,之所以要隐藏EditText的焦点,是因为当应用在第一次进入某个Activity时,由于该页面中的EditText获取了焦点,致使键盘弹出,而影响了用户的体验。
今天扩展下之前的内容,简单介绍如何动态设置某个控件获取到焦点。
首先,定义页面布局:
<LinearLayout 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" android:orientation="vertical" > <LinearLayout android:id="@+id/edit_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="true" android:focusableInTouchMode="true" android:orientation="vertical" > <EditText android:id="@+id/edit_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="姓名" android:inputType="text" android:minHeight="50dp" /> <EditText android:id="@+id/edit_mobile" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="手机号" android:inputType="phone" android:minHeight="50dp" /> </LinearLayout> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="确定" android:onClick="setFocus" /> </LinearLayout>观察下,我已经对两个EditText的父容器LinearLayout设置了两行代码android:focusable="true",android:focusableInTouchMode="true"。有了这两行代码,当我们进入该Activity时,便不会弹出软键盘啦。
下面,我们对两个文本输入框定义焦点事件:
private class OnFocusHandler implements OnFocusChangeListener { @Override public void onFocusChange(View v, boolean hasFocus) { switch (v.getId()) { case R.id.edit_name: if (hasFocus) { edit_name.setText("ning shuai"); } else { edit_name.setText(""); } break; case R.id.edit_mobile: if (hasFocus) { edit_mobile.setText("963258741"); } else { edit_mobile.setText(""); } break; } } }焦点事件很容易理解,不多讲了。最后,我们定义Button的点击事件,在这个事件中,我们让两个EditText的父容器LinearLayout动态获取焦点。效果是,当点击Button时,两个EditText输入框中的内容同时清空。
public void setFocus(View view) { edit_layout.setFocusable(true); edit_layout.setFocusableInTouchMode(true); edit_layout.requestFocus(); edit_layout.requestFocusFromTouch(); }
注意下, edit_layout.requestFocus();edit_layout.requestFocusFromTouch();这两行代码是必须添加的,不然LinearLayout是无法获取到焦点的!
好了,就是如此简单的内容,把整个Activity的代码给大家看下,就不上传源码了。
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnFocusChangeListener; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private LinearLayout edit_layout; private EditText edit_name; private EditText edit_mobile; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edit_layout = (LinearLayout) findViewById(R.id.edit_layout); edit_name = (EditText) findViewById(R.id.edit_name); edit_mobile = (EditText) findViewById(R.id.edit_mobile); this.edit_name.setOnFocusChangeListener(new OnFocusHandler()); this.edit_mobile.setOnFocusChangeListener(new OnFocusHandler()); } public void setFocus(View view) { edit_layout.setFocusable(true); edit_layout.setFocusableInTouchMode(true); edit_layout.requestFocus(); edit_layout.requestFocusFromTouch(); } private class OnFocusHandler implements OnFocusChangeListener { @Override public void onFocusChange(View v, boolean hasFocus) { switch (v.getId()) { case R.id.edit_name: if (hasFocus) { edit_name.setText("ning shuai"); } else { edit_name.setText(""); } break; case R.id.edit_mobile: if (hasFocus) { edit_mobile.setText("963258741"); } else { edit_mobile.setText(""); } break; } } } }
看下最后的效果: