package com.su.testalldialog;import java.util.ArrayList;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.Context;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.Spinner;import android.widget.Toast;public class TestAllDialogActivity extends Activity { String[] weekdays; Context context; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); context = this; weekdays = new String[] { "Sun", "Mon", "Tue", "Wed" }; Button button1 = (Button) findViewById(R.id.button1); Button button2 = (Button) findViewById(R.id.button2); Button button3 = (Button) findViewById(R.id.button3); Button button4 = (Button) findViewById(R.id.button4); Button button5 = (Button) findViewById(R.id.button5); Button button6 = (Button) findViewById(R.id.button6); button6.setOnClickListener(listener); button5.setOnClickListener(listener); button1.setOnClickListener(listener); button2.setOnClickListener(listener); button3.setOnClickListener(listener); button4.setOnClickListener(listener); initSpinner(); initAutocompletetextview(); } private void initAutocompletetextview() { AutoCompleteTextView autocompletetextview = (AutoCompleteTextView) findViewById(R.id.autocompletetextview); ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, weekdays);// 实现简单的adapter autocompletetextview.setAdapter(adapter); } private void initSpinner() { Spinner spinner = (Spinner) findViewById(R.id.spinner); spinner.setOnItemSelectedListener(slistener); ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, weekdays);// 实现简单的adapter adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// 实现选项的样式 spinner.setAdapter(adapter); } private OnItemSelectedListener slistener = new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { toast(weekdays[arg2]); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }; int index = 0;// 用来标记哪一项被选择 private OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { Builder dialog = new AlertDialog.Builder(context); switch (v.getId()) { case R.id.button1: dialog.setIcon(R.drawable.ic_launcher); dialog.setTitle("简单选择对话框"); dialog.setItems(weekdays, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { toast(weekdays[which]); } }); dialog.show(); break; case R.id.button2: dialog.setIcon(R.drawable.ic_launcher); dialog.setTitle("单选对话框"); dialog.setSingleChoiceItems(weekdays, 1, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { index = which; } }); dialog.setPositiveButton("是", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { toast(weekdays[index]); } }); dialog.setNegativeButton("否", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { } }); dialog.show(); break; case R.id.button3: final ArrayList<String> multiWeekadys = new ArrayList<String>();// 选择的数据放到list中 dialog.setIcon(R.drawable.ic_launcher); dialog.setTitle("复选对话框"); final boolean[] checkeditems = new boolean[weekdays.length];// 设置一打开时的选择情况 dialog.setMultiChoiceItems(weekdays, checkeditems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { checkeditems[which] = true;// 获取一个被选中的列表 } } }); dialog.setPositiveButton("是", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { for (int j = 0; j < checkeditems.length; j++) {// 遍历一遍看那个是选中的 if (checkeditems[j]) { multiWeekadys.add(weekdays[j]); } } toast(multiWeekadys.toString()); } }); dialog.setNegativeButton("否", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { } }); dialog.show(); break; case R.id.button4: dialog.setIcon(R.drawable.ic_launcher); dialog.setTitle("简单警告"); dialog.setMessage("测试警告"); dialog.setPositiveButton("是", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { toast("是"); } }); dialog.setNeutralButton("中", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { toast("中"); } }); dialog.setNegativeButton("否", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { } }); dialog.show(); break; case R.id.button5: final EditText et = new EditText(context); et.setText("测试"); dialog.setIcon(R.drawable.ic_launcher); dialog.setTitle("简单警告"); dialog.setMessage("测试警告"); dialog.setView(et); dialog.setPositiveButton("是", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { toast(et.getText().toString()); } }); dialog.setNeutralButton("中", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { toast("中"); } }); dialog.setNegativeButton("否", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { } }); dialog.show(); break; case R.id.button6: final ImageView im = new ImageView(context); im.setImageResource(R.drawable.ic_launcher); dialog.setIcon(R.drawable.ic_launcher); dialog.setTitle("简单警告"); dialog.setMessage("测试警告"); dialog.setView(im); dialog.setPositiveButton("是", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { } }); dialog.setNeutralButton("中", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { toast("中"); } }); dialog.setNegativeButton("否", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { } }); dialog.show(); break; default: break; } } }; private void toast(String msg) { Toast.makeText(context, msg, 10).show(); }}
详细解决方案
android 各种惯用,不易记的dialog,等控件整理
热度:41 发布时间:2016-05-01 14:59:34.0
相关解决方案
- android 读取byte[]中的元素解决方案
- android 标题栏兑现方式
- android 中Activity向BroadcastReceiver发送数据,该怎么解决
- Android 4.0 为什么模拟器老是提示小弟我谷歌拼音输入法已停止
- android:getSharedPreferences() 这是哪个类的方法解决思路
- android 怎么判断一个程序是否联网
- android 大量数据按周分组,该如何解决
- android RadioButton如何设置默认选中
- ksoap2-android-这个包,连接webService怎么设置超时
- android 怎么重新设置锚点
- android UI界面设计解决方案
- android 图片对象获取的有关问题
- android 怎么调用淘宝支付宝接口
- Android 沿袭InputMethodService自定义输入法
- android 关于服务连接的疑义
- android 两个activity如何通信
- android 怎么实现对view的放大和缩小
- android 教程解决方法
- android ID,该如何处理
- 准备复习2-3个月,看java+android,请问有经验者,怎么看效果最好》
- android UI线程与AsyncTask的有关问题
- android(java)中的java.net能不能和c#的system.net.sockets进行tcp通信,该如何解决
- android ListView 中的onItemClick Intent 没法跳转
- android(java) 中文乱码的有关问题
- 使用Jquery.ui.Dialog 插件和jquery Ztree 2个插件 样式乱了解决思路
- c#c++,android,ios(iphone),php,java视屏课程 散分
- jquery ui dialog 有关问题(添加到form1里面后还是无法使用服务器控件)
- 使用art.dialog.load加载登陆页返回的有关问题
- android Post文件到ASP.NET的有关问题,能收到参数收不到文件
- art.dialog.open透明度怎么修改,就是弹出层的透明度。