天天看點

自定義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接口回調就到這,當然,這上面隻是舉一個例子,具體的可以參考源代碼 點選下載下傳源代碼 覺得有用的小夥伴可以頂一下^_^