1
回答
AlertDialog如何获取用户所选择的按钮
滴滴云服务器,限时包月0.9元,为开发者而生>>>   
是获取这里面的按钮,然后触发相应的事件 

<无标签>
举报
fanbaobao
发帖于3年前 1回/1K+阅
共有1个答案 最后回答: 3年前

为你的单选框item设定一个监听器:如下

final ChoiceOnClickListener choiceListener =  new ChoiceOnClickListener();  
builder.setSingleChoiceItems(R.array.hobby, 0, choiceListener);  
                  
DialogInterface.OnClickListener btnListener =  new DialogInterface.OnClickListener() {  
         @Override  
         public void onClick(DialogInterface dialogInterface, int which) {  
                              
              int choiceWhich = choiceListener.getWhich();  
              String hobbyStr = getResources().getStringArray(R.array.hobby)[choiceWhich];  
          }  
};  
builder.setPositiveButton("确定", btnListener);

该监听器的实现如下:

private class ChoiceOnClickListener implements DialogInterface.OnClickListener {  
  
        private int which = 0;  
        @Override  
        public void onClick(DialogInterface dialogInterface, int which) {  
            this.which = which;  
        }  
          
        public int getWhich() {  
            return which;  
        }  
    }


顶部