setFocusable() 设置view接受焦点的资格
isFocusable() view是否具有接受焦点的资格
setFocusInTouchMode() 对应在触摸模式下,设置是否有焦点来响应点触的资格
isFocusableInTouchMode() 对应在触摸模式下,view是否具有焦点的资格
强制view焦点获取,注意:这些方法都不会触发事件(onTouch,onClick等),想要触发onClick事件请调用view.performClick()
requestFocus() ------ view
requestFocus(int direction)当用户在某个界面聚集焦点,参数为下面的4个
requestFocusFromTouch() 触摸模式下
......
requestChildFocus (View child, View focused) ------viewGroup
1 父元素调用此方法
2 child 将要获取焦点的子元素
3 focused 现在拥有焦点的子元素
一般也可以通过 配置文件设置
View.FOCUS_LEFT Move focus to the left
View.FOCUS_UP Move focus up
View.FOCUS_RIGHT Move focus to the right
View.FOCUS_DOWN Move focus down
代码设置实现 其实都是通过这些设置的
isInTouchMode() 触摸模式
-------------------------------------------------------------------------------
下面的例子主要使用了requestFocus()方法使焦点在各个控件之间切换。
看下图:
最上面的弹出框是个PopupWindow,需要依次输入4个密码,为了方便快捷,当上一个文本框输入值之后,焦点自动切换到下一个文本框,当输入到最后一个文本框后,PopupWindow自动关闭,并返回4个文本框中的值,放在String[]数组中。
看代码:
package com.reyo.view;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import com.reyo.merchant2.R;
public class PasswordPopupWindow extends PopupWindow {
private Context context;
private EditText[] texts;
private ImageButton btn_close;
public PasswordPopupWindow(Context context, View view) {
super(view, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true);
this.context = context;
this.setBackgroundDrawable(new BitmapDrawable());// 响应返回键,响应触摸周边消失
this.setAnimationStyle(R.style.PopupAnimationFromTop);
this.setInputMethodMode(PopupWindow.INPUT_METHOD_FROM_FOCUSABLE);
texts = new EditText[4];
texts[0] = (EditText) view.findViewById(R.id.et_0);
texts[1] = (EditText) view.findViewById(R.id.et_1);
texts[2] = (EditText) view.findViewById(R.id.et_2);
texts[3] = (EditText) view.findViewById(R.id.et_3);
for (int i = 0; i < texts.length; i++) {
final int curIndex = i;
texts[i].addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
int nextIndex = curIndex + 1;
//当输入到最后一个EditText时关闭PopupWindow
if (nextIndex >= texts.length) {
dismiss();
return;
}
texts[nextIndex].requestFocus();
}
});
}
btn_close = (ImageButton) view.findViewById(R.id.btn_close);
btn_close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
dismiss();
}
});
this.setOnDismissListener(onDismissListener);
}
private OnDismissListener onDismissListener = new OnDismissListener() {
public void onDismiss() {
// TODO Auto-generated method stub
if (onCompleteListener != null) {
String[] text = new String[texts.length];
for (int i = 0; i < texts.length; i++) {
text[i] = texts[i].getText().toString();
}
onCompleteListener.onComplete(text);
}
// 清空&归位
for (int i = 0; i < texts.length; i++) {
texts[i].setText("");
}
texts[0].requestFocus();
}
};
private OnCompleteListener onCompleteListener;
public void setOnCompleteListener(OnCompleteListener onCompleteListener) {
this.onCompleteListener = onCompleteListener;
}
public interface OnCompleteListener {
public void onComplete(String[] texts);
}
}
在Activity中的用法就简单了:
private PasswordPopupWindow popupWindow;
if (popupWindow == null) {
LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View popup_view = mLayoutInflater.inflate(R.layout.popup_window_password,null);
popupWindow = new PasswordPopupWindow(context, popup_view);
// popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_FROM_FOCUSABLE);
popupWindow.showAtLocation(container, Gravity.TOP, 0, 0);
popupWindow.setOnCompleteListener(new PasswordPopupWindow.OnCompleteListener() {
@Override
public void onComplete(String[] texts) {
// TODO Auto-generated method stub
StringBuffer sb = new StringBuffer();
for (int i = 0; i < texts.length; i++) {
sb.append(texts[i]);
}
String p=sb.toString();
if(p.length()==texts.length){
//doSomethingYouWant();
}
}
});
} else {
if (!popupWindow.isShowing()) {
popupWindow.showAtLocation(container, Gravity.TOP, 0, 0);
}
}
// 强制显示输入法
toggleSoftInput(context);
如果弹出PasswordPopupWindow后没有弹出输入法,则强制显示输入法:
/**
* 如果输入法打开则关闭,如果没打开则打开
*
* @param context
*/
protected void toggleSoftInput(Context context) {
InputMethodManager inputMethodManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(0,
InputMethodManager.HIDE_NOT_ALWAYS);
}
PasswordPopupWindow的布局文件popup_window_password.xml如下:
动画文件:
anim_top_in.xml
anim_top_out.xml
![点击查看原始大小图片 android 焦点控制及运用_第2张图片](http://img.e-com-net.com/image/info8/dd9733636bcc423788e8ca815e3709a8.jpg)
- 大小: 142.2 KB