gpt4 book ai didi

android - AlertDialog - 不要关闭项目点击

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:34:56 34 4
gpt4 key购买 nike

好的,所以我正在创建一个 ArrayAdapter 并在我的警报对话框中使用它,因为我不想在 SingleItemSelection 对话框中显示默认单选按钮。

相反,我想更改所选项目的背景,然后当用户按下肯定按钮时,我将执行与所选项目相关的操作。

    private void showAlertDialog()
{
final String[] options = getResources().getStringArray(R.array.dialog_options);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, options);

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("My Dialog");

dialogBuilder.setAdapter(adapter, new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(getApplicationContext(), "item clicked at index " + which, Toast.LENGTH_LONG).show();
// Here I need to change the background color of the item selected and prevent the dialog from being dismissed
}
});

//String strOkay = getString(R.string.okay);
dialogBuilder.setPositiveButton("OK", null); // TODO
dialogBuilder.setNegativeButton("Cancel", null); // nothing simply dismiss

AlertDialog dialog = dialogBuilder.create();
dialog.show();
}

我要解决两个问题。

How do I prevent the dialog from being dismissed when the user clicks on an item

How do I change the background of the item that has been selected when the user clicks on it

最佳答案

要防止在单击项目时关闭对话框,您可以使用 AdapterView.OnItemClickListener 而不是 DialogInterface.OnClickListener。

像这样:

dialogBuilder.setAdapter(adapter, null);
...
AlertDialog dialog = dialogBuilder.create();
alertDialog.getListView().setOnItemClickListener(
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// do your stuff here
}
});

关于android - AlertDialog - 不要关闭项目点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20057944/

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