Android 如何创建自定义对话框
我需要一个对话框在我的应用程序,它是多样化的外观,我的意思是,例如,一次我需要一个对话框,只有一个按钮,另一次我需要它有两个按钮。此外,我希望它的一些功能,如它的图像是不同的,取决于它的使用!有谁能建议我如何实施它? 对于我的解决方案,我搜索了一些网站,并根据下面链接中的代码实现了CustomDialogClass中实现的对话框,但它没有显示对话框,有人可以帮助我如何显示它吗Android 如何创建自定义对话框,android,Android,我需要一个对话框在我的应用程序,它是多样化的外观,我的意思是,例如,一次我需要一个对话框,只有一个按钮,另一次我需要它有两个按钮。此外,我希望它的一些功能,如它的图像是不同的,取决于它的使用!有谁能建议我如何实施它? 对于我的解决方案,我搜索了一些网站,并根据下面链接中的代码实现了CustomDialogClass中实现的对话框,但它没有显示对话框,有人可以帮助我如何显示它吗 您可以为每个对话框添加不同的布局。一个布局为1个按钮,另一个布局为按钮和图像等 示例: final Di
您可以为每个对话框添加不同的布局。一个布局为1个按钮,另一个布局为按钮和图像等 示例:
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom); //the layout of your custom dialog
dialog.setTitle("Title...");
dialog.show();
您可以在
对话框
中找到有关如何实施操作的更多信息,我认为您需要的是对话框
,而不是主题为活动的对话框。当这种情况发生时,每次要显示对话框时都必须启动(新)活动
相反,您可以使用对话框
,如:
// custom dialog
final Dialog dialog = new Dialog(context);
//specify to not display the default title/header to customize the whole layout
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.setContentView(R.layout.custom_layout_here);
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText(message);
//from the following part- buttons can be managed for your custom layout
Button dialogButton = (Button) dialog.findViewById(R.id.buttonOk);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
//if you want another button, add it here, just like the one above
//play with the visibility of button(or components) to hide or show acc to your requirement
dialog.show();
final Dialog d=新建对话框(DisplayUserData.this);
d、 requestWindowFeature(窗口。功能\u无\u标题)//之前
d、 setContentView(R.layout.edit\u msg\u对话框);
ImageView ivCross=(ImageView)d.findViewById(R.id.ivCross_msg);
最终EditText et_消息=(EditText)d.findViewById(R.id.etMessage);
TextView tv_OK=(TextView)d.findViewById(R.id.tvOkButton);
TextView tv_SKIP=(TextView)d.findViewById(R.id.tvSkipButton);
tv_OK.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(et_Message.getText().toString().length()>=40){
Toast.makeText(getApplicationContext(),“您不能输入超过40个字符。”,Toast.LENGTH_SHORT).show();
}else if(et_Message.getText().toString().length()<1){
Toast.makeText(getApplicationContext(),“请输入内容”,Toast.LENGTH\u SHORT.show();
}否则{
d、 解雇();
msgwrited=et_Message.getText().toString();
submittData();
}
}
});
ivCross.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
d、 取消();
}
});
tv_SKIP.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
d、 取消();
submittData();
}
});
d、 show();
d、 getWindow().setBackgroundDrawable(新的ColorDrawable(android.graphics.Color.TRANSPARENT));
WindowManager.LayoutParams lp=d.getWindow().getAttributes();
lp.dimAmount=0.7f;
d、 getWindow().setAttributes(lp);
试试android对话框库。使用非常简单,活动中只需几行代码
Pop.on(this)
.with()
.title(R.string.title) // if non needed skip this
.layout(R.layout.custom_pop)
.when(new Pop.Yah() { // if not needed skip this
@Override
public void clicked(DialogInterface dialog, View view) {
Toast.makeText(getBaseContext(), "Yah button clicked", Toast.LENGTH_LONG).show();
}
})
.when(new Pop.Nah() { // if not needed, skip this
@Override
public void clicked(DialogInterface dialog, View view) {
Toast.makeText(getBaseContext(), "Nah button clicked", Toast.LENGTH_LONG).show();
}
}).show();
谢谢你的解决方案,你是说我创建了一个方法,其中的内容就是你提到的代码?每次我想调用对话框时,我应该在我的应用程序中调用它?是的,取决于你想要一个或两个按钮的时间,你可以将上面的代码放入一个函数中,带有标志,取决于你将显示哪些按钮,并传递上下文、消息和其他需要设置的参数,这会起作用
Pop.on(this)
.with()
.title(R.string.title) // if non needed skip this
.layout(R.layout.custom_pop)
.when(new Pop.Yah() { // if not needed skip this
@Override
public void clicked(DialogInterface dialog, View view) {
Toast.makeText(getBaseContext(), "Yah button clicked", Toast.LENGTH_LONG).show();
}
})
.when(new Pop.Nah() { // if not needed, skip this
@Override
public void clicked(DialogInterface dialog, View view) {
Toast.makeText(getBaseContext(), "Nah button clicked", Toast.LENGTH_LONG).show();
}
}).show();