博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
安卓对话框
阅读量:4884 次
发布时间:2019-06-11

本文共 7478 字,大约阅读时间需要 24 分钟。

MainActivity.java

package com.chuanxidemo.shaoxin.demo03;import android.app.AlertDialog;import android.app.Dialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;import android.widget.RelativeLayout;import java.util.ArrayList;import java.util.List;public class MainActivity extends BaseActivity {    private RelativeLayout activityMain;    private Button btn1;    private Button btn2;    private Button btn3;    private Button btn4;    private Button btn5;    private Button btn6;    private AlertDialog.Builder builder;    private int numId;    private List data;    private int num;    private ProgressDialog progressDialog;    @Override    protected void setContentView() {        setContentView(R.layout.activity_main);    }    @Override    protected void setListener() {        btn1.setOnClickListener(this);        btn2.setOnClickListener(this);        btn3.setOnClickListener(this);        btn4.setOnClickListener(this);        btn5.setOnClickListener(this);        btn6.setOnClickListener(this);    }    @Override    protected void init() {        btn1 = (Button) findViewById(R.id.btn_1);        btn2 = (Button) findViewById(R.id.btn_2);        btn3 = (Button) findViewById(R.id.btn_3);        btn4 = (Button) findViewById(R.id.btn_4);        btn5 = (Button) findViewById(R.id.btn_5);        btn6 = (Button) findViewById(R.id.btn_6);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.btn_1:                builder = new AlertDialog.Builder(this);                builder.setTitle("简单对话框");                builder.setMessage("我是简单对话框");                builder.create().show();                break;            case R.id.btn_2:                builder = new AlertDialog.Builder(this);                builder.setTitle("普通对话框");                builder.setMessage("我是普通对话框");                builder.setPositiveButton("确认", null);                builder.setNegativeButton("取消", null);                builder.setNeutralButton("查看", null);                builder.setCancelable(false);                builder.create().show();                break;            case R.id.btn_3:                builder = new AlertDialog.Builder(this);                builder.setTitle("单选对话框");                builder.setIcon(R.mipmap.ic_launcher);                builder.setSingleChoiceItems(R.array.citysName, 0, new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        numId = which;                    }                });                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        Log.v("msg", getResources().getStringArray(R.array.citysName)[numId]);                    }                });                builder.setCancelable(false);                builder.create().show();                break;            case R.id.btn_4:                data = new ArrayList();                builder = new AlertDialog.Builder(this);                builder.setTitle("多选对话框");                builder.setIcon(R.mipmap.ic_launcher);                data.clear();                builder.setMultiChoiceItems(R.array.citysName, new boolean[]{
false, false, false}, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { String[] citys = getResources().getStringArray(R.array.citysName); if (isChecked) { data.add(citys[which]); } else { data.remove(citys[which]); } } }); builder.setNeutralButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Log.v("msg", data.toString()); } }); builder.create().show(); break; case R.id.btn_5: final Dialog dialog = new Dialog(this); dialog.setTitle("自定义对话框"); LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.diy_dialog, null); dialog.setContentView(view); Button login = (Button) view.findViewById(R.id.login); dialog.show(); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); break; case R.id.btn_6: progressDialog = new ProgressDialog(this); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setTitle("进度条对话框"); progressDialog.setMax(100); progressDialog.create(); progressDialog.show(); new MyThread(progressDialog).start(); break; } } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); progressDialog.setProgress(msg.arg1); } }; public class MyThread extends Thread { ProgressDialog progressDialog; public MyThread(ProgressDialog progressDialog) { this.progressDialog = progressDialog; } @Override public void run() { while (true) { try { Thread.sleep(100); Message message = new Message(); handler.sendEmptyMessage(0); if (num <= progressDialog.getMax()) { num = progressDialog.getProgress(); num++; message.arg1 = num; handler.sendMessage(message); }else { progressDialog.dismiss(); break; } } catch (InterruptedException e) { e.printStackTrace(); } } } }}

BaseActivity.java

package com.chuanxidemo.shaoxin.demo02;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.view.View;/** * Created by shaoxin on 2017/2/21. */public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {        @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView();        init();        setListener();    }    protected abstract void setContentView();    protected abstract void setListener();    protected abstract void init();}

activity_main.xml

 

转载于:https://www.cnblogs.com/ShaoXin/p/6428031.html

你可能感兴趣的文章
Pthon3各平台的安装
查看>>
python编程快速上手之第11章实践项目参考答案(11.11.3)
查看>>
JS 之CLASS类应用
查看>>
一个tga工具
查看>>
64bit CPU 知识 (IA32,IA64,EM64T,AMD64)
查看>>
结构体 枚举
查看>>
srtlen实现以及与sizeof的比较
查看>>
linux+win7双系统重装win7修复grub的办法
查看>>
让应用在横屏模式下启动
查看>>
Intent传递list集合时异常解决
查看>>
登录验证码demo-java
查看>>
日常练习 1.0
查看>>
php集成环境
查看>>
Ubuntu下的负载均衡Web集群配置
查看>>
Create a site by Google Site - All Free
查看>>
Fragment 的基本使用
查看>>
mvc的个别对输入数据的验证
查看>>
autoit学习安装说明及例子
查看>>
jQuery控制form表单元素聚焦
查看>>
wpf+.net 4.5 surface2.0 = 异步多点触控 时间轴 part1
查看>>