一.Toastjava
// 彈出吐司 Toast.makeText(getApplicationContext(), "吐司", 0).show();
二.對話框:
1.肯定取消對話框app
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.單選對話框ide
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.多選對話框ui
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.進度條對話框this
public void click4(View view){ ProgressDialog pd = new ProgressDialog(this); pd.setTitle("提醒"); pd.setMessage("正在加載數據...請稍等。"); pd.show(); }
5.帶進度的進度條對話框code
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手機通知欄get
//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); }