日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

android 焦點(diǎn)控制及運(yùn)用

系統(tǒng) 2080 0
setFocusable()?? 設(shè)置view接受焦點(diǎn)的資格???
isFocusable()??? view是否具有接受焦點(diǎn)的資格??

setFocusInTouchMode()????? 對(duì)應(yīng)在觸摸模式下,設(shè)置是否有焦點(diǎn)來(lái)響應(yīng)點(diǎn)觸的資格?????????
isFocusableInTouchMode()? 對(duì)應(yīng)在觸摸模式下,view是否具有焦點(diǎn)的資格

強(qiáng)制view焦點(diǎn)獲取,注意:這些方法都不會(huì)觸發(fā)事件(onTouch,onClick等),想要觸發(fā)onClick事件請(qǐng)調(diào)用view.performClick()
requestFocus()???????????????????????????????? ------ view
requestFocus(int direction)當(dāng)用戶在某個(gè)界面聚集焦點(diǎn),參數(shù)為下面的4個(gè)
requestFocusFromTouch()??? 觸摸模式下
? ......
requestChildFocus (View child, View focused)?? ------viewGroup
1 父元素調(diào)用此方法
2 child? 將要獲取焦點(diǎn)的子元素
3 focused 現(xiàn)在擁有焦點(diǎn)的子元素

一般也可以通過(guò) 配置文件設(shè)置
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????????????
代碼設(shè)置實(shí)現(xiàn) 其實(shí)都是通過(guò)這些設(shè)置的????????

isInTouchMode()??? 觸摸模式

-------------------------------------------------------------------------------
下面的例子主要使用了requestFocus()方法使焦點(diǎn)在各個(gè)控件之間切換。
看下圖:

android 焦點(diǎn)控制及運(yùn)用

最上面的彈出框是個(gè)PopupWindow,需要依次輸入4個(gè)密碼,為了方便快捷,當(dāng)上一個(gè)文本框輸入值之后,焦點(diǎn)自動(dòng)切換到下一個(gè)文本框,當(dāng)輸入到最后一個(gè)文本框后,PopupWindow自動(dòng)關(guān)閉,并返回4個(gè)文本框中的值,放在String[]數(shù)組中。
看代碼:
    
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());// 響應(yīng)返回鍵,響應(yīng)觸摸周邊消失
		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;
					//當(dāng)輸入到最后一個(gè)EditText時(shí)關(guān)閉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中的用法就簡(jiǎn)單了:
    
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);
					}
				}
				// 強(qiáng)制顯示輸入法
				toggleSoftInput(context);

  


如果彈出PasswordPopupWindow后沒有彈出輸入法,則強(qiáng)制顯示輸入法:
    
/**
	 * 如果輸入法打開則關(guān)閉,如果沒打開則打開
	 * 
	 * @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如下:
    
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical" 
    android:background="@color/gray1"
    >
    <ImageButton
        android:id="@+id/btn_close"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@android:color/transparent"
        android:src="@drawable/bg_btn_close"
        android:scaleType="center"
        android:layout_gravity="top|right"
        />
    
	    
	
    <LinearLayout 
        android:layout_width="match_parent"
    	android:layout_height="wrap_content" 
   		android:orientation="horizontal"
   		android:gravity="center"
        >
        <EditText 
            android:id="@+id/et_0"
            android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
    		android:inputType="textPassword"
    		android:singleLine="true"
    		android:minWidth="60dp"
    		android:minHeight="60dp"
    		android:maxLength="1"
    		android:layout_marginLeft="10dp"
    		android:layout_marginRight="10dp"
    		android:gravity="center"
    		android:textSize="@dimen/font_xxxbig"
            />
        <EditText 
            android:id="@+id/et_1"
            android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
    		android:inputType="textPassword"
    		android:singleLine="true"
    		android:minWidth="60dp"
    		android:minHeight="60dp"
    		android:maxLength="1"
    		android:layout_marginLeft="10dp"
    		android:layout_marginRight="10dp"
    		android:gravity="center"
    		android:textSize="@dimen/font_xxxbig"
            />
        <EditText 
            android:id="@+id/et_2"
            android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
    		android:inputType="textPassword"
    		android:singleLine="true"
    		android:minWidth="60dp"
    		android:minHeight="60dp"
    		android:maxLength="1"
    		android:layout_marginLeft="10dp"
    		android:layout_marginRight="10dp"
    		android:gravity="center"
    		android:textSize="@dimen/font_xxxbig"
            />
        <EditText 
            android:id="@+id/et_3"
            android:layout_width="wrap_content"
    		android:layout_height="wrap_content" 
    		android:inputType="textPassword"
    		android:singleLine="true"
    		android:minWidth="60dp"
    		android:minHeight="60dp"
    		android:maxLength="1"
    		android:layout_marginLeft="10dp"
    		android:layout_marginRight="10dp"
    		android:gravity="center"
    		android:textSize="@dimen/font_xxxbig"
            />
    </LinearLayout>
	<TextView
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="請(qǐng)輸入操作密碼(該操作由服務(wù)員完成)"
	        android:textColor="@color/white"
	        android:textSize="@dimen/font_middle"
	        android:singleLine="true"
	        android:layout_margin="20dp"
	        android:layout_gravity="center_horizontal"
	        />
</LinearLayout>

  


動(dòng)畫文件:
    
<style name="PopupAnimationFromTop" parent="android:Animation"  mce_bogus="1" >
        <item name="android:windowEnterAnimation">@anim/anim_top_in</item>
        <item name="android:windowExitAnimation">@anim/anim_top_out</item>
    </style>

  


anim_top_in.xml
    
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<translate 
	android:fromYDelta="-100%p" 
	android:toYDelta="0" 
	android:duration="200" 
	android:fillAfter="true"
	android:interpolator="@android:anim/bounce_interpolator"
	/>
</set>

  

anim_top_out.xml
    
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<translate 
	android:fromYDelta="0" 
	android:toYDelta="-100%p" 
	android:duration="200"
	android:fillAfter="true"
	android:interpolator="@android:anim/bounce_interpolator"
	/>
</set>


  

android 焦點(diǎn)控制及運(yùn)用


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 留坝县| 三原县| 竹北市| 东城区| 耒阳市| 武宁县| 建水县| 禄劝| 乌兰浩特市| 垫江县| 永顺县| 二连浩特市| 修文县| 义马市| 吴桥县| 武陟县| 辽阳市| 枣庄市| 化隆| 信阳市| 前郭尔| 上饶市| 沙雅县| 扎鲁特旗| 四会市| 沙田区| 乌兰浩特市| 泽库县| 五原县| 开化县| 十堰市| 寻乌县| 乌兰浩特市| 合江县| 家居| 呈贡县| 仁寿县| 聊城市| 徐汇区| 兴隆县| 绥德县|