Alert

xiaoxiao2021-02-27  640

package com.bwei.dialog; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private int num; @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); bt1.setOnClickListener(this); bt2.setOnClickListener(this); bt3.setOnClickListener(this); bt4.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt1:    //弹出普通的对话框 //创建对话框 AlertDialog.Builder builder = new AlertDialog.Builder(this); //设置对话框的图片 builder.setIcon(R.drawable.ic_launcher); //设置对话框的标题 builder.setTitle("这是皇家小树林,,"); //设置对话框的内容 builder.setMessage("禁止把妹,嘿嘿"); /** * 积极的 */ builder.setPositiveButton("知道了", new AlertDialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"知道了" , 0).show(); } }); builder.setNegativeButton("没听见", new AlertDialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"没听见" , 0).show(); } }); //展示对话框 builder.show(); break; case R.id.bt2:    //弹出单选对话框 final String[] itmes = {"鲍鱼","满汉全席","小鸡炖蘑菇","大虾"}; AlertDialog.Builder singlelBuilder = new AlertDialog.Builder(this); // singlelBuilder.setIcon(android.R.drawable.btn_dialog); singlelBuilder.setTitle("你想吃的美食"); /** * 第一个参数是对话的item * 第二个参数是选中的id * 第三个监听事件 */ singlelBuilder.setSingleChoiceItems(itmes, 1, new AlertDialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, itmes[which], 0).show(); } }); singlelBuilder.show(); break; case R.id.bt3:  //多选 AlertDialog.Builder MutilsBuilder = new AlertDialog.Builder(this); // MutilsBuilder.setIcon(android.R.drawable.btn_dialog); MutilsBuilder.setTitle("你喜欢的女神有哪些"); final String[] beautilsGirl = {"杨玉环","西施","汤唯","王威","风姐"}; boolean [] checkedItems = {false,false,false,true,true}; MutilsBuilder.setMultiChoiceItems(beautilsGirl, checkedItems, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(MainActivity.this, beautilsGirl[which], 0).show(); } }); MutilsBuilder.show(); break; case R.id.bt4:   //进度对话框 final ProgressDialog progress = new ProgressDialog(this); progress.setTitle("我是进度对话框"); //设置进度对话框的样式 progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //设置进度的最大值 progress.setMax(100); progress.show(); new Thread(){ public void run() { while(num<=100){ progress.setProgress(num); num+=10; SystemClock.sleep(200); } }; }.start(); break; default: break; } } }
转载请注明原文地址: https://www.6miu.com/read-616.html

最新回复(0)