總的頁面:
注意:按鈕間方法的改變需要: android:onClick="clearNoti" 添加相應的方法即可
1.點擊狀態列按鈕時:
public void Notification(View v){
showNotification("親來簡訊了","5557","我喜歡你", R.drawable.ic_launcher, R.drawable.ic_launcher);
}
public void showNotification(String tickerText,String contentTitle,String contentText ,int iconId,int notiId ){
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//建立一個Notification
Notification notification = new Notification();
//設定發出資訊的內容
notification.icon =iconId;
//設定發出的資訊
notification.tickerText=tickerText;
//設定發出通知的時間
notification.when=System.currentTimeMillis();
//設定顯示通知的預設的發聲或者震動,Light效果
notification.defaults=Notification.DEFAULT_VIBRATE;//震動的效果
//3步: PendingIntent Android系統負責維護
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,getIntent() ,0);
//Notification notification = new Notification(R.drawable.ic_launcher, "有新的訊息", System.currentTimeMillis());
//4步;設定更加詳細的資訊
notification.setLatestEventInfo(this, contentTitle,contentText,pendingIntent);
//5步:使用notificationManager對象的notify方法 顯示Notification訊息 需要制定 Notification的標識
notificationManager.notify(notiId,notification);
} :
2.點擊清楚狀態按鈕時:
//清除的操作
public void clearNoti(View v ){
notificationManager.cancelAll();//清除所有
}
3點擊建立對對話方塊時: public void DiaLog(){
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setIcon(R.drawable.mw)
.setTitle("DiaLog").setMessage("是否建立檔案")
.setPositiveButton("確認",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new AlertDialog.Builder(MainActivity.this).setMessage("您點擊了確認按鈕,檔案已經被建立").show();
}
})
.setNegativeButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new AlertDialog.Builder(MainActivity.this)
.setMessage("您已經選擇了取消的按鈕,該檔案不會被建立").create()
.show();
}
}).show();
}
4。點擊簡單列表對話方塊
public void Simplelistdialog(View v){
final String items[] = {"java","php","3g",".net"};
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("簡單列表對話方塊").setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "親,你喜歡的科目是:"+items[which],Toast.LENGTH_LONG).show();
}
}).show();
}
5.點擊單選列表對話方塊
public void Radiolistdialog(View v){
final String items[] = {"java","php","3g",".net"};
AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle("單選列表對話方塊")
//.setSingleChoiceItems(items, checkedItem, listener)
// .setSingleChoiceItems(itemsId, checkedItem, listener)
//.setSingleChoiceItems(cursor, checkedItem, labelColumn, listener) labelColumn如果資料來源是資料集
//資料集中的某一列會作為列表對話方塊的資料載入的列表框中,該參數表示該列的名稱(欄位名稱)
.setSingleChoiceItems(items,1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "親,你喜歡的科目是:"+items[which],Toast.LENGTH_LONG).show();
}
}).show();
}
7.點擊多選列表對話方塊
public void Multiselectlistdialog(View v){
final String items[]={"Java","PHP","3G",".NET"};
new AlertDialog.Builder(this).setTitle("多選列表對話方塊")
//.setMultiChoiceItems(itemsId, checkedItems, listener)
//.setMultiChoiceItems(cursor, isCheckedColumn, labelColumn, listener)
.setMultiChoiceItems(items, new boolean[]{false,true,true,false}, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked){
Toast.makeText(getApplicationContext(), "親,你選中的科目是:"+items[which], Toast.LENGTH_LONG).show();
}
}
}).setPositiveButton("確認", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "親,你喜歡的科目有:", Toast.LENGTH_LONG).show();
}
}).show();
}
8.點擊進度條
public void ProgressDialog(View v){
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what){
case PRO:
if(PRO>=MAX_PROGRESS){
//重新設定
progress=0;
progressDialog.dismiss();//銷毀對話方塊
}else{
progress++;
progressDialog.incrementProgressBy(1);
//延遲發送訊息
handler.sendEmptyMessageDelayed(PRO,100);
}
break;
default:
break;
}
}
};
progressDialog = new ProgressDialog(this);
progressDialog.setIcon(R.drawable.mw);
progressDialog.setTitle("正在處理資料......");
progressDialog.setMessage("請稍後.....");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); //設定進度條對話方塊 (水平,旋體)
//設定最大值
progressDialog.setMax(MAX_PROGRESS);
progressDialog.setButton("暫停",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.removeMessages(PRO);
}
});
progressDialog.setButton2("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//刪除訊息佇列
handler.removeMessages(PRO);
//恢複進度初始值
progress=0;
progressDialog.setProgress(progress);
}
});
//顯示
progressDialog.show();
//必須設定到show之後,show之前可能bug
progress = (progress>0) ?progress:0;
progressDialog.setProgress(progress);
//線程
handler.sendEmptyMessage(PRO);
}
還有一種效果是:
9.點擊自訂表格對話方塊
public void MyDialog(View v){
LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.activity_main, null); //R.layout.activty_main自訂的布局檔案這裡可以是自己隨意定義的
new AlertDialog.Builder(this).setView(view).setTitle("自訂的對話方塊").setPositiveButton("確認按鈕", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//處理
}
}).show();
}