程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> dialog-點擊按鈕後顯示AlertDialog

dialog-點擊按鈕後顯示AlertDialog

編輯:編程綜合問答
點擊按鈕後顯示AlertDialog

想用一個單選按鈕顯示對話框,但是點擊按鈕對話框卻不顯示。

代碼如下,不知道哪裡出現錯誤。

謝謝

public class MainPage extends Activity{
Button start;
private static final int DIALOG_SINGLE_CHOICE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainpage);
    start = (Button) findViewById(R.id.start);
    start.setBackgroundResource(R.drawable.read);
    start.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {
            showDialog(DIALOG_SINGLE_CHOICE );
        }
    });
}
@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
    id = DIALOG_SINGLE_CHOICE;
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Choose your option");
    builder.setSingleChoiceItems(R.array.Baani, 0, new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
        }
    })
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked Yes so do some stuff */
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked No so do some stuff */
                }
            })
           .create();
    return super.onCreateDialog(id);
        }   
  }
}

最佳回答:


public class MainActivity extends Activity implements OnClickListener{
private Button button;
private static final int DIALOG_SINGLE_CHOICE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button  = (Button) findViewById(R.id.button);
    button.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button:
        showDialog(DIALOG_SINGLE_CHOICE);
        break;

    default:
        break;
    }
}

@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Choose your option");
    builder.setSingleChoiceItems(R.array.Baani, 0, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
        }

    }).setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            /* User clicked Yes so do some stuff */
        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            /* User clicked No so do some stuff */
        }
    }).create().show();
    return super.onCreateDialog(id);
}

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved