自定义xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#12B6FF"
android:paddingBottom="100dp">
<TextView
android:id="@+id/tv_bt"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="评价一下吧!"
android:gravity="center"
android:textSize="20sp"
android:textColor="#ffffff"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_ts"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="喜欢吗?给个五星好评,鼓励\n我们会做的更好"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt_no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="我要吐槽"
android:textColor="@drawable/textcolor" />
<Button
android:id="@+id/bt_ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="五星好评"
android:textColor="@drawable/textcolor"/>
</LinearLayout>
</LinearLayout>
自定义Java代码:
public class DivDialog extends Dialog {
private TextView tvBt;
private TextView tvTs;
private Button btNo;
private Button btOk;
private String btStr;
private String tsStr;
private String noStr;
private String yesStr;
private onYesListener onYesListener;
private onNoListener onNoListener;
public void setOnYesListener(DivDialog.onYesListener onYesListener) {
this.onYesListener = onYesListener;
}
public void setOnNoListener(DivDialog.onNoListener onNoListener) {
this.onNoListener = onNoListener;
}
public void setBtStr(String btStr) {
this.btStr = btStr;
}
public void setTsStr(String tsStr) {
this.tsStr = tsStr;
}
public void setNoStr(String noStr) {
this.noStr = noStr;
}
public void setYesStr(String yesStr) {
this.yesStr = yesStr;
}
public DivDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.div_layout);
tvBt = (TextView) findViewById(R.id.tv_bt);
tvTs = (TextView) findViewById(R.id.tv_ts);
btNo = (Button) findViewById(R.id.bt_no);
btOk = (Button) findViewById(R.id.bt_ok);
if(btStr != null){
tvBt.setText(btStr);
}
if(tsStr != null){
tvTs.setText(btStr);
}
if(noStr != null){
btNo.setText(noStr);
}
if(yesStr != null){
btOk.setText(yesStr);
}
btNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNoListener.onNoClick();
dismiss();
}
});
btOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onYesListener.onYesClick();
dismiss();
}
});
}
interface onYesListener{
public void onYesClick();
};
interface onNoListener{
public void onNoClick();
};
}
实现Java代码:
DivDialog divDialog = new DivDialog(MainActivity.this);
divDialog.setOnNoListener(new DivDialog.onNoListener() {
@Override
public void onNoClick() {
Toast.makeText(MainActivity.this, "憋着", Toast.LENGTH_SHORT).show();
}
});
divDialog.setOnYesListener(new DivDialog.onYesListener() {
@Override
public void onYesClick() {
Toast.makeText(MainActivity.this, "谢谢你的好评,我们会继续努力", Toast.LENGTH_SHORT).show();
}
});
divDialog.show();