【问题标题】:check box on custom listview自定义列表视图上的复选框
【发布时间】:2013-03-15 22:28:11
【问题描述】:

我是这样的,它不应该只显示在特定项目上 如何将自定义列表视图中的单选按钮设为单一模式。如果我单击特定的单选按钮,则它必须出现,而另一个未选中。如果单击第一行,则选中第一行上的单选按钮。单击第二行上的第二行单选按钮,选中第二行,第一行未选中。单击第三行上的第三行单选按钮,选中,第一行和第二行未选中。

customlist.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<RadioButton
      android:id="@+id/radioButton1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:layout_alignParentLeft="true"
      android:text=""

       />

  <TextView
      android:id="@+id/card"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingLeft="10dip" android:layout_centerVertical="true"
      android:layout_toRightOf="@+id/radioButton1"
      android:text=""
      android:textColor="#000000"
      android:textSize="15dip"
      android:textStyle="bold" />


  </RelativeLayout>

  listView = (ListView) findViewById(R.id.cardlist);

adapter =new MyAdapter(this, app.arryList,app.arryList1);

listView.setOnItemClickListener(new OnItemClickListener() {

  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
      // TODO Auto-generated method stub

      Toast.makeText(getApplicationContext(), "am thelist",Toast.LENGTH_LONG).show();


  }
});


listView.setAdapter(adapter);

listView.setChoiceMode(listView.CHOICE_MODE_SINGLE);
}



public class MyAdapter extends BaseAdapter {

Context context = null;
ArrayList<String> items= null;
ArrayList<String> items1= null;


public MyAdapter(Newcard newcard, ArrayList<String> items,
      ArrayList<String> items1) {
  // TODO Auto-generated constructor stub

  this.items = items;
  this.items1 = items1;

}

@Override
public int getCount() {
  // TODO Auto-generated method stub
  return items.size();
}

@Override
public Object getItem(int position) {
  // TODO Auto-generated method stub
  return items; 

  //return items.get(position);
}

@Override
public long getItemId(int position) {
  // TODO Auto-generated method stub
  return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub

  View layout = null;
  TextView produ = null;  

  TextView desc = null;
  Button edit = null;

  RadioButton radio =null;

  if (convertView == null) {

      lay = LayoutInflater.from(getApplicationContext());
      layout = lay.inflate(R.layout.customlist, null);
  } else {
      layout = convertView;
  }

  produ = (TextView) layout.findViewById(R.id.card);
  produ.setText("" +app.arryList.get(position));


  radio = (RadioButton) layout.findViewById(R.id.radioButton1);



  radio.setOnClickListener(new OnClickListener() {

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

                      //check.setVisibility(View.GONE);


          System.out.println("data "+app.arryList.get(position)); 

      }
  });



      }
  });



  return layout;

【问题讨论】:

    标签: android radio-button


    【解决方案1】:

    我试图解决您的问题并从我这边完成。

    我在下面解释了方法,有关更多详细信息和完整源代码,请参阅下面的博客。

    http://amitandroid.blogspot.in/2013/03/android-custon-single-choice-lsitview.html

    希望这能解决您的问题。

    我创建了自定义单选列表。您可以更改布局并相应地添加小部件。

    步骤1)

    package com.custom.view;
    
    import java.util.ArrayList;
    import java.util.List;
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Checkable;
    import android.widget.RelativeLayout;
    
    
    public class CheckableRelativeLayout extends RelativeLayout implements
            Checkable {
    
        private boolean isChecked;
        private List<Checkable> checkableViews;
    
        public CheckableRelativeLayout(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
            initialise(attrs);
        }
    
        public CheckableRelativeLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            initialise(attrs);
        }
    
        public CheckableRelativeLayout(Context context, int checkableId) {
            super(context);
            initialise(null);
        }
    
        public boolean isChecked() {
            return isChecked;
        }
    
        public void setChecked(boolean isChecked) {
            this.isChecked = isChecked;
            for (Checkable c : checkableViews) {
                c.setChecked(isChecked);
            }
        }
        public void toggle() {
            this.isChecked = !this.isChecked;
            for (Checkable c : checkableViews) {
                c.toggle();
            }
        }
    
        @Override
        protected void onFinishInflate() {
            super.onFinishInflate();
    
            final int childCount = this.getChildCount();
            for (int i = 0; i < childCount; ++i) {
                findCheckableChildren(this.getChildAt(i));
            }
        }
    
        /**
         * Read the custom XML attributes
         */
        private void initialise(AttributeSet attrs) {
            this.isChecked = false;
            this.checkableViews = new ArrayList<Checkable>(5);
        }
    
        /**
         * Add to our checkable list all the children of the view that implement the
         * interface Checkable
         */
        private void findCheckableChildren(View v) {
            if (v instanceof Checkable) {
                this.checkableViews.add((Checkable) v);
            }
    
            if (v instanceof ViewGroup) {
                final ViewGroup vg = (ViewGroup) v;
                final int childCount = vg.getChildCount();
                for (int i = 0; i < childCount; ++i) {
                    findCheckableChildren(vg.getChildAt(i));
                }
            }
        }
    }
    

    第二步)

    package com.custom.view;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.KeyEvent;
    import android.view.MotionEvent;
    import android.widget.CheckBox;
    public class InertCheckBox extends CheckBox {
    
        public InertCheckBox(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public InertCheckBox(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public InertCheckBox(Context context) {
            super(context);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            return false;
        }
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            return false;
        }
    
        @Override
        public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
            return false;
        }
    
        @Override
        public boolean onKeyPreIme(int keyCode, KeyEvent event) {
            return false;
        }
    
        @Override
        public boolean onKeyShortcut(int keyCode, KeyEvent event) {
            return false;
        }
    
        @Override
        public boolean onKeyUp(int keyCode, KeyEvent event) {
            return false;
        }
    
        @Override
        public boolean onTrackballEvent(MotionEvent event) {
            return false;
        }
    }
    

    第三步)

    <?xml version="1.0" encoding="utf-8"?>
    <com.custom.view.CheckableRelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_marginRight="10dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp">
    
    
        <com.custom.view.InertCheckBox 
            android:id="@+id/singleitemCheckBox"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_alignParentLeft="true" 
            android:layout_centerVertical="true" 
            android:focusable="false" 
            android:button="@drawable/single_radio_chice" />
    
        <TextView
            android:id="@+id/singleitemId" 
            android:layout_width="wrap_content"
            android:layout_height="fill_parent" 
            android:layout_toRightOf="@+id/singleitemCheckBox"
            android:layout_centerVertical="true" 
            android:layout_marginLeft="20dp"
            android:textSize="14sp" 
            android:focusable="false" />
    
        <TextView
            android:id="@+id/nextitem" 
            android:layout_width="wrap_content"
            android:layout_height="fill_parent" 
            android:layout_toRightOf="@+id/singleitemId"
            android:layout_centerVertical="true" 
            android:layout_marginLeft="20dp"
            android:textSize="14sp" 
            android:focusable="false" />
    
    </com.custom.view.CheckableRelativeLayout>
    
    
    Step4)
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >
    
        <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:cacheColorHint="#00000000"
            android:divider="#b5b5b5"
            android:dividerHeight="1dp"
            android:choiceMode="singleChoice"
            android:listSelector="#00000000" />
    
    </RelativeLayout>
    

    Step5) single_radio_chice.xml

     <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
            <item android:state_checked="false" 
                android:drawable="@drawable/radio_off" />
    
            <item android:state_checked="true" 
                android:drawable="@drawable/radio_on" />
        </selector>
    

    【讨论】:

    • 我们如何才能使单选按钮最初默认选中?
    【解决方案2】:

    试试这个,使用ListView.CHOICE_MODE_MULTIPLE

     listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    

    【讨论】:

      【解决方案3】:

      我觉得ListView.CHOICE_MODE_SINGLE会更适合他的需求。

      但也许保存一组单选按钮并相应地设置它们是一个不错的主意。

      【讨论】:

      • 我检查了 ListView.CHOICE_MODE_SINGLE 和 istView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);两者都不起作用
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-23
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多