Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android應用之基本的組件(一)

Android應用之基本的組件(一)

編輯:關於Android編程

總的頁面:

   \

 


  注意:按鈕間方法的改變需要:     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();


     
}

 

\
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved