如何在Listview中获取单选按钮的值单击Android中的按钮

时间:2014-11-24 08:47:16

标签: java android android-listview

我在ListView中有Radio Group of Three Radio Button和一个提交按钮。我想得到Button监听器,并获得所选单选按钮的值。下面是我的listview项目视图。请帮帮我。

enter image description here

3 个答案:

答案 0 :(得分:1)

将此代码放入点击事件

if(rg1.getCheckedRadioButtonId()!=-1){
    int id= rg1.getCheckedRadioButtonId();
    View radioButton = rg1.findViewById(id);
    int radioId = radioGroup.indexOfChild(radioButton);
    RadioButton btn = (RadioButton) rg1.getChildAt(radioId);
    String selection = (String) btn.getText();
}

答案 1 :(得分:1)

您需要在自定义适配器的onClickListener方法中编写getView

public View getView(final int position, View convertView,ViewGroup parent) 
{

    Button submit= (Button) convertView.findViewById(R.id.submit);

    submit.setOnClickListener(new OnClickListener() 
    { 
        @Override
        public void onClick(View v) 
        {
            RadioGroup rGroup = (RadioGroup)convertView.findViewById(R.id.radiogroup);
            int checkedID = rGroup.getCheckedRadioButtonId();
            if(checkedID >=0 )
                RadioButton rButton = (RadioButton) convertView.findViewById(checkedId);
            else{
                //no radio button was checked
            }
        }  

    });
    return convertView ;
}

希望这有帮助。

答案 2 :(得分:0)

RadioGroup group=(RadioGroup) findViewById(R.id.radioGroup1);

        group.setOnCheckedChangeListener(new OnCheckedChangeListener() 
           {

            public void onCheckedChanged(RadioGroup group, int checkedId) 
               {
                // TODO Auto-generated method stub
                if(radiobutton1.isChecked())
                  {
                    //your code
                  }
                else if(radiobutton2.isChecked())
                  {

                     //your code
                  }
             }
        });