Android带Radio选项、确定、取消按钮的上下文菜单的实现

来源:互联网 发布:腾讯云绑定域名有冲突 编辑:程序博客网 时间:2024/06/14 10:10

    在应用的实现过程中,需要弹出上下文对话框选择,提供用户选择的Radio,还有确定和取消按钮。


1、在布局文件中增加

android:onClick="onShowDialogClick1" 


2、源码中增加对应的响应函数,以下代码是我的项目中用到的,只做参考

     public void onShowDialogClick1(View v){    
             AlertDialog.Builder builder = new AlertDialog.Builder(this);  
             EmptyListener l = new EmptyListener();  
             SyncListener synclis=new SyncListener(0);//0表示上传
             this.allOrfresh=1;
             
             //设置Dialog的标题  
             builder.setTitle(R.string.card_select);  
             //设置可供选择的ListView  
             builder.setSingleChoiceItems(R.array.select_backups,1, new DialogInterface.OnClickListener() {  
                 //@Override  
                 public void onClick(DialogInterface dialog, int which) {  
                     //which是选中的位置(基于0的)  
                     if(which==0)
                     {
                         allOrfresh=0;
                     }else
                     {
                         allOrfresh=1;
                     }
                     String[] items = getResources().getStringArray(R.array.select_backups);  
                     Toast.makeText(AndroidSyncPhonebook.this, items[which], Toast.LENGTH_LONG).show();  
                 }  
             });  
            
             builder.setPositiveButton(R.string.dialog_ok, synclis);  
             builder.setNegativeButton(R.string.dialog_cancel, l);
             AlertDialog ad = builder.create();   
             ad.show();    
        } 



     //空的监听类  
     private class EmptyListener implements DialogInterface.OnClickListener{  
         //@Override  
             public void onClick(DialogInterface dialog, int which) {  
            }  
        } 


     private class SyncListener implements DialogInterface.OnClickListener {     
         private int idx;
         private int syncMode = 0;
         private int m_count = 0;
         private int i=0;
         public SyncListener(int idx) {
             this.idx = idx;
         }
         public void onClick(DialogInterface dialog, int which) {    
             Log.d("d","allOrfresh="+allOrfresh);
           //  Log.debug(TAG, "Clicked on item: " + idx + " hasFocus=" + v.hasFocus());
                if(this.idx==0) {
                    if(allOrfresh==0){ }
                    else{ }
                }
                else if(this.idx==1) { }
                else if(this.idx==2) { }
                 Log.d("d","syncmode="+syncMode);
                 homeScreenController.buttonPressed(0,syncMode);
         }
    }





原创粉丝点击