Android應用開發筆記 - 項目代碼3

來源:互聯網
上載者:User

1.3 App類控制項
    MainActivity.java
[java]
package com.example.apppalettesdemo; 
 
import andro
id.app.Activity; 
import android.app.AlertDialog; 
import android.app.AlertDialog.Builder; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.app.ProgressDialog; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Toast; 
 
public class MainActivity extends Activity implements OnClickListener { 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.my_options_menu, menu); 
        return true; 
    } 
     
   @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
        // TODO Auto-generated method stub  
       Boolean flag = false; 
        
        switch (item.getItemId()) { 
        case R.id.AlertDialog01: 
            Builder dialog = new AlertDialog.Builder(MainActivity.this); 
            dialog.setTitle("Hint"); 
            dialog.setMessage("This is Message!"); 
            dialog.setPositiveButton("Ok", this); 
            dialog.show(); 
                         
            flag = true; 
            break; 
 
        case R.id.notificationManager01:     
            NotificationManager notiManager =  
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
             
            Notification notiInst = new Notification( 
                    R.drawable.ic_launcher, "This is notify 2012-09-12",  
                    System.currentTimeMillis()); 
            PendingIntent pendIntent = PendingIntent.getActivity( 
                    this, 0, new Intent(this, MainActivity.class),  
                    PendingIntent.FLAG_UPDATE_CURRENT); 
            notiInst.setLatestEventInfo(this, "Notify", "Notify 01", pendIntent); 
             
            notiManager.notify(0, notiInst); 
             
            flag = true; 
            break; 
             
        case R.id.progressDialog01: 
            ProgressDialog progrDialog = new ProgressDialog(this); 
            progrDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
            progrDialog.setMessage("Loading Information..."); 
            progrDialog.show(); 
             
            flag = true; 
            break; 
             
        default: 
            break; 
        } 
         
        return flag; 
    } 
    
   // DialogInterface  
   @Override 
    public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub  
        Toast.makeText(this, "You just press 'AlertDialog' button!", Toast.LENGTH_SHORT).show(); 
    } 
    

package com.example.apppalettesdemo;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.my_options_menu, menu);
        return true;
    }
   
   @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // TODO Auto-generated method stub
    Boolean flag = false;
   
  switch (item.getItemId()) {
  case R.id.AlertDialog01:
   Builder dialog = new AlertDialog.Builder(MainActivity.this);
   dialog.setTitle("Hint");
   dialog.setMessage("This is Message!");
   dialog.setPositiveButton("Ok", this);
   dialog.show();
      
   flag = true;
   break;

  case R.id.notificationManager01: 
   NotificationManager notiManager =
    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
   
   Notification notiInst = new Notification(
     R.drawable.ic_launcher, "This is notify 2012-09-12",
     System.currentTimeMillis());
   PendingIntent pendIntent = PendingIntent.getActivity(
     this, 0, new Intent(this, MainActivity.class),
     PendingIntent.FLAG_UPDATE_CURRENT);
   notiInst.setLatestEventInfo(this, "Notify", "Notify 01", pendIntent);
   
   notiManager.notify(0, notiInst);
   
   flag = true;
   break;
   
  case R.id.progressDialog01:
   ProgressDialog progrDialog = new ProgressDialog(this);
   progrDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
   progrDialog.setMessage("Loading Information...");
   progrDialog.show();
   
   flag = true;
   break;
   
  default:
   break;
  }
  
  return flag;
 }
  
   // DialogInterface
   @Override
 public void onClick(DialogInterface dialog, int which) {
  // TODO Auto-generated method stub
  Toast.makeText(this, "You just press 'AlertDialog' button!", Toast.LENGTH_SHORT).show();
 }
  
}
 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.