Dialog详解

转载请注明出处:Dialog详解-陈小武
dialog共有8种
1、普通的dialog

AlertDialog.Builder(context).setTitle("").setMessage("").setIco (资源 id).create().show();

2、带按钮的dialog

dialog=new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("title").setMessage("content");
dialog.setPositiveButton("Ok", new OnClickListener() {}
dialog.setNegativeButton("Cancel", new OnClickListener() {}
dialog.setNeutralButton("稍后提醒", new OnClickListener() {}
dialog.create().show();

3、可配置试图的其他dialog

dialog=new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("title").setMessage("content");
dialog.setItems(R.array.colors, new OnClickListener(){}}//带单选列表
dialog.create().show();

4、配置adapter的dialog

dialog=new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("title").setMessage("content");
SimpleAdapter adapter=new SimpleAdapter(MainActivity.this, list,R.layout.dialog_item,new String[]{""},new int[]{R.id.textView});
dialog.setAdapter(adapter, new OnClickListener() {}//添加适配器的dialog
dialog.create().show();

5、选则列表dialog

dialog=new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("title").setMessage("content");
//参数1、列表资源2、默认选中3、点击事件
dialog.setSingleChoiceItems(String[], 0, new OnClickListener() {});
//表示以boolean类型的数组标示item的选中情况,如果都没有选中可是设置null,如果需啊哟设置默认选中则需要将数组的长度与数据源的长度一致 true表示选中 false表示未选中
dialog.setMultiChoiceItems((String[], new boolean[]{false,false,true}, new OnMultiChoiceClickListener() {});
dialog.create().show();

6、日期dialog

DatePickerDialog dialog=new DatePickerDialog(MainActivity.this,
new OnDateSetListener() {
//表示日期时间设置回调的函数
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {}},2015,9,8);//(2015,9,8)年,月,日的初始化值
datePickerDialog.show();

7、时间dialog

TimePickerDialog dialog=new TimePickerDialog(MainActivity.this,
new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
}, 11, 39, false);//时,分,是否24小时初始化
dialog.show();

8、自定义dialog

1、简单自定义
dialog=new AlertDialog.Builder(MainActivity.this);
View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_dialog, null);
final EditText name=(EditText) view.findViewById(R.id.name);
final EditText pwd=(EditText) view.findViewById(R.id.pwd);
dialog.setView(view);//设置view
dialog.setPositiveButton("登录", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
    String name=name.getText().toString();
    String pwd=pwd.getText().toString();
}
});
2、完全自定义(抛弃dialog的格式,感觉现在都这么用)

先来张图


Dialog详解_第1张图片
QQ截图20170909103616.png
public class CommenDialog extends Dialog {
    private TextView dialog_cancel,dialog_sure,dialog_title,dialog_content;
    private OnDialogListener mListener;//定义接口处理点点击事件
    public interface OnDialogListener{
        void setPositive();
        void setNegative();
    }

    public void  setOnDialogListener(OnDialogListener listener){
        mListener = listener;
    }

    public CommenDialog(Context context) {
        super(context);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_commen);
        setCanceledOnTouchOutside(false);
        initView();
        initData();
        setListener();
    }
    private void initView(){
        dialog_title = (TextView) findViewById(R.id.dialog_title);
        dialog_content = (TextView) findViewById(R.id.dialog_content);
        dialog_cancel = (TextView) findViewById(R.id.dialog_cancel);
        dialog_sure = (TextView) findViewById(R.id.dialog_sure);
    }
    private String strContent = "确认放弃吗?";
    private String strPositive = "确定";
    private String strNegative = "取消";
    private void initData(){
        if (dialog_cancel != null) {
            dialog_cancel.setText(strNegative);
        }
        if (dialog_sure != null){
            dialog_sure.setText(strPositive);
        }
        if (dialog_content !=null ){
            dialog_content.setText(strContent);
        }
    }
    public void setPositiveText(String str){
        strPositive = str; //这里你不能使用dialog_sure.setText(str);
    }
    public void setNegativeText(String str){
        strNegative = str;//同上
    }
    public void setDialogContent(String str){
        strContent = str;//同上
    }
    //设置监听
    private void setListener(){
        dialog_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.setNegative();
            }
        });
        dialog_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.setPositive();
            }
        });
    }
}

    
    
    
        
        
    

你可能感兴趣的:(Dialog详解)