Android中AlertDialog等的使用
来源:互联网 发布:软件安装助理 编辑:程序博客网 时间:2024/06/05 12:47
package com.example.administrator.imooc;import android.app.DatePickerDialog;import android.content.DialogInterface;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.DatePicker;import android.widget.Toast;import java.util.Calendar;/** 这个代码是主要学习了三个dialog的使用,以后不会的时候回来看。*/public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button setDate,single,muti; private AlertDialog alertDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout); init(); } public void init(){ setDate=(Button) findViewById(R.id.btn_setting); single=(Button) findViewById(R.id.btn_single); muti=(Button) findViewById(R.id.btn_muti); setDate.setOnClickListener(this); single.setOnClickListener(this); muti.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_setting://设置日期 showDatePickerDialog(); break; case R.id.btn_single://设置单选 showSingleListDialog(); break; case R.id.btn_muti://设置多选 showMutiListDialog(); break; } } public void showDatePickerDialog(){ Calendar c=Calendar.getInstance(); /*datepicker的参数*/ new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { Toast.makeText(MainActivity.this,"日期为:"+ year+"年"+(month+1)+"月"+(dayOfMonth)+"日",Toast.LENGTH_SHORT).show(); } },c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show(); } public void showSingleListDialog(){ AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("性别选择"); builder.setIcon(android.R.drawable.btn_star); final String[] sex=getResources().getStringArray(R.array.sex); //setSingleChoiceItems(数据资源id,表示默认被选中的item的下标 -1不存在默认选中, // 点击单选item的点击事件) builder.setSingleChoiceItems(R.array.sex, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { /*toast的时候千万别忘了最后的show*/ Toast.makeText(MainActivity.this,"您的性别是:"+sex[which],Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); /*一定要写这一句不然前面的都是白写*/ builder.create().show(); } public void showMutiListDialog(){ AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("选择你的爱好"); builder.setIcon(android.R.drawable.btn_star); final String[] loves=getResources().getStringArray(R.array.loves);//获取爱好的列表 final boolean[] checkedItems=new boolean[loves.length]; //setMultiChoiceItems(数据元的资源id,boolean类型数组 默认被选中 true选中 false未选中 builder.setMultiChoiceItems(R.array.loves, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkedItems[which]=isChecked; // Toast.makeText(MainActivity.this,"您的爱好是:"+loves[which],Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override /*因为有多个选项,所以通过一个列表把所有的选项都存起来,最后输出*/ public void onClick(DialogInterface dialog, int which) { StringBuilder sb=new StringBuilder(); for(int i=0;i<checkedItems.length;i++){ if(checkedItems[i]){ sb.append(loves[i]); sb.append("\n"); } } Toast.makeText(MainActivity.this,"您的爱好是:"+sb.toString(),Toast.LENGTH_SHORT).show(); } }); builder.create().show(); }}
0 0
- Android中AlertDialog等的使用
- Android中AlertDialog的使用
- Android中AlertDialog的使用
- Android 中 AlertDialog的使用
- Android中AlertDialog的使用
- Android中AlertDialog使用
- Android中AlertDialog和Toast的使用
- Android中AlertDialog和Toast的使用
- Android中AlertDialog和Toast的使用
- Android中AlertDialog和Toast的使用
- Android中AlertDialog对话框的使用
- Android中AlertDialog的简单使用
- Android中AlertDialog和ProgressDialog的使用
- android studio中AlertDialog的使用
- Android中AlertDialog(对话框)的使用
- android alertDialog的使用
- Android中自定义AlertDialog使用
- Android中自定义AlertDialog使用
- MySql过程语句范例
- 张瑞峰商城项目视频
- 单链表冒泡、选择排序的c语言实现
- linux学习笔记(十)
- Redis数据可持续化
- Android中AlertDialog等的使用
- Android 自定义覆盖层控件,悬浮窗控件。
- 动态规划-最长公共子序列
- 信号处理——Hilbert端点效应浅析
- AAC格式ADTS
- MySQL必知必会-14MySQL组合查询
- uva 10940Throwing cards away II
- NO.13 linux的脚本编写
- Adding .jar's to classpath (Scala)