自定义对话框
xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#11ffffff">
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="警告"
android:textColor="#38ADFF"
android:textSize="16sp"/>
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_gravity="center"
android:text="保护视力,少玩手机"/>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="15dp"
android:background="#E4E4E4"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<Button
android:id="@+id/no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:background="@null"
android:gravity="center"
android:lines="1"
android:text="取消"
android:textColor="#7D7D7D"
android:textSize="16sp"/>
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#E4E4E4"/>
<Button
android:id="@+id/yes"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:background="@null"
android:gravity="center"
android:lines="1"
android:text="确定"
android:textColor="#38ADFF"
android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
自定义dialog的代码
自定义dialog需要继承dialog,在重写oncreate方法,在方法中定义自定义的布局。
public class MyDialog extends Dialog {
private TextView title;
private TextView message;
private Button no;
private Button yes;
private String titletext;
private String messagetext;
private String notext;
private String yestext;
public MyDialog(@NonNull Context context) {
super(context);
}
//创建接口
public interface NoClick{
void noClick();
}
protected interface YesClick{
void yesClick();
}
//初始化接口
private NoClick noClick;
private YesClick yesClick;
//定义接口方法
public MyDialog setNoClick(NoClick noClick) {
this.noClick = noClick;
return this;
}
public MyDialog setYesClick(YesClick yesClick) {
this.yesClick = yesClick;
return this;
}
//初始化
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
title = (TextView) findViewById(R.id.title);
message = (TextView) findViewById(R.id.message);
no = (Button) findViewById(R.id.no);
yes = (Button) findViewById(R.id.yes);
//如果修改显示的文字
if (titletext!=null){
title.setText(titletext);
}
if (messagetext!=null){
message.setText(messagetext);
}
if (notext!=null){
no.setText(notext);
}
if (yestext!=null){
yes.setText(yestext);
}
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
noClick.noClick();
dismiss();
}
});
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yesClick.yesClick();
dismiss();
}
});
}
//接收要改成的内容
public void setTitle(String title) {
titletext=title;
}
public void setMessage(String message) {
messagetext=message;
}
public void setNo(String no) {
notext=no;
}
public void setYes(String yes) {
yestext=yes;
}
}
主页面代码
public void mydialog(View view) {
final MyDialog myDialog = new MyDialog(MainActivity.this);
myDialog.setTitle("shishi");
myDialog.setMessage("ggggg");
myDialog.setNo("不是");
myDialog.setYes("是");
myDialog.show();
myDialog.setYesClick(new MyDialog.YesClick() {
@Override
public void yesClick() {
Toast.makeText(MainActivity.this, "bushi", Toast.LENGTH_SHORT).show();
}
});
myDialog.setNoClick(new MyDialog.NoClick() {
@Override
public void noClick() {
Toast.makeText(MainActivity.this, "bu", Toast.LENGTH_SHORT).show();
}
});
}