定义布局
<?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="wrap_content"
android:background="@drawable/dialog_bg"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="14dp"
android:textColor="@color/login_hint"
android:textSize="@dimen/text_size_18" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="14dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp" >
<TextView
android:id="@+id/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@drawable/btn_confirm_selector"
android:gravity="center"
android:textColor="@color/white"
android:textSize="@dimen/text_size_16" />
<TextView
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@drawable/btn_cancel_selector"
android:gravity="center"
android:textColor="@color/login_hint"
android:textSize="@dimen/text_size_16" />
</LinearLayout>
</LinearLayout>
自定义新的类
package com.corn.widget;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import com.corn.R;
public class ConfirmDialog extends Dialog {
private Context context;
private String title;
private String confirmButtonText;
private String cacelButtonText;
private ClickListenerInterface clickListenerInterface;
public interface ClickListenerInterface {
public void doConfirm();
public void doCancel();
}
public ConfirmDialog(Context context, String title, String confirmButtonText, String cacelButtonText) {
super(context, R.style.MyDialog);
this.context = context;
this.title = title;
this.confirmButtonText = confirmButtonText;
this.cacelButtonText = cacelButtonText;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
init();
}
public void init() {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.confirm_dialog, null);
setContentView(view);
TextView tvTitle = (TextView) view.findViewById(R.id.title);
TextView tvConfirm = (TextView) view.findViewById(R.id.confirm);
TextView tvCancel = (TextView) view.findViewById(R.id.cancel);
tvTitle.setText(title);
tvConfirm.setText(confirmButtonText);
tvCancel.setText(cacelButtonText);
tvConfirm.setOnClickListener(new clickListener());
tvCancel.setOnClickListener(new clickListener());
Window dialogWindow = getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
dialogWindow.setAttributes(lp);
}
public void setClicklistener(ClickListenerInterface clickListenerInterface) {
this.clickListenerInterface = clickListenerInterface;
}
private class clickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
switch (id) {
case R.id.confirm:
clickListenerInterface.doConfirm();
break;
case R.id.cancel:
clickListenerInterface.doCancel();
break;
}
}
};
}
使用
public static void Exit(final Context context) {
final ConfirmDialog confirmDialog = new ConfirmDialog(context, "确定要退出吗?", "退出", "取消");
confirmDialog.show();
confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
@Override
public void doConfirm() {
// TODO Auto-generated method stub
confirmDialog.dismiss();
//toUserHome(context);
AppManager.getAppManager().AppExit(context);
}
@Override
public void doCancel() {
// TODO Auto-generated method stub
confirmDialog.dismiss();
}
});
}
转载自 http://www.cnblogs.com/lwbqqyumidi/p/3645561.html
感谢 博客园 CORN