AlertDialog的简单封装

<span style="color:#000000;">//在fragment中调用</span>
<span style="color:#000000;">public class CeshiFragment extends Fragment</span><span style="color:#33ff33;"> </span>{
    private int a[]={R.id.tv_air_unlimited,R.id.tv_air_economics,
            R.id.tv_air_super,R.id.tv_air_official,R.id.tv_air_frist};
    private String b[]={"舱位不限","经济舱","超级经济舱","公务舱","头等舱"};
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ceshi, container, false);
        initView(view);
        return view;
    }
    private void initView(View view) {
        tv_sj = (TextView) view.findViewById(R.id.tv_sj);
        tv_sj.setText("测试数据测试数据测试数据测试数据测试数据");
        //dialog的使用
        tv_sj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialogUtils alertDialogUtils=new AlertDialogUtils(getActivity(),R.layout.alert_dialog_item);
                for(int i=0;i<a.length;i++){
                    alertDialogUtils.setText(a[i], b[i], new ClickListener(i));
                }

//                ClickListener[] lls = new ClickListener[a.length];
//                for(int i = 0; i < lls.length; i++){
//                    lls[i] = new ClickListener(i);
//                }
//              alertDialogUtils.setText( a ,b , lls);
                alertDialogUtils.setOnClickListener(new  AlertDialogUtils.DialogClickListener() {

                    @Override
                    public void clickYes() {
                    }
                    @Override
                    public void clickNo() {
                    }
                });
            }
        });
       
    }

     private class ClickListener implements View.OnClickListener{
        int index;
        private ClickListener(int index){
            this.index = index;
        }
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(),b[index]+"",Toast.LENGTH_SHORT).show();
        }
    }

}

你可能感兴趣的:(AlertDialog的简单封装)