0

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

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) AlertDialog.Builder 类型中的 setSingleChoiceItems(int, int, DialogInterface.OnClickListener) 不适用于参数 (String[], int, View.OnClickListener) MainActivity.java /master/src/com/example/hotdog_master 第 98 行Java 问题

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

你能帮助理解什么是错的吗?

4

2 回答 2

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

  }
于 2013-01-02T12:41:09.287 回答
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 来保持差异化

于 2013-01-02T12:40:27.057 回答