Android中各类Dialog实例——交互
今天打开技术博客发现自己的上一篇技术博客已经是去年11月份的了。但凡是生活中或是学习中的许多事情我们一直都有在做,只是并没有一直在记录。有时候脑袋真的很像存储机制,即使脑容量有着科学研究的浩如宇宙,但大脑之所谓有“大”来修饰,终归还是会有溢出的时候。所以对于学习生活中运行过的“程序”所残留的内存,要么释放,要么记录加以保管,而技术博客正是这样的存在。
做技术的不空谈,现在开始。。。
在androidApp的开发中会遇到许多需要人机交互的组件,借此获取用户所作出的选择或是反馈。这样的一些组件到底有多少种又该如何实例呢? 看下图:
而这些组件各自的效果 如下图:
进行插入图片操作时很想吐槽该网站。。。。。
具体代码实现如下:
package com.example.dialogtest;import java.util.ArrayList;import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.Toast;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;public class MainActivity extends Activity implements Runnable {//Runnable线程接口 下面写进度条选择框时模拟使用private Button btn_diaNormal; private Button btn_diaMulti;private Button btn_diaList;private Button btn_diaSinChos;private Button btn_diaMultiChos;private Button btn_diaProcess;private Button btn_diaReadProcess;private Button btn_diaCustom;private Button btn_popUpDia;private PopupWindow window=null;private Button cusPopupBtn1;private View popupView;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getView();setListener();}private void getView(){btn_diaNormal=(Button)findViewById(R.id.btn_diaNormal);btn_diaMulti=(Button)findViewById(R.id.btn_diaMulti);btn_diaList=(Button)findViewById(R.id.btn_diaList);btn_diaSinChos=(Button)findViewById(R.id.btn_diaSigChos);btn_diaMultiChos=(Button)findViewById(R.id.btn_diaMultiChos);btn_diaProcess=(Button)findViewById(R.id.btn_diaProcess);btn_diaReadProcess=(Button)findViewById(R.id.btn_diaReadProcess);btn_diaCustom=(Button)findViewById(R.id.btn_diaCustom);btn_popUpDia=(Button)findViewById(R.id.btn_popUpDia);}private void setListener(){final Button Button[] = {btn_diaNormal,btn_diaMulti,btn_diaList,btn_diaSinChos, btn_diaMultiChos,btn_diaProcess,btn_diaReadProcess,btn_diaCustom,btn_popUpDia};for(int i = 0;i<9;i++){Button[i].setOnClickListener(btnListener);}}private Button.OnClickListener btnListener= new Button.OnClickListener(){public void onClick(View v){if(v instanceof Button){int btnId=v.getId();switch(btnId){case R.id.btn_diaNormal:showNormalDia();break;case R.id.btn_diaMulti:showMultiDia();break;case R.id.btn_diaList:showListDia();break;case R.id.btn_diaSigChos:showSinChosDia();break;case R.id.btn_diaMultiChos:showMultiChosDia();break;case R.id.btn_diaReadProcess:showReadProcess();break;case R.id.btn_diaProcess:showProcessDia();break;case R.id.btn_diaCustom:showCustomDia();break;case R.id.btn_popUpDia:showCusPopUp(v);break;default:break;}}}};/*普通的对话框*/private void showNormalDia(){AlertDialog.Builder bui=new AlertDialog.Builder(MainActivity.this);bui.setIcon(R.drawable.ic_launcher);bui.setTitle("普通的对话框");bui.setMessage("这是普通对话框中的message内容");bui.setPositiveButton("YES", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) showClickMessage("您点按了确定");}});bui.setNegativeButton("NO", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {showClickMessage("您点按了取消");}});bui.create().show();//创建并显示}/*多按钮对话框*/private void showMultiDia(){AlertDialog.Builder multiDia=new AlertDialog.Builder(MainActivity.this);multiDia.setTitle("多选项对话框");multiDia.setPositiveButton("Button_1", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {showClickMessage("您点按了按钮一");}});multiDia.setNeutralButton("Button_2", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {showClickMessage("您点按了按钮二");}});multiDia.setNegativeButton("Button_3", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {showClickMessage("您点按了按钮三");}});multiDia.create().show();//创建并显示}/*列表对话框*/private void showListDia(){ //声明一个存放选项的数组final String[] mList={"选项1","选项2","选项3","选项4","选项5","选项6","选项7"};AlertDialog.Builder listDia=new AlertDialog.Builder(MainActivity.this);listDia.setTitle("这是一个列表对话框");listDia.setItems(mList, new DialogInterface.OnClickListener() {//setItems(队列对象,监听器);public void onClick(DialogInterface dialog, int which) {/*下标是从0开始的*/showClickMessage(mList[which]);}});listDia.create().show();//创建并显示}/*单项选择对话框*/int yourChose;private void showSinChosDia(){ //声明一个存放选项的数组final String[] mList={"选项1","选项2","选项3","选项4","选项5","选项6","选项7"};yourChose=-1;AlertDialog.Builder sinChosDia=new AlertDialog.Builder(MainActivity.this);sinChosDia.setTitle("单项选择对话框");sinChosDia.setSingleChoiceItems(mList, 0, new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {yourChose=which;}});sinChosDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {if(yourChose!=-1){showClickMessage(mList[yourChose]);}}});sinChosDia.create().show();//创建并显示}/*多项选择对话框*/ArrayList myChose= new ArrayList();private void showMultiChosDia(){final String[] mList={"选项1","选项2","选项3","选项4","选项5","选项6","选项7"};final boolean mChoseSts[]={false,false,false,false,false,false,false};myChose.clear();//初始化数组队列AlertDialog.Builder multiChosDia=new AlertDialog.Builder(MainActivity.this);multiChosDia.setTitle("多项选择对话框");multiChosDia.setMultiChoiceItems(mList, mChoseSts, new DialogInterface.OnMultiChoiceClickListener() {public void onClick(DialogInterface dialog, int which, boolean isChecked) {if(isChecked){myChose.add(which);}else{myChose.remove(which);}}});multiChosDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {int size=myChose.size();String str="";for(int i=0;i
还有相应的两个布局文件,即自定义对话框和popup window实现的对话框中的布局文件:
至此,android中各类Dialog的实例已经全贴上去了。希望我的总结对大家有所帮助。
晴时有风阴有时雨,听说~下雨天技术博客和敲代码更配哦。。。
梣梓cenzi
2015 4 5
标签:
相关文章
-
无相关信息