Android自定义对话框

         一般来说每个APP都有自己统一的样式或主题,比如整个应用对话框的主题都是一样的,而每次弹出对话框显示都要写那段无聊的代码,所以我们有必要自己写一个自定义的对话框类,根据自己所要设置的内容通过工具类把它调用出来显示,就可以轻松实现对话框功能,代码复用性也更加好。微笑

         首先我们要新建一个类去继续Dialog

       

public class MCustomDialog extends Dialog implements OnClickListener{
    private Context context;
	private TextView dailog_tv;
	private Button positive_Btn,negative_Btn;
	private MyOnClickListener onClickListener;
    /**
     * @param onClickListener:一个接口:实现自定义的方法
     */
	public void setOnClickListener(MyOnClickListener onClickListener) {
		this.onClickListener = onClickListener;
	}
	/**
	 * 构造器
	 * @param context 上下文
	 * @param theme 主题
	 */
	public MCustomDialog(Context context,int theme) {
		super(context,theme);
		this.context = context;
		init();
		initListener();
	}
	/**
	 * 初始化监听
	 */
	public void initListener() {
		positive_Btn.setOnClickListener(this);
		negative_Btn.setOnClickListener(this);
	}
	/**
	 * 设置正按钮内容
	 * @param content
	 */
	public void setPosiContent(String content){
		if(positive_Btn != null){
		positive_Btn.setText(content);
	   }
	}
	
	/**
	 * 设置负按钮内容
	 * @param content
	 */
	public void setNegaContent(String content){
		if(negative_Btn != null){
			negative_Btn.setText(content);
	   }
	}
	private void init() {
		View view = View.inflate(context, R.layout.view_custom_dialog, null);
		dailog_tv = (TextView)view.findViewById(R.id.dialog_content_tv);
		positive_Btn = (Button)view.findViewById(R.id.dialog_positive_btn);
		negative_Btn = (Button)view.findViewById(R.id.dialog_negative_btn);
		
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				DisplayUtils.dip2px(context, 300),DisplayUtils.dip2px(context, 150)
				);
		this.setContentView(view,params);
	}
	
	/***
	 * 设置所需的内容
	 * @param content:设置的内容
	 */
	public void setContent(String content){
		if(dailog_tv != null && content != null){
		dailog_tv.setText(content);
		}
	}
	
	/**
	 * 实现点击事件
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.dialog_positive_btn:
			if(onClickListener!=null){
			onClickListener.onPositiveListener();
			}
			MCustomDialog.this.dismiss();
			break;
		case R.id.dialog_negative_btn:
			if(onClickListener!=null){
				onClickListener.onNagetiveListener();
		    }
			MCustomDialog.this.dismiss();
			break;

		default:
			break;
		}
		
	}
	  /**
	    * 自定义一个接口
	    * @author Administrator
	    * 两个方法:接口回调,调用两个按钮的点击事件实现方法
	    */
		public interface MyOnClickListener{
			 void onPositiveListener();
			 void onNagetiveListener();
		}
  
}

对话框的按钮点击通过接口暴露到外面,功能逻辑由用户自己实现。

布局文件

<?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="150dp"
    android:background="@drawable/defaulting_dialog_bg"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/dialog_content_tv"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:gravity="center"
        android:text="是不是真要离开?"
        android:textColor="#3F3B3F"
        android:textSize="20sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="#e7e7e7" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <Button
            android:id="@+id/dialog_negative_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:gravity="center"
            android:text="取消"
            android:textColor="#848484" />

        <View
            android:layout_width="1px"
            android:layout_height="match_parent"
            android:background="#e7e7e7" />

        <Button
            android:id="@+id/dialog_positive_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:text="确定"
            android:gravity="center"
            android:textColor="#D26638" />
    </LinearLayout>

</LinearLayout>
建立一个对话框工具类去实现对话框的调用,用到了方法重载

public class DialogUtils {
    private static MCustomDialog dialog;
    /**
	 * @param context
	 * @param content:对话框显示的文本
	 * @param onMClickListner:对话框回调接口
	 * 显示默认对话框
	 */
    //com.example.radiogroup.R.style.myDialogTheme
    public static void showDialog(Context context,String content,MyOnClickListener onClickListener){
    	showDialog(context, content, onClickListener, null, null);
    }
    
    /**
     * 显示对话框
     * @param context
     * @param content
     * @param onClickListener
     * @param positiveContent
     * @param negativiContent
     */
    public static void showDialog(Context context,String content,MyOnClickListener onClickListener,String positiveContent,String negativiContent){
    	dialog = new MCustomDialog(context, com.example.radiogroup.R.style.myDialogTheme);
    	dialog.setContent(content);
    	dialog.setPosiContent(positiveContent);
    	dialog.setNegaContent(negativiContent);
    	dialog.setOnClickListener(onClickListener);
    	dialog.show();
    }
    
}
以后每次调用就可以showDialog就可以了,这样就可以更好的管理了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章