【问题标题】:setting radio button for different task为不同的任务设置单选按钮
【发布时间】:2017-01-09 16:59:22
【问题描述】:

我正在尝试在对话框中显示单选组,当单击单选按钮时,它将在共享首选项中设置一些数据。单击任何按钮时我都可以烤面包,但是单击不同按钮时我想做点什么。我尝试在单选按钮中设置 Onclick,但它不起作用。我正在尝试向 here 学习 这是我的对话类

 public Activity c;
public Dialog d;
public Button yes, no;
public RadioGroup radioGroup;
public RadioButton radioButton;

public CustomDialogClass(Activity a) {
    super(a);
    // TODO Auto-generated constructor stub
    this.c = a;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.custom_dialog);
    yes = (Button) findViewById(R.id.btn_yes);
    no = (Button) findViewById(R.id.btn_no);
    yes.setOnClickListener(this);
    no.setOnClickListener(this);
    Radioactive();
}

private void Radioactive() {
    radioGroup = (RadioGroup) findViewById(R.id.radio);
    int selectedId = radioGroup.getCheckedRadioButtonId();
    radioButton = (RadioButton) findViewById(selectedId);




}

我只想知道在选中特定单选按钮时如何做一些事情。

【问题讨论】:

  • 您的所有RadioButtons 是否都在RadioGroup 标签内并且有单独的ID?另外,RadioActive() 应该在 onClick() 方法中,所以也分享一下
  • 你们都在不同 id 的 RadioGroup 下

标签: android radio-button android-radiobutton


【解决方案1】:

单选按钮点击可以实现如下。 更多详情android onCheckedChanged for radiogroup 针对不同的功能

radioGroup
    .setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
 Log.d("chk", "id" + checkedId);

            if (checkedId == R.id.a) {
                //some code
            } else if (checkedId == R.id.b) {
                //some code
            }

        }

    });

【讨论】:

    猜你喜欢
    • 2017-09-30
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多