带有Android复选框的对话框

时间:2013-01-02 12:37:11

标签: android

我找到了带有复选框的对话框示例:

final int DIALOG_ITEMS = 1;
    String data[] = { "one", "two", "three", "four" };
    @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button but1=(Button) findViewById(R.id.button1);
            but1.setOnClickListener(new OnClickListener() 
            {
                @Override
                public void onClick(View arg0) 
                {
                     showDialog(DIALOG_ITEMS);
                }
            }); 
        }

        protected Dialog onCreateDialog(int id) {
            AlertDialog.Builder adb = new AlertDialog.Builder(this);

              adb.setTitle(R.string.items);
              adb.setSingleChoiceItems(data, -1, myClickListener);


          }


          OnClickListener myClickListener = new OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
              ListView lv = ((AlertDialog) dialog).getListView();
              if (which == Dialog.BUTTON_POSITIVE)
                Log.d(LOG_TAG, "pos = " + lv.getCheckedItemPosition());
              else
                Log.d(LOG_TAG, "which = " + which);
            }
          };

Eclipse关注两个错误:

  

1)   setSingleChoiceItems(int,int,DialogInterface.OnClickListener)中的   类型AlertDialog.Builder不适用于参数   (String [],int,   View.OnClickListener)MainActivity.java / master / src / com / example / hotdog_master line   98 Java问题

     

2)   View.OnClickListener(){}必须实现继承的抽象方法   View.OnClickListener.onClick(查看)MainActivity.java / master / src / com / example / hotdog_master line   104 Java问题

你能帮忙解决一下是什么问题吗?

2 个答案:

答案 0 :(得分:1)

更改上面的代码如下:

DialogInterface.OnClickListener myClickListener = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
              ListView lv = ((AlertDialog) dialog).getListView();
              if (which == Dialog.BUTTON_POSITIVE)
                Log.d(LOG_TAG, "pos = " + lv.getCheckedItemPosition());
              else
                Log.d(LOG_TAG, "which = " + which);
            }
          };

您已经为Button Click导入了View.OnClickListener,并且eclipse正在检测下面的onClickListener也作为View.onClickListener()。因此,通过添加DialogInterface.OnClickListener

来保持差异化

答案 1 :(得分:1)

将OnClickListener更改为:

  DialogInterface.OnClickListener myClickListener = new 
                                   DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
              ListView lv = ((AlertDialog) dialog).getListView();
              if (which == Dialog.BUTTON_POSITIVE)
                Log.d(LOG_TAG, "pos = " + lv.getCheckedItemPosition());
              else
                Log.d(LOG_TAG, "which = " + which);
            }
          };

并创建AlertDialog.Builder实例:

AlertDialog.Builder adb = new AlertDialog.Builder(Your_Current_Activity.this);

编辑:

onCreateDialog方法更改为:

protected Dialog onCreateDialog(int id) {
    AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);

      adb.setTitle(R.string.items);
      adb.setSingleChoiceItems(data, -1, myClickListener);

       AlertDialog dialog = builder.create();
       dialog.show();
      return super.onCreateDialog(id);  //<<<< add return here

  }