当前位置:操作系统 > 安卓/Android >>

Android中自定义监听事件带参数

刚开始边学习边开发,错误之处请指正

最近在项目中遇到Android监听事件需要传参的问题

比如,在ListView中,一些不定的数据有CheckBox,要通过点击勾选在下方显示出来,故而在监听 setOnClickListener 事件中,需要带入一些参数来控制

最后想到的方式是不通过默认的监听类

 

[java]  holder.cbNode.setOnClickListener(new OnClickListener() 
                { 
                     
                    @Override 
                    public void onClick(View v) 
                    { 
                        // TODO Auto-generated method stub  
                         
                    } 
                }); 

holder.cbNode.setOnClickListener(new OnClickListener()
    {
     
     @Override
     public void onClick(View v)
     {
      // TODO Auto-generated method stub
      
     }
    });


,自己写一个监听类实现监听接口,构造带参数的构造函数来做,如下所示:


[java]  class CheckBoxListener implements OnClickListener{ 
            private TextView _tvPeople = null; 
            private ViewHolder _holder = null; 
            public CheckBoxListener(TextView tvPeople, ViewHolder holder) 
            { 
                // TODO Auto-generated constructor stub  
                this._tvPeople = tvPeople; 
                this._holder = holder; 
            } 
            @Override 
            public void onClick(View v) 
            { 
                // TODO Auto-generated method stub  
                if(this._holder.cbNode!=null) 
                { 
                    String curSelectedPeople = this._tvPeople.getText().toString(); 
                    if(!this._holder.cbNode.isChecked()) 
                    { 
                        //取消以选择的阅办人  
                        curSelectedPeople = (","+curSelectedPeople).replace(","+this._holder.cbNode.getText().toString(), "").substring(1); 
                        this._tvPeople.setText(curSelectedPeople); 
                    }else 
                    { 
                        //显示增加的阅办人  
                        if(!curSelectedPeople.equals("")) 
                            curSelectedPeople+= ","+this._holder.cbNode.getText().toString(); 
                        else 
                            curSelectedPeople = this._holder.cbNode.getText().toString(); 
                        _tvPeople.setText(curSelectedPeople); 
                    } 
                } 
            } 
             
        } 

class CheckBoxListener implements OnClickListener{
   private TextView _tvPeople = null;
   private ViewHolder _holder = null;
   public CheckBoxListener(TextView tvPeople, ViewHolder holder)
   {
    // TODO Auto-generated constructor stub
    this._tvPeople = tvPeople;
    this._holder = holder;
   }
   @Override
   public void onClick(View v)
   {
    // TODO Auto-generated method stub
    if(this._holder.cbNode!=null)
    {
     String curSelectedPeople = this._tvPeople.getText().toString();
     if(!this._holder.cbNode.isChecked())
     {
      //取消以选择的阅办人
     

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,