简体   繁体   English

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

[英]How can get the value of Radio Button Inside Listview Click on Button in Android

I have Radio Group of Three Radio Button and a submit button also inside of ListView. 我在ListView中也有三个单选按钮的Radio Group和一个Submit按钮。 I want to get the Button listener and also get the value of selected radio button. 我想要获取Button侦听器,还希望获取所选单选按钮的值。 Below to my listview item view. 下面到我的listview项目视图。 please help me. 请帮我。

在此处输入图片说明

put this code inside click event 将此代码放入click事件中

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

You will need to write onClickListener in your custom adapter's getView method: 您将需要在自定义适配器的getView方法中编写onClickListener

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

Hope this helps. 希望这可以帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM