gpt4 book ai didi

Android 设置 OnClick 不工作/对话框不出现

转载 作者:行者123 更新时间:2023-11-30 03:27:41 25 4
gpt4 key购买 nike

应用程序启动正常,但当我单击 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 方法。)

谢谢!

最佳答案

此方法返回一个对话框,因此您必须在您的 onClick 中构建一个这样的对话框

     Dialog d = onCreateDialog(savedInstanceState);
d.show();

我认为您曾尝试过重写 Activity 方法 onCreateDialog(),但您必须以另一种方式进行,如下所示:

http://www.mysamplecode.com/2011/11/android-alertdialog-example-showdialog.html

关于Android 设置 OnClick 不工作/对话框不出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17971493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com