当前位置:   article > 正文

Dialog(对话框)的使用_.dialogcart是干嘛的

.dialogcart是干嘛的

Dialog是一种能遮盖住其他控件操作特殊控件,一般用于确认用户操作或者有紧急的信息需要回馈给用户,会需要使用到Dialog,下面是一些关于Dialog的使用。

  1. package com.example.dialogdemo;
  2. import java.util.Calendar;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.DatePicker;
  9. import android.widget.ListView;
  10. import android.widget.TextView;
  11. import android.widget.TimePicker;
  12. import android.widget.Toast;
  13. import android.app.Activity;
  14. import android.app.AlertDialog;
  15. import android.app.DatePickerDialog;
  16. import android.app.DatePickerDialog.OnDateSetListener;
  17. import android.app.ProgressDialog;
  18. import android.app.TimePickerDialog;
  19. import android.app.TimePickerDialog.OnTimeSetListener;
  20. import android.content.DialogInterface;
  21. import android.content.Intent;
  22. import android.content.DialogInterface.OnMultiChoiceClickListener;
  23. public class MainActivity extends Activity implements OnClickListener, Runnable {
  24. ProgressDialog pDialog;
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29. TextView tv = (TextView) findViewById(R.id.dailog_tv);
  30. tv.setOnClickListener(this);
  31. }
  32. /**
  33. * AlertDialog
  34. */
  35. // 消息对话框
  36. public void messageDialog() {
  37. // 提示对话框的建立者,新建AlertDialog内部类Buileder的对象
  38. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  39. // Builder builder = new Builder(this);//也可以直接new Bulider对象
  40. // 设置标题,没有标题就没有图标
  41. builder.setTitle("消息");
  42. // 设置图标
  43. builder.setIcon(R.drawable.ic_launcher);
  44. // 设置对话框点击其他位置不会被取消
  45. builder.setCancelable(false);
  46. // 设置左边按键。4.0以后消极在左边。Negative
  47. builder.setNegativeButton("消极", new DialogInterface.OnClickListener() {
  48. /**
  49. * @parm dialog-->当前按钮所在的对话框
  50. * @parm which-->判断是那个按键,因为监听的只是消极,所以在这没用 如果设置了两个一样的 按键,会被后一个覆盖
  51. */
  52. @Override
  53. public void onClick(DialogInterface dialog, int which) {
  54. dialog.dismiss();
  55. }
  56. });
  57. // 设置右边按键,Positive
  58. builder.setPositiveButton("积极", new DialogInterface.OnClickListener() {
  59. @Override
  60. public void onClick(DialogInterface dialog, int which) {
  61. }
  62. });
  63. // 设置中间按键,Neutral
  64. builder.setNeutralButton("中立", new DialogInterface.OnClickListener() {
  65. @Override
  66. public void onClick(DialogInterface dialog, int which) {
  67. }
  68. });
  69. // 设置消息
  70. builder.setMessage("天降正义");
  71. // 通过builder.create(),新建AlertDialog对象
  72. AlertDialog dialog = builder.create();
  73. dialog.show();
  74. }
  75. // 列表对话框
  76. public void itemDialog() {
  77. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  78. builder.setTitle("列表");
  79. final String[] items = { "呵呵", "哈哈", "嘻嘻", "dj", "ds", "sd" };
  80. // 设置列表
  81. builder.setItems(items, new DialogInterface.OnClickListener() {
  82. /**
  83. * @parm which-->当前所选中的元素
  84. */
  85. @Override
  86. public void onClick(DialogInterface dialog, int which) {
  87. Log.e("items[" + which + "]", items[which]);
  88. }
  89. });
  90. AlertDialog dialog = builder.create();
  91. dialog.show();
  92. }
  93. // 单选对话框
  94. public void singleDialog() {
  95. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  96. builder.setTitle("单选");
  97. // 单选元素
  98. final String[] items = { "呵呵", "哈哈", "嘻嘻", "dj", "ds", "sd" };
  99. // 设置单选元素
  100. /*
  101. * items单选的元素, checkedItem选中的元素的角标,默认选中的元素的角标,若要设置无选中则设置为-1
  102. */
  103. builder.setSingleChoiceItems(items, -1,
  104. new DialogInterface.OnClickListener() {
  105. @Override
  106. public void onClick(DialogInterface dialog, int which) {
  107. Log.e("items[" + which + "]", items[which]);
  108. }
  109. });
  110. // 包含创建对话框和显示对话框,可以不用创建Dialog对象
  111. builder.show();
  112. }
  113. // 多选对话框
  114. public void multilDialog() {
  115. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  116. builder.setTitle("多选");
  117. // 多选的元素
  118. final String[] items = { "呵呵", "哈哈", "嘻嘻", "dj", "ds", "sd" };
  119. // 选中的元素,与多选的元素对应,被选中则为true
  120. final boolean[] checkedItems = new boolean[items.length];
  121. // 设置多选的元素
  122. builder.setMultiChoiceItems(items, checkedItems,
  123. new OnMultiChoiceClickListener() {
  124. /**
  125. * @parm which-->选中的元素
  126. * @parm isChecked-->该元素是否被选中
  127. */
  128. @Override
  129. public void onClick(DialogInterface dialog, int which,
  130. boolean isChecked) {
  131. Toast.makeText(MainActivity.this,
  132. items[which] + "======" + isChecked,
  133. Toast.LENGTH_LONG).show();
  134. AlertDialog.Builder builders = new AlertDialog.Builder(
  135. MainActivity.this);
  136. builders.setNegativeButton("取消",
  137. new AlertDialog.OnClickListener() {
  138. @Override
  139. public void onClick(DialogInterface dialog,
  140. int which) {
  141. Log.e("取消", "取消了选择");
  142. }
  143. });
  144. builders.setPositiveButton("确定",
  145. new AlertDialog.OnClickListener() {
  146. @Override
  147. public void onClick(DialogInterface dialog,
  148. int which) {
  149. }
  150. });
  151. builders.show();
  152. }
  153. });
  154. builder.setNegativeButton("确认", new DialogInterface.OnClickListener() {
  155. @Override
  156. public void onClick(DialogInterface dialog, int which) {
  157. for (int i = 0; i < checkedItems.length; i++) {
  158. if (checkedItems[i]) {
  159. Log.e(i + ":", items[i]);
  160. }
  161. }
  162. }
  163. });
  164. builder.show();
  165. }
  166. /**
  167. * 日期对话框
  168. */
  169. public void dateDialog() {
  170. final Calendar calendar = Calendar.getInstance();
  171. final DatePickerDialog datePickerDialog = new DatePickerDialog(this,
  172. new OnDateSetListener() {
  173. /**
  174. * @parm view 监听的对话框中显示的日期的改变
  175. * @parm year 对话框消失前所显示的年份
  176. * 。。。
  177. */
  178. @Override
  179. public void onDateSet(DatePicker view, int year,
  180. int monthOfYear, int dayOfMonth) {
  181. System.out.println("11111111111111");
  182. calendar.set(Calendar.YEAR, year);
  183. calendar.set(Calendar.MONTH, monthOfYear);
  184. calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
  185. updateDate(calendar);
  186. Toast.makeText(MainActivity.this, "hehe......",
  187. Toast.LENGTH_LONG).show();
  188. System.out.println("---------------");
  189. }
  190. }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
  191. calendar.get(Calendar.DAY_OF_MONTH));
  192. //设置按键,会先执行
  193. datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "消极", new DialogInterface.OnClickListener() {
  194. @Override
  195. public void onClick(DialogInterface dialog, int which) {
  196. // datePickerDialog.show();
  197. }
  198. });
  199. datePickerDialog.show();
  200. }
  201. /**
  202. * 更新日期
  203. */
  204. public void updateDate(Calendar calendar) {
  205. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  206. builder.setTitle("选择的日期");
  207. builder.setMessage(calendar.get(Calendar.YEAR) + "年"
  208. + (calendar.get(Calendar.MONTH) + 1) + "月"
  209. + calendar.get(Calendar.DAY_OF_MONTH) + "日");
  210. builder.show();
  211. }
  212. /**
  213. * 时间对话框
  214. */
  215. public void timeDialog() {
  216. final Calendar calendar = Calendar.getInstance();
  217. TimePickerDialog timePickerDialog = new TimePickerDialog(this,
  218. new OnTimeSetListener() {
  219. @Override
  220. public void onTimeSet(TimePicker view, int hourOfDay,
  221. int minute) {
  222. calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
  223. calendar.set(Calendar.MINUTE, minute);
  224. updateTime(calendar);
  225. }
  226. }, calendar.get(Calendar.HOUR_OF_DAY),
  227. calendar.get(Calendar.MINUTE), true/*24小时制*/);
  228. timePickerDialog.show();
  229. }
  230. /**
  231. * 更新日期
  232. */
  233. public void updateTime(Calendar calendar) {
  234. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  235. builder.setTitle("选择的时间");
  236. builder.setMessage(calendar.get(Calendar.HOUR_OF_DAY) + "时"
  237. + calendar.get(Calendar.MINUTE) + "分");
  238. builder.show();
  239. }
  240. /**
  241. * 自定义对话框
  242. */
  243. public void myDialog() {
  244. MyDialog myDialog = new MyDialog(this);
  245. myDialog.show();
  246. }
  247. /**
  248. * 局部自定义,自定义ListView、按键、消息内容等
  249. */
  250. public void myAlertDialog() {
  251. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  252. //1用listView作为视图
  253. // ListView listView = new ListView(this);
  254. //2用布局过滤器设置视图
  255. LayoutInflater li = LayoutInflater.from(this);
  256. View view = li.inflate(R.layout.dialog, null);
  257. //3.设置Activity作为对话框
  258. //列表内容适配器设置
  259. //设置对话框的视图
  260. // builder.setView(listView);
  261. builder.setView(view);
  262. }
  263. /**
  264. * Activity作为对话框
  265. */
  266. public void dialogActivity() {
  267. Intent intent = new Intent(this, DialogActivity.class);
  268. startActivity(intent);
  269. }
  270. /**
  271. * ProgressDialog
  272. */
  273. public void progressDialog() {
  274. pDialog = new ProgressDialog(this);
  275. /*
  276. * 设置风格精确进度条:ProgressDialog.STYLE_HORIZONTAL
  277. * 模糊进度条:ProgressDialog.STYLE_SPINNER
  278. */
  279. // 精确进度条
  280. pDialog.setProgressStyle(1);
  281. // 设置最大值
  282. pDialog.setMax(50);
  283. // 设置二级进度
  284. // pDialog.setSecondaryProgress(30);
  285. // 设置进度图片
  286. // pDialog.setProgressDrawable(d)
  287. // 模糊进度条
  288. // pDialog.setProgressStyle(0);
  289. // 设置消息
  290. pDialog.setMessage("请稍后。。。");
  291. // 设置进度条的图片
  292. // pDialog.setIndeterminateDrawable(d)
  293. // 设置对话框的按钮
  294. pDialog.setButton(DialogInterface.BUTTON_NEUTRAL/* 按键形式 */, "确认",
  295. new DialogInterface.OnClickListener() {
  296. @Override
  297. public void onClick(DialogInterface dialog, int which) {
  298. }
  299. });
  300. pDialog.show();
  301. // 设置当前进度,要在show之后
  302. // pDialog.setProgress(20);
  303. // 进度条加载完就消失
  304. // if (pDialog.getProgress() == pDialog.getMax()) {
  305. // pDialog.dismiss();
  306. // }
  307. Thread thread = new Thread(this);
  308. thread.start();
  309. }
  310. @Override
  311. public void onClick(View v) {
  312. if (v.getId() == R.id.dailog_tv) {
  313. // messageDialog();
  314. // itemDialog();
  315. // singleDialog();
  316. // multilDialog();
  317. // progressDialog();
  318. // dateDialog();
  319. // timeDialog();
  320. dialogActivity();
  321. }
  322. }
  323. @Override
  324. public void run() {
  325. int temp = 0;
  326. while (!(pDialog.getProgress() == pDialog.getMax())) {
  327. temp += 10;
  328. try {
  329. Thread.sleep(1000);
  330. pDialog.setProgress(temp);
  331. } catch (InterruptedException e) {
  332. e.printStackTrace();
  333. }
  334. }
  335. pDialog.dismiss();
  336. }
  337. }

将Activity作为Dialog的形式显示,只需要将Activity的清单中设置属性为Dialog即可,如下

android:theme="@android:style/Theme.Dialog"



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/771044
推荐阅读
相关标签
  

闽ICP备14008679号