問題描述
我想創(chuàng)建一個(gè)自定義對(duì)話框.所以我創(chuàng)建了一個(gè)模板dialog_change"并打開了對(duì)話框.
I want to create a custom dialog. So i create a template 'dialog_change' and I open the dialog.
Dialog myDialog = new Dialog(Overview.this);
myDialog.setContentView(R.layout.dialog_change);
myDialog.setTitle("My Custom Dialog Title");
myDialog.show();
現(xiàn)在我想在底部添加兩個(gè)按鈕(一個(gè)正按鈕和一個(gè)負(fù)按鈕).我該怎么做?
Now i want to add two button (one positive and one negative button), at the bottom. How can i do that?
推薦答案
我只是制作你自己的自定義類來(lái)模擬一個(gè) AlertDialog,這樣你就可以使用你自己的布局而不附加任何字符串.(如果您想要一個(gè)完全樣式化的 AlertDialog,則存在一些無(wú)法完全擺脫框架的奇怪問題).類似這樣的東西,但您可以隨意擴(kuò)展它:
I'd just make your own custom class to simulate an AlertDialog, this way you can use your own layout with no strings attached. (There are some weird issues where you can't fully get rid of the frame if you want a fully styled AlertDialog). Something like this, but you can expand this as fully as you want:
public class CustomDialog extends Dialog {
private Button positive, negative;
public CustomDialog(Context context) {
super(context);
initialize(context);
}
protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
initialize(context);
}
public CustomDialog(Context context, int theme) {
super(context, theme);
initialize(context);
}
private void initialize(Context c) {
//Inflate your layout, get a handle for the buttons
positive = (Button)layout.findViewById(R.id.positive):
negative = (Button)layout.findViewById(R.id.negative):
positive.setVisibility(View.GONE);
negative.setVisibility(View.GONE);
}
public void setPositiveButton(String buttonText, View.OnClickListener listener) {
positive.setText(buttonText);
positive.setOnClickListener(listener);
positive.setVisibility(View.VISIBLE);
}
public void setNegativeButton(String buttonText, View.OnClickListener listener) {
negative.setText(buttonText);
negative.setOnClickListener(listener);
negative.setVisibility(View.VISIBLE);
}
}
這篇關(guān)于如何在自定義對(duì)話框中創(chuàng)建正面和負(fù)面按鈕的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!