天天看点

自定义Dialog接口回调参数

      基本上所有的app项目都会有自定义dialog,但有时如果把dialog单独写成一个类,我们就需要考虑到如何将dialog的参数回调到activity或者fragment,当然有很多方法,这里主要讲的是通过接口回调的方式来把dialog中的参数传递到activity或者fragment,主要讲述的是一个接口回调的思想,这种思想同样可以应用到项目中的其他地方,其次这里也阐述了如何将一个字符串中的某部分文字变色,当然这是从项目中抽取出来的一部分,好了,一起来看下效果图吧:

自定义Dialog接口回调参数
自定义Dialog接口回调参数
自定义Dialog接口回调参数

        1、如何实现改变一个字符串中的部分文字的颜色

              1)、可以使用Github 项目上一个开源项目 

              2)、可以使用html代码,在这里我使用的是该方法

String s= "累计本次您已请假<font color =\"#FFCB22\">"
                + one + "</font>次,<font color = \"#FFCB22\" >" + two + "</font>天;剩余请假<font color = \"#FFCB22\">" + three + "</font>次,<font color = \"#FFCB22\">" + four + "</font>天。是否提交申请?")
           

                         然后  contentTv.setText(Html.fromHtml(s)) 传入该字符串即可

          2、在xml 代码里根据自己的需求定义布局,注意的是dialog的圆角是通过自定义的shaper 来进行实现的

自定义Dialog接口回调参数
这里relativelayout_backgound_shape_left.xml的代码如下       
<?xml version="1.0" encoding="utf-8"?>
         <selector
           xmlns:android="http://schemas.android.com/apk/res/android">
               <item android:state_pressed="true" android:drawable="@drawable/shape_gray_dialog_right_15" />
               <item android:state_pressed="false" android:drawable="@drawable/shape_white_dialog_15"/>
        </selector>
    
           
relativelayout_backgound_shape_right.xml的代码如下:
       
<?xml version="1.0" encoding="utf-8"?>
        <selector
           xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:state_pressed="true" android:drawable="@drawable/shape_gray_dialog_left_15" />
            <item android:state_pressed="false" android:drawable="@drawable/shape_white_dialog_15"/>
        </selector>
           
shape_gray_dialog_left_15.xml的代码如下:  
       
<?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
           <corners android:bottomRightRadius="@dimen/distance_15"/>
           <solid android:color="@color/gray_e5e5e5" />
        </shape>
           
shape_gray_dialog_right_15.xml的代码如下:
<?xml version="1.0" encoding="utf-8"?>
       <shape xmlns:android="http://schemas.android.com/apk/res/android">

         <corners android:bottomLeftRadius="@dimen/distance_15"/>
         <solid android:color="@color/gray_e5e5e5" />
       </shape>
           
3、自定义一个DialogFactory,在里面实现代码
public static AlertDialog showEditDialog(final Context context, final ResultEditListener listener) {
        final AlertDialog alertDialog = new AlertDialog.Builder(context)
                .create();
        alertDialog.show();
        final Window window = alertDialog.getWindow();
        window.getDecorView().setBackgroundColor(context.getResources().getColor(R.color.transparent));
        window.setContentView(R.layout.course_buy_dialog);
        window.setLayout(
                window.getContext().getResources().getDisplayMetrics().widthPixels,
                WindowManager.LayoutParams.WRAP_CONTENT);
        window.setGravity(Gravity.CENTER);
        alertDialog.setCanceledOnTouchOutside(true);
        final EditText etContent = (EditText) window.findViewById(R.id.et_content);

        window.findViewById(R.id.tv_sure).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null) {
                            listener.SureListener(etContent.getText().toString().trim());
                        }
                    }
                });
        window.findViewById(R.id.tv_cancle).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (listener != null) {
                            listener.CancleListener();
                        }
                    }
                });
        return alertDialog;
    }
           
4、定义一个接口
public interface ResultEditListener {
        void SureListener(String s);
        void CancleListener();
     }
           
5、在activity或者fragment中进行调用,并实现对应的接口
public class MainActivity extends AppCompatActivity implementsView.OnClickListener ,DialogFactory.ResultEditListener{
    private AlertDialog alertDialog;
    private TextView tvEtShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        tvEtShow = (TextView) findViewById(R.id.tv_ed_show);
        tvEtShow.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_ed_show:
                alertDialog = DialogFactory.showEditDialog(MainActivity.this,MainActivity.this);
                break;
        }
    }

    /**
     * 弹出带有EditView的Dialog
     */
    @Override
    public void SureListener(String result) {
        Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
        alertDialog.dismiss();
    }

    @Override
    public void CancleListener() {
        Toast.makeText(this, "您点击了取消", Toast.LENGTH_SHORT).show();
        alertDialog.dismiss();
    }
}
           
好了,自定义Dialog接口回调就到这,当然,这上面只是举一个例子,具体的可以参考源代码 点击下载源代码 觉得有用的小伙伴可以顶一下^_^