天天看點

安卓的對話框和通知的知識點總結

  每天持續分享關于程式設計的小知識,歡迎關注轉發收藏!

  在 Android 中種對話框及各種通知效果的應用

  常用對話框的使用,彈出式對話框、日期選擇對話框、時間選擇對話框、進度條對話框

  通知(出現在通知清單)和提示性通知(Toast)的示範

  1、常用對話框的示範

  res/layout/main.xml

  代碼

  

  android:orientation="vertical" android:layout_width="fill_parent"

  android:layout_height="fill_parent">

  android:layout_height="wrap_content">

  res/layout/view.xml

  android:layout_width="wrap_content" android:layout_height="wrap_content">

  res/values/array.xml

  項目 1

  項目 2

  Main.java

  package com.webabcd.dialog;

  import java.util.Calendar;

  import android.app.Activity;

  import android.app.AlertDialog;

  import android.app.DatePickerDialog;

  import android.app.Dialog;

  import android.app.ProgressDialog;

  import android.app.TimePickerDialog;

  import android.app.DatePickerDialog.OnDateSetListener;

  import android.app.TimePickerDialog.OnTimeSetListener;

  import android.content.DialogInterface;

  import android.content.DialogInterface.OnClickListener;

  import android.os.Bundle;

  import android.view.View;

  import android.widget.DatePicker;

  import android.widget.TextView;

  import android.widget.TimePicker;

  import android.widget.Button;

  public class Main extends Activity {

  /* Called when the activity is first created. /

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  // 彈出式對話框的 Demo。先調用 Builder(),在 Create(), 需要顯示對話框的是後再調用 show()

  AlertDialog dialog=new AlertDialog.Builder(this).setTitle("彈出對話框").create();

  dialog.show();

  // 以下是各種對話框的 Demo

  MyButtonClickListener listener=new MyButtonClickListener();

  Button btn1=(Button) this.findViewById(R.id.btn1);

  btn1.setText("簡單的對話框的 Demo");

  btn1.setOnClickListener(listener);

  Button btn2=(Button) this.findViewById(R.id.btn2);

  btn2.setText("包括常用設定的對話框(資料來自 xml)");

  btn2.setOnClickListener(listener);

  Button btn3=(Button) this.findViewById(R.id.btn3);

  btn3.setText("彈出的對話框的内容是一個 View");

  btn3.setOnClickListener(listener);

  Button btn4=(Button) this.findViewById(R.id.btn4);

  btn4.setText("日期選擇對話框");

  btn4.setOnClickListener(listener);

  Button btn5=(Button) this.findViewById(R.id.btn5);

  btn5.setText("

域名購買平台

時間選擇對話框");

  btn5.setOnClickListener(listener);

  Button btn6=(Button) this.findViewById(R.id.btn6);

  btn6.setText("進度條對話框");

  btn6.setOnClickListener(listener);

  }

  class MyButtonClickListener implements View.OnClickListener {

  public void onClick(View v) {

  // 具體的對話框的實作可以通過重寫 onCreateDialog 完成

  switch (v.getId()) {

  case R.id.btn1:

  Main.this.showDialog(0);

  break;

  case R.id.btn2:

  Main.this.showDialog(1);

  case R.id.btn3:

  Main.this.showDialog(2);

  case R.id.btn4:

  Main.this.showDialog(3);

  case R.id.btn5:

  Main.this.showDialog(4);

  case R.id.btn6:

  Main.this.showDialog(5);

  public Dialog onCreateDialog(int id) {

  switch (id) {

  case 0:

  // 一個簡單的彈出對話框

  return new AlertDialog.Builder(this).setTitle("這是一個簡單的彈出對話框的 Demo")

  .create();

  case 1:

  // 一個相對複雜的彈出對話框

  return new AlertDialog.Builder(this)

  .setTitle("标題") // 設定标題

  // .setCustomTitle(View) // 以一個 View 作為标題

  .setIcon(R.drawable.icon01) // 設定标題圖檔

  // .setMessage("資訊") // 需要顯示的彈出内容

  .setPositiveButton("确定", new OnClickListener() { // 設定彈框的确認按鈕所顯示的文本,以及單擊按鈕後的響應行為

  public void onClick(DialogInterface a0, int a1) {

  TextView txtMsg=(TextView) Main.this.findViewById(R.id.txtMsg);

  txtMsg.append("單擊了對話框上的“确認”按鈕

  ");

  })

  .setItems(R.array.ary, new

  DialogInterface.OnClickListener() { // 彈框所顯示的内容來自一個數組。數組中的資料會一行一行地依次排列

  public void onClick(DialogInterface dialog, int which) {

  // 其他常用方法如下

  // .setMultiChoiceItems(arg0, arg1, arg2)

  // .setSingleChoiceItems(arg0, arg1, arg2)

  // .setNeutralButton(arg0, arg1)

  // .setNegativeButton(arg0, arg1)

  case 2:

  // 彈出對話框為指定的 View 的 Demo

  return new AlertDialog.Builder(this).setTitle("此對話框的内容是一個 View")

  .setView(this.findViewById(R.layout.view)).create();

  case 3:

  // 彈出日期選擇對話框

  Calendar c=Calendar.getInstance();

  return new DatePickerDialog(this, new OnDateSetListener() {

  public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

  txtMsg.append("新設定的日期為:" + String.valueOf(year) + "-"

  + String.valueOf(monthOfYear) + "-"

  + String.valueOf(dayOfMonth) + "

  }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE));

  case 4:

  // 彈出時間選擇對話框

  Calendar c2=Calendar.getInstance();

  return new TimePickerDialog(this, new OnTimeSetListener() {

  public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

  txtMsg.append("新設定的時間為:"

  + String.valueOf(hourOfDay) + ":"

  + String.valueOf(minute) + "

  }, c2.get(Calendar.HOUR), c2.get(Calendar.MINUTE), true);

  case 5:

  // 彈出進度條對話框

  ProgressDialog progress=new ProgressDialog(this);

  progress.setMessage("loading...");

  return progress;

  default:

  return null;

  安卓是非常有用的一門程式設計,現階段是市場需要依然很大!

繼續閱讀