程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-Android 中帶復選框的對話框

android-Android 中帶復選框的對話框

編輯:編程綜合問答
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

請問是哪裡出錯了呢?

最佳回答:


如下修改 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
  }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved