Android 单击NeutralButton时如何避免隐藏(或取消)AlertDialog?
我正在尝试使用一个多选项对话框,如下代码所示。我想用一个中性按钮来取消选择列表中的所有项目。但是,当单击对话框上的某个按钮时,对话框立即消失,我认为这一定是默认操作。但我想保留它,因为我认为用户并不期望这样的行为。单击对话框上的按钮时,是否可以避免对话框消失,或者是否应该创建自定义对话框Android 单击NeutralButton时如何避免隐藏(或取消)AlertDialog?,android,Android,我正在尝试使用一个多选项对话框,如下代码所示。我想用一个中性按钮来取消选择列表中的所有项目。但是,当单击对话框上的某个按钮时,对话框立即消失,我认为这一定是默认操作。但我想保留它,因为我认为用户并不期望这样的行为。单击对话框上的按钮时,是否可以避免对话框消失,或者是否应该创建自定义对话框 protected Dialog onCreateDialog( int index) { return new AlertDialog.Builder(this) .setT
protected Dialog onCreateDialog( int index)
{
return new AlertDialog.Builder(this)
.setTitle( "title" )
.setMultiChoiceItems(items, selections, new DialogInterface.OnMultiChoiceClickListener(){
@Override
public void onClick( DialogInterface dialog, int clicked, boolean selected ) { }
})
.setPositiveButton( "OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id)
{
//Do something
}
})
.setNeutralButton( "Deselect all", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id)
{
//Do something
}
})
.create();
}
提前感谢yokyo,您最好创建一个自定义对话框。您可以使用任何活动,只需将活动的主题设置为theme.Dialog。即,在您的舱单中:
<activity android:theme="@android:style/Theme.Dialog">
如果使用这些setButton方法中的任何一种添加按钮,则该对话框将在处理程序运行后关闭。所以你有两个选择。创建你自己的对话框,让按钮按你想要的方式工作。或者,由于您使用的是列表,因此可以在AlertDialog上调用getListView,并使用按钮调用addFooterView。尽管使用其他按钮,该解决方案可能看起来很奇怪。我通过以下方法实现了选择/取消选择所有项目。这个想法来自Rpond的建议,谢谢Rpond
--/res/layout/dialog\u footer.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footerRoot"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="@+id/selectAllButton"
android:text="@string/select_all"
style="@style/FooterButton" />
</LinearLayout>
这正是我想要的。
yokyo谢谢Mayra,我会挖掘这个方法。谢谢Rpond。你向我保证没有正常的方法可以避免这种行为。我将介绍如何创建自定义对话框方法。
protected Dialog onCreateDialog( int index)
{
AlertDialog builder= new AlertDialog.Builder(this)
.setTitle( "title" )
.setMultiChoiceItems(items, selections, new DialogInterface.OnMultiChoiceClickListener(){
@Override
public void onClick( DialogInterface dialog, int clicked, boolean selected ) { }
})
.create();
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dialog_footer, (ViewGroup)findViewById(R.id.footerRoot));
Button selectAllButton = (Button)layout.findViewById(R.id.selectAllButton);
selectAllButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
Log.v("Test", "hello");
}
});
builder.setView(layout);
return builder;
}