zoukankan      html  css  js  c++  java
  • android 输入框自动匹配AutoCompleteTextView

    android 输入框自动匹配-AutoCompleteTextView

    封装自动适配的adapter可以监听,并且可以监听自动匹配输入框的输入事件。
    package com;
    
    import java.util.List;
    
    import android.app.Activity;
    import android.text.Editable;
    import android.text.TextUtils;
    import android.text.TextWatcher;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.AutoCompleteTextView;
    
    /**
     * @ClassName: AutoCompleteTextViewUtil
     * @author:hlu
     * @Date:2013-4-17
     * @version  V1.0.0
     * @Description: AutoCompleteTextView 实现的自动匹配的实现
     */
    public class AutoCompleteTextViewUtil {
    	
    	private AutoCompleteTextView mACTV ;
    	private ArrayAdapter<String> mAdapter  ;
    	
    	private ChangeAdapterDataListener mListener ;
    	
    	private List<String> mListObjects= null ;
    	
    	private boolean flagMListObjects = false  ;
    	//是否执行查询,当输入框的字符长度大于2时,才会查询,查询了一次之后不会再查询。
    	private boolean flagQuery = false ;
    	
    	public AutoCompleteTextViewUtil(AutoCompleteTextView actv){
    		this.mACTV = actv ;
    		mACTV.addTextChangedListener(watcher) ;
    		mACTV.setOnFocusChangeListener(focusListener) ;
    		mACTV.setOnItemClickListener(itemClickListener) ;
    		mACTV.setOnClickListener(onClickL) ;
    	}
    	
    	public void setAdapter(Activity activity,int resource,List<String> objects){
    		this.mListObjects = objects ;
    		flagMListObjects = true ;
    		flagQuery = false ;
    		mAdapter = new ArrayAdapter<String>(activity,android.R.layout.simple_dropdown_item_1line, mListObjects);
    		mACTV.setAdapter(mAdapter) ;
    	}
    	
    	public void setAdapter(Activity activity,List<String> objects){
    		this.setAdapter(activity, android.R.layout.simple_dropdown_item_1line, objects);
    	}
    	
    	public void setChangeAdapterDataListener(ChangeAdapterDataListener l){
    		this.mListener = l ;
    	}
    	
    	private void setDataList(String text){
    		List<String> list = mListener.getDataList(text) ;
    		if(list != null && list.size() > 0){
    			mListObjects.addAll(list) ;
    		}
    	}
    	
    	private TextWatcher watcher = new TextWatcher() {
    
    	    @Override
    	    public void beforeTextChanged(CharSequence s, int start, int count,
    		    int after) {
    		
    	    }
    
    	    @Override
    	    public void onTextChanged(CharSequence s, int start, int before,
    		    int count) {
    	    	String enteredText = s.toString() ;
    	    	if(!TextUtils.isEmpty(enteredText) && enteredText.trim().length() > 0){
    	    		//查询新匹配值
    	    		if(!flagQuery){
    		    		if(flagMListObjects){
    		    			//更新List的数据。
    		    			setDataList(enteredText.trim());
    		    		}
    		    		mAdapter.notifyDataSetChanged() ;
    	    		}
    	    		flagQuery = true ;
    	    	}else{
    	    		flagQuery = false ;
    	    	}
    	    }
    
    	    @Override
    	    public void afterTextChanged(Editable s) {
    	    	
    	    }
    		
    	} ;
    	
    	
    	
    	public interface ChangeAdapterDataListener{
    		
    		List<String> getDataList(String text) ;
    		
    		void checkInputTextExist(String text) ;
    		
    	}
    	
    	private View.OnFocusChangeListener focusListener = new View.OnFocusChangeListener() {
    		
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
            	mACTV.setFocusable(hasFocus) ;
            	if(!hasFocus){
            		mListener.checkInputTextExist(mACTV.getText().toString().trim()) ;
            	}
            }
        }; 
    
        private OnItemClickListener itemClickListener = new OnItemClickListener() {
    
    		@Override
    		public void onItemClick(AdapterView<?> parent, View view, int position,
    				long id) {
    			mListener.checkInputTextExist(mACTV.getText().toString().trim()) ;
    		}
        	
    	};
        
        private OnClickListener onClickL = new OnClickListener() {
    		
    		@Override
    		public void onClick(View v) {
    			mACTV.setFocusableInTouchMode(true) ;
    			mACTV.requestFocus();
    		}
    	};
    	
    }
    


    
    
    使用方法:
    AutoCompleteTextViewUtil actvUtil = new AutoCompleteTextViewUtil(purchaseOrderSite) ;
    		List<String> data = new ArrayList<String>() ;
    		actvUtil.setAdapter(this, data) ;
    		actvUtil.setChangeAdapterDataListener(new ChangeAdapterDataListener() {
    	    
    	    @Override
    	    public List<String> getDataList() {
    	    	List<String> data = new ArrayList<String>() ;
    			data.add("qaz");
    			data.add("qaza");
    			data.add("qazab");
    			data.add("qazabd");
    			return data;
    	    }
    		});
    效果图:



    注意:AutoCompleteTextView.setOnItemSelectedListener(), 没有效果



  • 相关阅读:
    解读Unity中的CG编写Shader系列十 (光滑的镜面反射(冯氏着色))
    解读Unity中的CG编写Shader系列八(多光源漫反射)
    Unity3d《Shader篇》漫反射
    解读Unity中的CG编写Shader系列八(镜面反射)
    渲染物体到一张UITexture上
    解读Unity中的CG编写Shader系列六(漫反射)
    解读Unity中的CG编写Shader系列五(理论知识)
    解读Unity中的CG编写Shader系列四(unity中的圆角矩形shader)
    解读Unity中的CG编写Shader系列三
    解读Unity中的CG编写Shader系列二
  • 原文地址:https://www.cnblogs.com/java20130722/p/3207265.html
Copyright © 2011-2022 走看看