android 焦点控制及运用

http://gundumw100.iteye.com/blog/1779247

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[]数组中。
看代码:
Java代码 复制代码  收藏代码
  1. package com.reyo.view;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.drawable.BitmapDrawable;  
  5. import android.text.Editable;  
  6. import android.text.TextWatcher;  
  7. import android.view.View;  
  8. import android.widget.EditText;  
  9. import android.widget.ImageButton;  
  10. import android.widget.LinearLayout.LayoutParams;  
  11. import android.widget.PopupWindow;  
  12.   
  13. import com.reyo.merchant2.R;  
  14.   
  15. public class PasswordPopupWindow extends PopupWindow {  
  16.   
  17.     private Context context;  
  18.     private EditText[] texts;  
  19.     private ImageButton btn_close;  
  20.   
  21.     public PasswordPopupWindow(Context context, View view) {  
  22.         super(view, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true);  
  23.         this.context = context;  
  24.         this.setBackgroundDrawable(new BitmapDrawable());// 响应返回键,响应触摸周边消失  
  25.         this.setAnimationStyle(R.style.PopupAnimationFromTop);  
  26.         this.setInputMethodMode(PopupWindow.INPUT_METHOD_FROM_FOCUSABLE);  
  27.         texts = new EditText[4];  
  28.         texts[0] = (EditText) view.findViewById(R.id.et_0);  
  29.         texts[1] = (EditText) view.findViewById(R.id.et_1);  
  30.         texts[2] = (EditText) view.findViewById(R.id.et_2);  
  31.         texts[3] = (EditText) view.findViewById(R.id.et_3);  
  32.         for (int i = 0; i < texts.length; i++) {  
  33.             final int curIndex = i;  
  34.             texts[i].addTextChangedListener(new TextWatcher() {  
  35.   
  36.                 @Override  
  37.                 public void onTextChanged(CharSequence s, int start,  
  38.                         int before, int count) {  
  39.                     // TODO Auto-generated method stub  
  40.   
  41.                 }  
  42.   
  43.                 @Override  
  44.                 public void beforeTextChanged(CharSequence s, int start,  
  45.                         int count, int after) {  
  46.                     // TODO Auto-generated method stub  
  47.   
  48.                 }  
  49.   
  50.                 @Override  
  51.                 public void afterTextChanged(Editable s) {  
  52.                     // TODO Auto-generated method stub  
  53.                     int nextIndex = curIndex + 1;  
  54.                     //当输入到最后一个EditText时关闭PopupWindow  
  55.                     if (nextIndex >= texts.length) {  
  56.                         dismiss();  
  57.                         return;  
  58.                     }  
  59.                     texts[nextIndex].requestFocus();  
  60.                 }  
  61.             });  
  62.         }  
  63.   
  64.         btn_close = (ImageButton) view.findViewById(R.id.btn_close);  
  65.         btn_close.setOnClickListener(new View.OnClickListener() {  
  66.   
  67.             public void onClick(View v) {  
  68.                 // TODO Auto-generated method stub  
  69.                 dismiss();  
  70.             }  
  71.         });  
  72.   
  73.         this.setOnDismissListener(onDismissListener);  
  74.   
  75.     }  
  76.   
  77.     private OnDismissListener onDismissListener = new OnDismissListener() {  
  78.   
  79.         public void onDismiss() {  
  80.             // TODO Auto-generated method stub  
  81.             if (onCompleteListener != null) {  
  82.                 String[] text = new String[texts.length];  
  83.                 for (int i = 0; i < texts.length; i++) {  
  84.                     text[i] = texts[i].getText().toString();  
  85.                 }  
  86.                 onCompleteListener.onComplete(text);  
  87.             }  
  88.             // 清空&归位  
  89.             for (int i = 0; i < texts.length; i++) {  
  90.                 texts[i].setText("");  
  91.             }  
  92.             texts[0].requestFocus();  
  93.         }  
  94.   
  95.     };  
  96.   
  97.     private OnCompleteListener onCompleteListener;  
  98.   
  99.     public void setOnCompleteListener(OnCompleteListener onCompleteListener) {  
  100.         this.onCompleteListener = onCompleteListener;  
  101.     }  
  102.   
  103.     public interface OnCompleteListener {  
  104.         public void onComplete(String[] texts);  
  105.     }  
  106.   
  107. }  
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中的用法就简单了:
Java代码 复制代码  收藏代码
  1. private PasswordPopupWindow popupWindow;  
  2.   
  3. if (popupWindow == null) {  
  4. LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
  5. View popup_view = mLayoutInflater.inflate(R.layout.popup_window_password,null);  
  6. popupWindow = new PasswordPopupWindow(context, popup_view);  
  7. //                  popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_FROM_FOCUSABLE);  
  8.                     popupWindow.showAtLocation(container, Gravity.TOP, 00);  
  9.                     popupWindow.setOnCompleteListener(new PasswordPopupWindow.OnCompleteListener() {  
  10.   
  11.                                 @Override  
  12.                                 public void onComplete(String[] texts) {  
  13.                                     // TODO Auto-generated method stub  
  14.                                     StringBuffer sb = new StringBuffer();  
  15.                                     for (int i = 0; i < texts.length; i++) {  
  16.                                         sb.append(texts[i]);  
  17.                                     }  
  18.                                     String p=sb.toString();  
  19.                                     if(p.length()==texts.length){  
  20. //doSomethingYouWant();  
  21.                                                                         }  
  22.                                 }  
  23.                             });  
  24.                 } else {  
  25.                     if (!popupWindow.isShowing()) {  
  26.                         popupWindow.showAtLocation(container, Gravity.TOP, 00);  
  27.                     }  
  28.                 }  
  29.                 // 强制显示输入法  
  30.                 toggleSoftInput(context);  
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后没有弹出输入法,则强制显示输入法:
Java代码 复制代码  收藏代码
  1. /** 
  2.      * 如果输入法打开则关闭,如果没打开则打开 
  3.      *  
  4.      * @param context 
  5.      */  
  6.     protected void toggleSoftInput(Context context) {  
  7.         InputMethodManager inputMethodManager = (InputMethodManager) context  
  8.                 .getSystemService(Context.INPUT_METHOD_SERVICE);  
  9.         inputMethodManager.toggleSoftInput(0,  
  10.                 InputMethodManager.HIDE_NOT_ALWAYS);  
  11.     }  
/**
	 * 如果输入法打开则关闭,如果没打开则打开
	 * 
	 * @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如下:
Xml代码 复制代码  收藏代码
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:gravity="center_vertical"  
  5.     android:orientation="vertical"   
  6.     android:background="@color/gray1"  
  7.     >  
  8.     <ImageButton  
  9.         android:id="@+id/btn_close"  
  10.         android:layout_width="60dp"  
  11.         android:layout_height="60dp"  
  12.         android:background="@android:color/transparent"  
  13.         android:src="@drawable/bg_btn_close"  
  14.         android:scaleType="center"  
  15.         android:layout_gravity="top|right"  
  16.         />  
  17.       
  18.           
  19.       
  20.     <LinearLayout   
  21.         android:layout_width="match_parent"  
  22.         android:layout_height="wrap_content"   
  23.         android:orientation="horizontal"  
  24.         android:gravity="center"  
  25.         >  
  26.         <EditText   
  27.             android:id="@+id/et_0"  
  28.             android:layout_width="wrap_content"  
  29.             android:layout_height="wrap_content"   
  30.             android:inputType="textPassword"  
  31.             android:singleLine="true"  
  32.             android:minWidth="60dp"  
  33.             android:minHeight="60dp"  
  34.             android:maxLength="1"  
  35.             android:layout_marginLeft="10dp"  
  36.             android:layout_marginRight="10dp"  
  37.             android:gravity="center"  
  38.             android:textSize="@dimen/font_xxxbig"  
  39.             />  
  40.         <EditText   
  41.             android:id="@+id/et_1"  
  42.             android:layout_width="wrap_content"  
  43.             android:layout_height="wrap_content"   
  44.             android:inputType="textPassword"  
  45.             android:singleLine="true"  
  46.             android:minWidth="60dp"  
  47.             android:minHeight="60dp"  
  48.             android:maxLength="1"  
  49.             android:layout_marginLeft="10dp"  
  50.             android:layout_marginRight="10dp"  
  51.             android:gravity="center"  
  52.             android:textSize="@dimen/font_xxxbig"  
  53.             />  
  54.         <EditText   
  55.             android:id="@+id/et_2"  
  56.             android:layout_width="wrap_content"  
  57.             android:layout_height="wrap_content"   
  58.             android:inputType="textPassword"  
  59.             android:singleLine="true"  
  60.             android:minWidth="60dp"  
  61.             android:minHeight="60dp"  
  62.             android:maxLength="1"  
  63.             android:layout_marginLeft="10dp"  
  64.             android:layout_marginRight="10dp"  
  65.             android:gravity="center"  
  66.             android:textSize="@dimen/font_xxxbig"  
  67.             />  
  68.         <EditText   
  69.             android:id="@+id/et_3"  
  70.             android:layout_width="wrap_content"  
  71.             android:layout_height="wrap_content"   
  72.             android:inputType="textPassword"  
  73.             android:singleLine="true"  
  74.             android:minWidth="60dp"  
  75.             android:minHeight="60dp"  
  76.             android:maxLength="1"  
  77.             android:layout_marginLeft="10dp"  
  78.             android:layout_marginRight="10dp"  
  79.             android:gravity="center"  
  80.             android:textSize="@dimen/font_xxxbig"  
  81.             />  
  82.     </LinearLayout>  
  83.     <TextView  
  84.             android:layout_width="wrap_content"  
  85.             android:layout_height="wrap_content"  
  86.             android:text="请输入操作密码(该操作由服务员完成)"  
  87.             android:textColor="@color/white"  
  88.             android:textSize="@dimen/font_middle"  
  89.             android:singleLine="true"  
  90.             android:layout_margin="20dp"  
  91.             android:layout_gravity="center_horizontal"  
  92.             />  
  93. </LinearLayout>  


动画文件:
Xml代码 复制代码  收藏代码
  1. <style name="PopupAnimationFromTop" parent="android:Animation"  mce_bogus="1" >  
  2.         <item name="android:windowEnterAnimation">@anim/anim_top_in</item>  
  3.         <item name="android:windowExitAnimation">@anim/anim_top_out</item>  
  4.     </style>  


anim_top_in.xml
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <translate   
  4.     android:fromYDelta="-100%p"   
  5.     android:toYDelta="0"   
  6.     android:duration="200"   
  7.     android:fillAfter="true"  
  8.     android:interpolator="@android:anim/bounce_interpolator"  
  9.     />  
  10. </set>  

anim_top_out.xml
Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <translate   
  4.     android:fromYDelta="0"   
  5.     android:toYDelta="-100%p"   
  6.     android:duration="200"  
  7.     android:fillAfter="true"  
  8.     android:interpolator="@android:anim/bounce_interpolator"  
  9.     />  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值