package?com.example.administrator.dialog; import?android.app.AlertDialog; import?android.content.DialogInterface; import?android.support.v7.app.ActionBarActivity; import?android.os.Bundle; import?android.view.Menu; import?android.view.MenuItem; import?android.view.View; import?android.widget.Button; import?android.widget.EditText; import?android.widget.Toast; public?class?MainActivity?extends?ActionBarActivity?{ ????private?Button?btn_okcancel; ????private?Button?btn_radio; ????private?Button?btn_checkbox; ????private?Button?btn_item; ????private?Button?btn_custom; ????private?String[]?radioStrs?=?new?String[]{"男","女","保密"}; ????private?String[]?interestStrs?=?new?String[]{"運動","電玩","旅行","閱讀","電影"}; ????@Override ????protected?void?onCreate(Bundle?savedInstanceState)?{ ????????super.onCreate(savedInstanceState); ????????setContentView(R.layout.activity_main); ????????btn_okcancel?=?(Button)findViewById(R.id.btn_okcancel); ????????btn_radio?=?(Button)findViewById(R.id.btn_radio); ????????btn_checkbox?=?(Button)findViewById(R.id.btn_checkbox); ????????btn_item?=?(Button)findViewById(R.id.btn_item); ????????btn_custom?=?(Button)findViewById(R.id.btn_custom); ????????btn_okcancel.setOnClickListener(new?View.OnClickListener()?{ ????????????@Override ????????????public?void?onClick(View?v)?{ ????????????????AlertDialog.Builder?ok_cancel_dialog?=?new?AlertDialog.Builder(MainActivity.this); ????????????????ok_cancel_dialog.setIcon(R.mipmap.ic_launcher); ????????????????ok_cancel_dialog.setTitle("確認下班嗎?"); ????????????????ok_cancel_dialog.setMessage("你真的確認下班嗎?"); ????????????????ok_cancel_dialog.setNegativeButton("左",?new?DialogInterface.OnClickListener()?{ ????????????????????@Override ????????????????????public?void?onClick(DialogInterface?dialog,?int?which)?{ ????????????????????} ????????????????}); ????????????????ok_cancel_dialog.setPositiveButton("右",?new?DialogInterface.OnClickListener()?{ ????????????????????@Override ????????????????????public?void?onClick(DialogInterface?dialog,?int?which)?{ ????????????????????} ????????????????}); ????????????????ok_cancel_dialog.setNeutralButton("中",?new?DialogInterface.OnClickListener()?{ ????????????????????@Override ????????????????????public?void?onClick(DialogInterface?dialog,?int?which)?{ ????????????????????} ????????????????}); ????????????????AlertDialog?dialog?=?ok_cancel_dialog.create(); ????????????????dialog.show(); ????????????} ????????}); ????????btn_radio.setOnClickListener(new?View.OnClickListener()?{ ????????????@Override ????????????public?void?onClick(View?v)?{ ????????????????AlertDialog.Builder?radio_dialog?=?new?AlertDialog.Builder(MainActivity.this); ????????????????radio_dialog.setIcon(R.mipmap.ic_launcher); ????????????????radio_dialog.setTitle("請選擇性別?"); //????????????????radio_dialog.setMessage("認真選擇哦?");?不可以設置這個屬性 ????????????????radio_dialog.setSingleChoiceItems(radioStrs,?0,?new?DialogInterface.OnClickListener()?{ ????????????????????@Override ????????????????????public?void?onClick(DialogInterface?dialog,?int?which)?{ ????????????????????} ????????????????}); ????????????????AlertDialog?dialog?=?radio_dialog.create(); ????????????????dialog.show(); ????????????} ????????}); ????????btn_checkbox.setOnClickListener(new?View.OnClickListener()?{ ????????????@Override ????????????public?void?onClick(View?v)?{ ????????????????AlertDialog.Builder?checkbox_dialog?=?new?AlertDialog.Builder(MainActivity.this); ????????????????checkbox_dialog.setIcon(R.mipmap.ic_launcher); ????????????????checkbox_dialog.setTitle("請選擇興趣愛好?"); //????????????????checkbox_dialog.setMessage("可以選擇多個哦?");?不可以設置這個屬性 ????????????????checkbox_dialog.setMultiChoiceItems(interestStrs,?null,?new?DialogInterface.OnMultiChoiceClickListener()?{ ????????????????????@Override ????????????????????public?void?onClick(DialogInterface?dialog,?int?which,?boolean?isChecked)?{ ????????????????????} ????????????????}); ????????????????AlertDialog?dialog?=?checkbox_dialog.create(); ????????????????dialog.show(); ????????????} ????????}); ????????btn_checkbox.setOnClickListener(new?View.OnClickListener()?{ ????????????@Override ????????????public?void?onClick(View?v)?{ ????????????????AlertDialog.Builder?checkbox_dialog?=?new?AlertDialog.Builder(MainActivity.this); ????????????????checkbox_dialog.setIcon(R.mipmap.ic_launcher); ????????????????checkbox_dialog.setTitle("請選擇興趣愛好?"); //????????????????checkbox_dialog.setMessage("可以選擇多個哦?");?不可以設置這個屬性 ????????????????checkbox_dialog.setMultiChoiceItems(interestStrs,?null,?new?DialogInterface.OnMultiChoiceClickListener()?{ ????????????????????@Override ????????????????????public?void?onClick(DialogInterface?dialog,?int?which,?boolean?isChecked)?{ ????????????????????} ????????????????}); ????????????????AlertDialog?dialog?=?checkbox_dialog.create(); ????????????????dialog.show(); ????????????} ????????}); ????????btn_item.setOnClickListener(new?View.OnClickListener()?{ ????????????@Override ????????????public?void?onClick(View?v)?{ ????????????????AlertDialog.Builder?items_dialog?=?new?AlertDialog.Builder(MainActivity.this); ????????????????items_dialog.setIcon(R.mipmap.ic_launcher); ????????????????items_dialog.setTitle("請選擇興趣愛好?"); //????????????????checkbox_dialog.setMessage("可以選擇多個哦?");?不可以設置這個屬性 ????????????????items_dialog.setItems(interestStrs,?new?DialogInterface.OnClickListener()?{ ????????????????????@Override ????????????????????public?void?onClick(DialogInterface?dialog,?int?which)?{ ????????????????????} ????????????????}); ????????????????AlertDialog?dialog?=?items_dialog.create(); ????????????????dialog.show(); ????????????} ????????}); ????????btn_custom.setOnClickListener(new?View.OnClickListener()?{ ????????????@Override ????????????public?void?onClick(View?v)?{ ????????????????final?View?view?=?View.inflate(MainActivity.this,?R.layout.edit_dialog,?null); ????????????????AlertDialog.Builder?custom_dialog?=?new?AlertDialog.Builder(MainActivity.this); ????????????????custom_dialog.setIcon(R.mipmap.ic_launcher); ????????????????custom_dialog.setTitle("請輸入您的年齡?"); ????????????????custom_dialog.setNeutralButton("確定",?new?DialogInterface.OnClickListener()?{ ????????????????????@Override ????????????????????public?void?onClick(DialogInterface?dialog,?int?which)?{ ????????????????????????EditText?editText?=?(EditText)view.findViewById(R.id.text); ????????????????????????Toast.makeText(MainActivity.this,?editText.getText()?,Toast.LENGTH_LONG).show(); ????????????????????} ????????????????}); ????????????????custom_dialog.setView(view); ????????????????AlertDialog?dialog?=?custom_dialog.create(); ????????????????dialog.show(); ????????????} ????????}); ????} }