想用一個單選按鈕顯示對話框,但是點擊按鈕對話框卻不顯示。
代碼如下,不知道哪裡出現錯誤。
謝謝
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);
}
}