android中給使用者提醒的三種方式

來源:互聯網
上載者:User

標籤:

一.Toast

// 彈出多士  Toast.makeText(getApplicationContext(), "多士", 0).show();

 二.對話方塊:
   1.確定取消對話方塊

 public void click1(View view){       //對話方塊的建立器       AlertDialog.Builder builder = new Builder(this);       builder.setTitle("我是對話方塊");       builder.setMessage("對話方塊顯示的內容");       builder.setPositiveButton("確定", new OnClickListener() {        @Override        public void onClick(DialogInterface dialog, int which) {         Toast.makeText(getApplicationContext(), "確定被點擊了", 0).show();        }       });       builder.setNegativeButton("取消", new OnClickListener() {        @Override        public void onClick(DialogInterface dialog, int which) {         //什麼都不寫預設實現就是關閉掉對話方塊        }       });       builder.setCancelable(false);       builder.create().show();     }

2.單選對話方塊

   public void click2(View view){     //對話方塊的建立器     AlertDialog.Builder builder = new Builder(this);     builder.setTitle("請選擇您的性別");     final String[] items = {"男","女","未知"};     builder.setSingleChoiceItems(items, 2, new OnClickListener() {      @Override      public void onClick(DialogInterface dialog, int which) {       Toast.makeText(getApplicationContext(), "您的性別:"+items[which], 0).show();       dialog.dismiss();      }     });     builder.create().show();    }

3.多選對話方塊

 public void click3(View view){     //對話方塊的建立器     AlertDialog.Builder builder = new Builder(this);     builder.setTitle("請選擇你最愛吃的水果");     final String[] items={"蘋果","梨","菠蘿","香蕉","黃瓜"};     final boolean[] result =new boolean[]{true,false,true,false,false};     builder.setMultiChoiceItems(items, result, new OnMultiChoiceClickListener() {      @Override      public void onClick(DialogInterface dialog, int which, boolean isChecked) {       Toast.makeText(getApplicationContext(), items[which]+isChecked, 0).show();       result[which] = isChecked;      }     });     builder.setPositiveButton("提交", new OnClickListener() {      @Override      public void onClick(DialogInterface dialog, int which) {       StringBuffer sb = new StringBuffer();       for(int i=0;i<result.length;i++){        if(result[i]){         sb.append(items[i]+",");        }       }       Toast.makeText(getApplicationContext(), "您選中了,"+sb.toString(), 0).show();      }     });     builder.show();    }

4.進度條對話方塊

public void click4(View view){    ProgressDialog pd = new ProgressDialog(this);    pd.setTitle("提醒");    pd.setMessage("正在載入資料...請稍等。");    pd.show();   }

5.帶進度的進度條對話方塊

public void click5(View view){    final ProgressDialog pd = new ProgressDialog(this);    pd.setTitle("提醒");    pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);    pd.setMax(100);    pd.setMessage("正在載入資料...請稍等。");    pd.show();    new Thread(){     public void run() {      for(int i = 0;i<100;i++){       try {        Thread.sleep(40);       } catch (InterruptedException e) {        e.printStackTrace();       }       pd.setProgress(i);      }      pd.dismiss();     };    }.start();   }

三.notification----Android手機通知欄

//3.0以前的版本需要用過時的方法才行,否則報錯  public void click(View view){   NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);   Notification notification = new Notification(R.drawable.notification, "我是一個通知", System.currentTimeMillis());   notification.flags = Notification.FLAG_AUTO_CANCEL;//點擊後自動清除通知(Notification.FLAG_NO_CLEAR無法清除通知等等等)   Intent intent = new Intent();   intent.setAction(Intent.ACTION_CALL);   intent.setData(Uri.parse("tel:119"));//向119撥打到電話,要增加打電話的許可權   PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);   notification.setLatestEventInfo(this, "我是標題", "我是內容", contentIntent);   nm.notify(0, notification);  }  /**   * 新版本的notification   * @param view   */  @SuppressLint("NewApi")  public void click2(View view){    Notification noti = new Notification.Builder(this)          .setContentTitle("我是標題")          .setContentText("我是內容")          .setSmallIcon(R.drawable.notification)          .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))          .build();    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);    nm.notify(0, noti);  }

android中給使用者提醒的三種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.