我想问一下,我是否可以在对话框的标题中点击一个图标。 我使用以下行在对话框的左侧设置标题“New Contact”,在右侧设置一个图标。那么,这个图标(x5)可以通过某种方式变为可点击吗?我希望它像关闭按钮一样运作。
dialog.requestWindowFeature(Window.FEATURE_RIGHT_ICON);
dialog.setTitle("New Contact ");
dialog.setContentView(R.layout.pop_up_new_contact);//loads the layout we have already create pop_up_new_contact.xml
dialog.setFeatureDrawableResource(Window.FEATURE_RIGHT_ICON, R.drawable.x5);//adds an icon in the left of the Title.
dialog.setCancelable(true);
答案 0 :(得分:1)
我找到了解决方案!这就是我所做的,以防有人需要它。
@Override
protected Dialog onCreateDialog(int id) {//creates the dialog
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature( Window.FEATURE_CUSTOM_TITLE );//adds in the dialog box a frame for the custom title
@Override
protected Dialog onCreateDialog(int id) {//creates the dialog
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature( Window.FEATURE_CUSTOM_TITLE );//adds in the dialog box a frame for the custom title
//dialog.setTitle("New Contact");//sets the title of the dialog box
dialog.setContentView(R.layout.pop_up_new_contact);//loads the layout we have already create pop_up_new_contact.xml
dialog.getWindow().setFeatureInt( Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_dialog_box);//sets our custom title(custom_title_dialog_box.xml) as the current title of the dialog box. The custom_title_dialog_box.xml contains a TextView with id: title_new_contact and an ImageView with id: x1_button.
dialog.setCancelable(true);//close the dialog box when the back key is pushed.
ImageView x1_button = (ImageView)dialog.findViewById(R.id.x1_button);
x1_button.setOnClickListener(new View.OnClickListener(){
public void onClick(View View3) {
dialog.dismiss();
}
});
return dialog;
}
也许这不是最好的解决方案,但至少它有效!
答案 1 :(得分:0)
最好创建一个自定义对话框。但其糟糕的用户体验设计将其用作关闭按钮,因为Android用户习惯于后退做同样的操作。
答案 2 :(得分:0)
我确实发现这并不是你所要求的,但是如果你正在寻找一个前瘾按钮。这将有效。
dialog.setButton(getText(R.string.cancel), new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton) { }
});