应用程序启动正常,但是当我单击 Select_Players 按钮时,我的设备上没有出现对话框。这是代码:
public class MainActivity extends Activity {
private Button selectPlayers;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
super.onStart(); //customize
super.onResume(); //customize
selectPlayers = (Button) findViewById(R.id.add_players);
selectPlayers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Launch dialogbox on click
onCreateDialog(savedInstanceState);
}
});
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
@SuppressWarnings("rawtypes")
final ArrayList mSelectedItems = new ArrayList(); // Where we track the selected items
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Set the dialog title
builder.setTitle(R.string.select_players)
// Specify the list array, the items to be selected by default (null for none),
// and the listener through which to receive callbacks when items are selected
.setMultiChoiceItems(R.array.players_name, null,
new DialogInterface.OnMultiChoiceClickListener() {
@SuppressWarnings("unchecked")
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(which);
} else if (mSelectedItems.contains(which)) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(Integer.valueOf(which));
}
}
})
// Set the action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//CODE TO CLOSE DIALOGBOX AND START FORGE
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//CODE TO JUST CLOSE DIALOGBOX
}
});
return builder.create();
}
}
我看到 Dialog 方法返回一个 Dialog 但我不知道如何使它显示为 onClick 的结果?(作为参考,我从 Android 开发网站获取了 Dialog 方法。)
谢谢!