自定义对话框
package com.bw.day01;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Dialog extends android.app.Dialog {
private TextView title;
private TextView xinxi;
private Button no;
private Button yes;
private String noStr;
private String yesStr;
private String titleStr;
private String messageStr;
public interface Noclicklistenner{
void onclick();
}
private Noclicklistenner noclicklistenner;
public void setNoclicklistenner(String str,Noclicklistenner noclicklistenner) {
if (str!=null){
noStr=str;
}
this.noclicklistenner = noclicklistenner;
}
public interface Yesclicklistenner{
void onclick();
}
private Yesclicklistenner yesclicklistenner;
public void setYesclicklistenner(String str,Yesclicklistenner yesclicklistenner) {
if(str!=null){
yesStr=str;
}
this.yesclicklistenner = yesclicklistenner;
}
public Dialog(@NonNull Context context) {
super(context);
}
public void setTitleStr(String titleStr) {
this.titleStr = titleStr;
}
public void setMessageStr(String messageStr) {
this.messageStr = messageStr;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
title = (TextView) findViewById(R.id.title);
xinxi = (TextView) findViewById(R.id.xinxi);
no = (Button) findViewById(R.id.no);
yes = (Button) findViewById(R.id.yes);
if (messageStr!=null){
xinxi.setText(messageStr);
}
if (titleStr!=null){
title.setText(titleStr);
}
if (noStr!=null){
no.setText(noStr);
}
if(yesStr!=null){
yes.setText(yesStr);
}
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (noclicklistenner!=null){
noclicklistenner.onclick();
}
}
});
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yesclicklistenner.onclick();
}
});
}
}
实现类
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Dialog dialog = new Dialog(this);
dialog.setMessageStr("wucoia");
dialog.setTitleStr("w无名之辈");
dialog.setNoclicklistenner("nononono",new Dialog.Noclicklistenner() {
@Override
public void onclick() {
}
});
dialog.setYesclicklistenner("yesyes",new Dialog.Yesclicklistenner() {
@Override
public void onclick() {
}
});
dialog.show();
}
}
来源:CSDN
作者:BananaSnake
链接:https://blog.csdn.net/BananaSnake/article/details/103734421