Android如何实现对话框
扫描二维码
随时随地手机看文章
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(); } }); } }