AlertDialog如何获取用户所选择的按钮

fanbaobao 发布于 2014/07/31 15:15
阅读 2K+
收藏 0
是获取这里面的按钮,然后触发相应的事件 

加载中
1
wangxigui
wangxigui

为你的单选框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;  
        }  
    }


返回顶部
顶部