代码之家  ›  专栏  ›  技术社区  ›  ajwest

如何响应来自自定义视图中按钮的单击事件?

  •  0
  • ajwest  · 技术社区  · 10 年前

    我在努力适应 this tutorial 用于 TextView Button 。我已经创建了 ArrayList ListView :

       private void displayListView() {
    
        //putting two Country objects into the ArrayList
        //these countries have email addresses for some reason, just go with it
        ArrayList<Country> countryList = new ArrayList<Country>();
        country = new Country("email@email.com","DummyName1",false);
        countryList.add(country);
        country = new Country("email@email.com","DummyName2",false);
        countryList.add(country);
    
        //create an ArrayAdaptar from the String Array
        dataAdapter = new MyCustomAdapter(this,
                R.layout.pending_invite, countryList);
    
        ListView listView = (ListView) findViewById(R.id.listViewPendingRequests);
        // Assign adapter to ListView
        listView.setAdapter(dataAdapter);
    
    }
    

    以下是我的主要XML:

     <?xml version="1.0" encoding="utf-8"?>
    
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:orientation="vertical" android:layout_width="match_parent"
         android:layout_height="match_parent">  
    
             <ListView
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:id="@+id/listViewPendingRequests" />
    
      </LinearLayout>
    

    这是我的 pending_invite.xml :

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout" >
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Pending Invitation 1"
                android:id="@+id/textView7"
                android:layout_weight="1" />
    
            <Button
                style="?android:attr/buttonStyleSmall"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="Cancel"
                android:id="@+id/button5"
                android:layout_weight="1"
                android:onClick="onClickCancel"/>
        </LinearLayout>
    </LinearLayout>
    

    我想我有麻烦了 MyCustomAdapter 考虑到我不再找支票之类的东西了。无论如何,这就是我目前所得到的:

     private class MyCustomAdapter extends ArrayAdapter<Country> {
    
        private ArrayList<Country> countryList;
    
        public MyCustomAdapter(Context context, int textViewResourceId,
                               ArrayList<Country> countryList) {
            super(context, textViewResourceId, countryList);
            this.countryList = new ArrayList<Country>();
            this.countryList.addAll(countryList);
        }
    
        private class ViewHolder {
            TextView code;
            Button name;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            ViewHolder holder = null;
            Log.v("ConvertView", String.valueOf(position));
    
            if (convertView == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.pending_invite, null);
    
                holder = new ViewHolder();
                holder.code = (TextView) convertView.findViewById(R.id.textView7);
                holder.name = (Button) convertView.findViewById(R.id.button5);
                convertView.setTag(holder);
    
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            Country country = countryList.get(position);
            holder.name.setText(country.getName());
            holder.name.setTag(country);
    
            return convertView;
    
        }
    
    }
    

    因此,这对于构造视图是有效的(令人惊讶),但我如何知道在onClick中按下了哪个?:

     public void onClickCancel(View view){
        Log.i(LOG_TAG, "OnClickCancel view: " + view.getId()); //getId returns the same thing for all clicks. How do I get which button clicked?
    
    }
    

    这个 view.getId 为所有按钮返回相同的ID。如何将onClick侦听器附加到自定义视图中的膨胀按钮?

    4 回复  |  直到 10 年前
        1
  •  1
  •   ajwest    10 年前

    你可以这样做

    private class MyCustomAdapter extends ArrayAdapter<Country> {
    
        private ArrayList<Country> countryList;
    
        public MyCustomAdapter(Context context, int textViewResourceId,
                               ArrayList<Country> countryList) {
            super(context, textViewResourceId, countryList);
            this.countryList = new ArrayList<Country>();
            this.countryList.addAll(countryList);
        }
    
        private class ViewHolder {
            TextView code;
            Button name;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            ViewHolder holder = null;
            Log.v("ConvertView", String.valueOf(position));
    
            if (convertView == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.pending_invite, null);
    
                holder = new ViewHolder();
                holder.code = (TextView) convertView.findViewById(R.id.textView7);
                holder.name = (Button) convertView.findViewById(R.id.button5);
                convertView.setTag(holder);
    
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            Country country = countryList.get(position);
           // holder.code.setText(" (" +  country.getCode() + ")");
            holder.name.setText(country.getName());
            //holder.name.setChecked(country.isSelected());
            holder.name.setTag(countryList.get(position).getName());     // here you are setting the tag, it will be retrieved in handleClick method
            // here create your on click handler, like
    
           holder.name.setOnClickListener(handleClick(holder.name));
    
    
            return convertView;
    
        }
    
    }
    

    在此定义您的 handleClick 方法

    private OnClickListener handleClick(final Button butn) {
            // TODO Auto-generated method stub
            return new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    String TAG = butn.getTag().toString();  // get the tag, now you can compare which button has been clicked with respect to its tag name 
    
                 if (TAG.equals("somehting")
                 {
                      // your logic here
                 }
                }
            };
        }
    

    我希望你从这个答案中得到一些想法。

        2
  •  0
  •   Arun Shankar    10 年前

    在类Country中创建一个Filed“ID”。然后在创建Country实例时为每个对象分配一个值。然后在class country中为按钮添加一个onclick监听器,传递位置。然后使用下面的函数获得点击

    public void onClickCancel(int position){
    
    Country country = countryList.get(position);
    
    String countryId = country.ID;
    Log.i(LOG_TAG, "OnClickCancel view: " + countryId ); 
    }
    
        3
  •  0
  •   Hareshkumar Chhelana    10 年前

    试着这样做,希望这能帮助你解决问题。

    private class MyCustomAdapter extends ArrayAdapter<Country> {
    
        private ArrayList<Country> countryList;
        private Context context;
    
        public MyCustomAdapter(Context context, int textViewResourceId,
                               ArrayList<Country> countryList) {
            super(context, textViewResourceId, countryList);
            this.countryList =countryList;
            this.context = context;
        }
    
        private class ViewHolder {
            TextView code;
            Button name;
        }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
    
            ViewHolder holder;
            Log.v("ConvertView", String.valueOf(position));
    
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.pending_invite, null);
                holder = new ViewHolder();
                holder.code = (TextView) convertView.findViewById(R.id.textView7);
                holder.name = (Button) convertView.findViewById(R.id.button5);
                convertView.setTag(holder);
    
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            holder.name.setText(countryList.get(position).getName());
            holder.name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context,countryList.get(position).getName(),Toast.LENGTH_SHORT).show();
                }
            });
            holder.name.setOnClickListener(handleClick(holder.name));
    
            return convertView;
    
        }
    
    }
    
        4
  •  0
  •   Linh Nguyen    10 年前

    如果只想捕捉按钮的onClick,则必须为按钮实现onClickListener:holder.name

    holder.name.setOnClickListener(新View.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
            }
        });
    

    如果要获取单击的项目,请为列表视图设置OnItemClickListener。或者只为customView设置OnClickListener convertView.setOnClickListener(新View.OnClickListener(){

    @覆盖
    单击时公共无效(视图v){
    //TODO自动生成的方法存根
    
    }
    });