0

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

自定义列表.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;
4

3 回答 3

0

试试这个,使用ListView.CHOICE_MODE_MULTIPLE

 listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
于 2013-03-15T13:44:41.490 回答
0

我认为这ListView.CHOICE_MODE_SINGLE将更适合他的需要。

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

于 2013-03-15T13:49:19.547 回答
0

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

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

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));
            }
        }
    }
}

第2步)

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;
    }
}

步骤 3)

<?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>
于 2013-03-15T14:19:26.140 回答