zoukankan      html  css  js  c++  java
  • android自己定义TextView

    Android控件中的TextView控件仅仅有一个输入框。可是为了用于的操作方便我们应该实现一些功能:

    1. 能够直接将内容删除的功能button

    2. 可以记录用户曾经输入的数据,同一时候可以将数据通过下拉显示,点击的时候实现输入

    先上图:

    下拉的图片没有做。所以和删除的图片使用同一个了,同志们能够直接在xml文件里更换即可了


    分析:

    肯定要使用自己定义view来实现的。我们知道自己定义view大概能够分为三类:自绘控件,组合控件,继承控件,我们这里是要进行增强的textView的功能。所以我这里使用的

    是组合控件的方式来进行实现

    既然使用组合控件,那么我们就来看看究竟要使用什么控件来组合呢:

    1.  当中一个必须是textView了

    2. 下拉的那两个button是什么。当然是imageView了

    3. 另一个下拉列表,。

    。。。那就使用popwindow了

    思路:

    1. 怎样实现直接删除用户的输入

    使用addTextChangedListener监听textView内容的变化的时间依据内容的变化进行确定是否显示删除button,同一时候绑定删除button的点击事件

    2.怎样实现下拉显示用户输入过的数据,以及选中的时候实现输入

     我们通过下拉button的点击进行确定popwindow窗体的显示,在popwindow的界面有一个listview,在这个listview的adpater中进行绑定条目的点击事件

    那么我们在adapter中绑定的事件,怎样控制整个控件的功能输入呢,这里就是用handler了,在创建adapter的时候将handler传递过去,

    然后当点击事件发生的时候我们使用handler进行send消息即可了,当然我们在send消息的时候将当前点击的数据传递过来即可了

    上代码:

    1. 控件主体代码

    /**
     * 自己定义的控件,自带删除的button,下拉button
     * @author zcs
     * */
    public class EditTextClearSelect extends FrameLayout {
    	
    	private EditText editText;  //显示的editText输入框
        private ImageButton clearImageButton;  //显示的用于进行删除editText中内容的button
        private ImageButton selectImageButton;  //显示的用于下拉editText中内容的button
    	
        //PopupWindow对象  ,用于已下拉形式显示数据
        private PopupWindow selectPopupWindow= null;  
        //自己定义Adapter  
        private ECS_OptionsAdapter optionsAdapter = null;  
        //下拉框选项数据源  
        private ArrayList<String> datas = new ArrayList<String>();   
        //下拉框依附组件 
        private LinearLayout parent;  
        //展示全部下拉选项的ListView  
        private ListView listView = null;   
        //用来处理选中或者删除下拉项消息  
        private Handler handler;  
        
    	public EditTextClearSelect(Context context) {
    		super(context);
    	}
    	//用于对自己定义的控件进行初始化
    	public EditTextClearSelect(Context context, AttributeSet attrs){
    		super(context, attrs);
    		//调用初始化自己定义控件的方法
    		init(context,attrs);
    	}
    	
    	/**  
         * 初始化下拉功能使用的组件  
         */  
        private void initWedget(Context context){  
            //初始化Handler,用来处理消息  
            handler = new Handler(){
            	public void handleMessage(Message msg) {
            		//当adapter中传递过来消息以后依据选中的id,将相应的值填写到EditText组件中
            		Bundle data = msg.getData();  
                    //选中下拉项,下拉框消失  
                    int selIndex = data.getInt("selIndex");  
                    editText.setText(datas.get(selIndex));  
                    dismiss(); 
            	}  
            };
              
            //假设没有数据。则下拉菜单不显示
            if( !(datas.size() > 0) ){
            	selectImageButton.setVisibility(View.GONE);
            }
            
            //设置点击下拉箭头图片事件,点击弹出PopupWindow浮动下拉框  
            selectImageButton.setOnClickListener(new View.OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                	 //获取下拉框依附的组件宽度,然后又一次设置popWindow的宽度
                	selectPopupWindow.setWidth(parent.getWidth());
                    //显示PopupWindow窗体  
                    popupWindwShowing();  
                }  
            });  
              
            //初始化PopupWindow  
            initPopuWindow(context);  
        }  
    	
        /**  
         * 初始化PopupWindow  
         */   
        private void initPopuWindow(Context context){   
              
            //PopupWindow浮动下拉框布局  
            View loginwindow = LayoutInflater.from(context).inflate(R.layout.wecs_options, null); 
            listView = (ListView) loginwindow.findViewById(R.id.list);   
              
            //设置自己定义Adapter  
            optionsAdapter = new ECS_OptionsAdapter(context,handler,datas);   
            listView.setAdapter(optionsAdapter);   
    
            selectPopupWindow = new PopupWindow(loginwindow, 0,LayoutParams.WRAP_CONTENT, true);   
            selectPopupWindow.setOutsideTouchable(true);   
            //实现当点击屏幕其它地方的时候将当前的pop关闭
            selectPopupWindow.setBackgroundDrawable(new BitmapDrawable());    
        }  
        
        /**  
         * 显示PopupWindow窗体  
         * @param popupwindow  
         */   
        public void popupWindwShowing() { 
           //将pop窗体在自己定义控件的底部显示
           selectPopupWindow.showAsDropDown(parent);   
        }   
           
        /**  
         * PopupWindow消失  
         */   
        public void dismiss(){   
            selectPopupWindow.dismiss();   
        }  
        
    	 /**
         * 初始化,包含添加删除button。下拉button
         */
        public void init(Context context,AttributeSet attrs){
        	//获取自己定义控件的界面,相当于当前的自己定义View就使用的View
            View view = LayoutInflater.from(context).inflate(R.layout.weight_edit_clear_select, this, true);
            
            parent =  (LinearLayout) view.findViewById(R.id.parent);  //当前的自己定义控件
            editText = (EditText) view.findViewById(R.id.et);  //输入框 
            clearImageButton = (ImageButton) view.findViewById(R.id.clear_ib); //删除button
            selectImageButton = (ImageButton) view.findViewById(R.id.select_id); //下拉button
            
            //当点击删除button的会后将输入框数据清空
            clearImageButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    editText.setText("");
                }
            });
            //依据输入框中的内容。决定是否显示删除button
            editText.addTextChangedListener(new TextWatcher(){
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (s.length() > 0) {
                    	//输入框有内容,显示button
                    	editText.setSelection(s.length());
                        clearImageButton.setVisibility(View.VISIBLE);
                    } else {
                        clearImageButton.setVisibility(View.GONE);
                    }
                }
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                }
                @Override
                public void afterTextChanged(Editable s) {
                }
    
            });
            
            //初始化pop组件,设置下拉button的功能
            initWedget(context);  
            
            //将属性值设置到控件中
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EditTextClearSelect);
            //输入框的默认的显示文本
            CharSequence text = a.getText(R.styleable.EditTextClearSelect_textECS);
            CharSequence hint = a.getText(R.styleable.EditTextClearSelect_hintECS);
            CharSequence parent_width  = a.getText(R.styleable.EditTextClearSelect_layout_width);
            
            if (text!=null&&!"".equals(text.toString().trim())) {
                editText.setText(text);
                //设置光标位置
                editText.setSelection(text.length());
                this.clearImageButton.setVisibility(View.VISIBLE);
            }
            if (hint!=null&&!"".equals(hint.toString().trim())) {
                editText.setHint(hint);
            }
            if(parent_width!=null && !"".equals(parent_width.toString().trim()) ){
            	//设置当前控件的宽度,为屏幕的百度比有參数进行设置
            	LayoutParams parent_lp = (LayoutParams) parent.getLayoutParams();
            	parent_lp.width = (int) (AppUtil.getScreenDispaly(context)[0] * ( (Double)(Double.parseDouble(parent_width.toString()) / 100) ));
            	Log.i("控件宽度", parent_lp.width+"");
            	parent.setLayoutParams(parent_lp);
            }
            a.recycle();
        }
         
        /**
         * 获得输入的值
         * @return
         */
        public String getText(){
            return this.editText.getText().toString();
        }
         
        /**
         * 设置值
         * @param text
         */
        public void setText(String text){
            this.editText.setText(text);
        }
         
        /**
         * 设置默认值
         * @param hint
         */
        public void setHint(String hint){
            this.editText.setHint(hint);
        }
         
        /**
         * 获得输入框控件
         * @return
         */
        public EditText getEditText(){
            return this.editText;
        }
         
        /**
         * 获得消除button
         * @return
         */
        public ImageButton getClearImageButton(){
            return this.clearImageButton;
        }
    	
        //设置下拉列表中的选项值
        public void setOptionsValue(ArrayList<String> inDatas){
        	datas.clear();
        	if( (inDatas ==null) || !(inDatas.size() > 0) ){
        		selectImageButton.setVisibility(View.GONE);
        	}else{
        		selectImageButton.setVisibility(View.VISIBLE);
        		datas.addAll(inDatas);
        	}
        	optionsAdapter.notifyDataSetChanged();
        }

    2. popwindow里面listview的适配器

    public class ECS_OptionsAdapter extends BaseAdapter {
    
    	  	private ArrayList<String> list = new ArrayList<String>();   
    	    private Context context = null;   
    	    //传递过来的hanler,用于进行通知操作(这里是通知自己定义的view要继续改动editText中的数据)
    	    private Handler handler;
    	    
    	    public ECS_OptionsAdapter(Context context,Handler handler,ArrayList<String> list){  
    	        this.context = context;  
    	        this.handler = handler;  
    	        this.list = list;  
    	    }  
    	      
    	    @Override  
    	    public int getCount() {  
    	        return list.size();  
    	    }  
    	  
    	    @Override  
    	    public Object getItem(int position) {  
    	        return list.get(position);  
    	    }  
    	  
    	    @Override  
    	    public long getItemId(int position) {  
    	        return position;  
    	    }  
    	  
    	    @Override  
    	    public View getView(final int position, View convertView, ViewGroup parent) {  
    	          
    	    	ViewHolder holder = null;   
    	        if (convertView == null) {   
    	            holder = new ViewHolder();   
    	            //下拉项布局  
    	            convertView = LayoutInflater.from(context).inflate(R.layout.wecs_option_item, null);   
    	            holder.textView = (TextView) convertView.findViewById(R.id.item_text);   
    	            convertView.setTag(holder);   
    	        } else {   
    	            holder = (ViewHolder) convertView.getTag();   
    	        }   
    	        holder.textView.setText(list.get(position));  
    	        //为下拉框选项文字部分设置事件。终于效果是点击将其文字填充到文本框  
    	        holder.textView.setOnClickListener(new View.OnClickListener() {  
    	            @Override  
    	            public void onClick(View v) {  
    	            	//当点击的时候进行发送消息。通知组件进行改动数据
    	                Message msg = new Message();  
    	                //设置要传递的数据
    	                Bundle data = new Bundle();  
    	                //设置选中索引  
    	                data.putInt("selIndex", position);  
    	                msg.setData(data); 
    	                //发出消息  
    	                handler.sendMessage(msg);
    	            }  
    	        });  
    	        return convertView;   
    	    }  
    	  
    	}  
    
    	class ViewHolder {   
    	    TextView textView;   
    	}   

    3. 使用

    	private EditTextClearSelect etcs;
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		init();
    	}
    	
    	//初始化
    	private void init(){
    		String selectDatas = "admin,login,user";
    		etcs = (EditTextClearSelect) findViewById(R.id.username_edit);
    		//为控件设置下拉的数据
    		etcs.setOptionsValue( new ArrayList<String>(Arrays.asList(selectDatas.split(","))) );
    		
    	}

    ok搞定

    源代码下载


  • 相关阅读:
    iOS- 优化与封装 APP音效的播放
    iOS- iPhone App 如何运营?
    iOS- 封装单例宏
    iOS- 详解文本属性Attributes
    iOS- 如何将应用集成发短信、发邮件、打电话
    iOS- <项目笔记> UIApplication常见属性与方法总结
    iOS- <项目笔记>iOS6 & iOS7屏幕图片适配
    iOS- <项目笔记>项目配置常见文件
    iOS- <项目笔记>UI控件常见属性总结
    iOS- UIPickerView餐厅点餐系统
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6848641.html
Copyright © 2011-2022 走看看