我正在嘗試查看單選按鈕的對話框,但是當我單擊按鈕時,警告對話框從不出現。以下是代碼。請讓我知道是否有任何替代解決方案。這個樣本來自API演示。當點擊按鈕時顯示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);
}
}
}
'.create()'是不必要的。在Builder上調用'.show()'將會創建它並將其顯示在一行代碼中。使用'.create()'的唯一原因是你可以在其他地方多次調用'.show()',但在這種情況下它不是必需的。 – TronicZomB