标签:对话框 oncreatedialog 自定义dialog dismissdialog onpreparedialog
public class MainActivity extends Activity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt1 = (Button) findViewById(R.id.bt1); Button bt2 = (Button) findViewById(R.id.bt2); Button bt3 = (Button) findViewById(R.id.bt3); Button bt4 = (Button) findViewById(R.id.bt4); Button bt5 = (Button) findViewById(R.id.bt5); Button bt6 = (Button) findViewById(R.id.bt6); Button bt7 = (Button) findViewById(R.id.bt7); bt1.setOnClickListener(this); bt2.setOnClickListener(this); bt3.setOnClickListener(this); bt4.setOnClickListener(this); bt5.setOnClickListener(this); bt6.setOnClickListener(this); bt7.setOnClickListener(this); } protected void dialog() { AlertDialog.Builder builder = new Builder(MainActivity.this); builder.setMessage("确认退出吗?"); builder.setTitle("提示"); builder.setPositiveButton("确认", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); MainActivity.this.finish(); } }); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } protected void dialog2() { Dialog dialog = new AlertDialog.Builder(this) .setIcon(android.R.drawable.btn_star).setTitle("喜好调查") .setMessage("你喜欢周星星的电影吗?") .setPositiveButton("很喜欢", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "我很喜欢他的电影。", Toast.LENGTH_LONG).show(); } }).setNegativeButton("不喜欢", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "我不喜欢他的电影。", Toast.LENGTH_LONG).show(); } }).setNeutralButton("一般", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "谈不上喜欢不喜欢。", Toast.LENGTH_LONG).show(); } }).create(); dialog.show(); } protected void dialog3() { new AlertDialog.Builder(this).setTitle("请输入") .setIcon(android.R.drawable.ic_dialog_info) .setView(new EditText(this)).setPositiveButton("确定", null) .setNegativeButton("取消", null).show(); } protected void dialog4() { new AlertDialog.Builder(this) .setTitle("复选框") .setMultiChoiceItems(new String[] { "Item1", "Item2" }, null, null).setPositiveButton("确定", null) .setNegativeButton("取消", null).show(); } protected void dialog5() { new AlertDialog.Builder(this) .setTitle("单选框") .setIcon(android.R.drawable.ic_dialog_info) .setSingleChoiceItems(new String[] { "Item1", "Item2" }, 0, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).setNegativeButton("取消", null).show(); } protected void dialog6() { new AlertDialog.Builder(this).setTitle("列表框") .setItems(new String[] { "Item1", "Item2" }, null) .setNegativeButton("确定", null).show(); } protected void dialog7() { LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.dialog, (ViewGroup) findViewById(R.id.dialog)); new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout) .setPositiveButton("确定", null).setNegativeButton("取消", null) .show(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt1: dialog(); break; case R.id.bt2: dialog2(); break; case R.id.bt3: dialog3(); break; case R.id.bt4: dialog4(); break; case R.id.bt5: dialog5(); break; case R.id.bt6: dialog6(); break; case R.id.bt7: dialog7(); break; default: break; } } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.dialogtest.MainActivity" > <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定取消对话框" /> <Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="三个按钮对话框" /> <Button android:id="@+id/bt3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="带文本框的对话框" /> <Button android:id="@+id/bt4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="单选对话框" /> <Button android:id="@+id/bt5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="复选对话框" /> <Button android:id="@+id/bt6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="列表对话框" /> <Button android:id="@+id/bt7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自定义对话框 " /> </LinearLayout>
版权声明:本文为博主原创文章,未经博主允许不得转载。
Android 对话框(Dialog) 及 自定义Dialog
标签:对话框 oncreatedialog 自定义dialog dismissdialog onpreparedialog
原文地址:http://blog.csdn.net/a78270528/article/details/47754851