2 markdowneide markdowneide 于 2013.03.25 13:53 提问

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) in the type AlertDialog.Builder is not applicable for the arguments (String[], int, View.OnClickListener) MainActivity.java /master/src/com/example/hotdog_master line 98 Java Problem

2) View.OnClickListener(){} must implement the inherited abstract method View.OnClickListener.onClick(View) MainActivity.java /master/src/com/example/hotdog_master line 104 Java Problem

请问是哪里出错了呢?

2个回答

nana4ever
nana4ever   2013.03.26 10:47
已采纳

如下修改 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
  }
CSDN_Ask
CSDN_Ask   2013.04.02 14:54

把您的代码改为:

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);
            }
          };
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!