Android學習筆記3-開發過程中一些常用操作

Activity

1.使用Intent 啓動另一個Activity

//啓動一個網頁

Uri myUri = Uri.parse("http://www.flashwing.net");

Intent openBrowserIntent = new Intent(Intent.ACTION_VIEW,myUri);

startActivity(openBrowserIntent);

//打開一個新的Activity

Intent openWelcomeActivityIntent=new Intent();

openWelcomeActivityIntent.setClass(this, TestActivity.class);

startActivity(openWelcomeActivityIntent);

2.從源Activity 中傳遞數據

//數據寫入

Intent Intent openWelcomeActivityIntent=new Intent();

Bundle myBundelForName=new Bundle();

myBundelForName.putString("Key_Name",inName.getText().toString()); myBundelForName.putString("Key_Age",inAge.getText().toString()); openWelcomeActivityIntent.putExtras(myBundelForName);

openWelcomeActivityIntent.setClass(AndroidBundel.this, Welcome.class);

startActivity(openWelcomeActivityIntent);

 

目標Activity 中獲取數據

//從Intent 中獲取數據 

Bundle myBundelForGetName=this.getIntent().getExtras(); 

String name=myBundelForGetName.getString("Key_Name"); 

myTextView_showName.setText("歡迎您進入:"+name); 

 

3.使用Bundle 在Activity 間傳遞數據從源請求Activity 中通過一個Intent 把一個服務請求傳到目標Activity 中 

private Intent toNextIntent;

//Intent 成員聲明 

toNextIntent=new Intent();

//Intent 定義 

toNextIntent.setClass(TwoActivityME3.this, SecondActivity3.class); 

//設定開啓的下一個

Activity startActivityForResult(toNextIntent, REQUEST_ASK); 

 

在源請求Activity 中等待Intent 返回應答結果,通過重載onActivityResult()方法  

 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

       super.onActivityResult(requestCode, resultCode, data); 

       if(requestCode==REQUEST_ASK){

              if(resultCode==RESULT_CANCELED){ 

                     setTitle("Cancel****"); 

             }else if(resultCode==RESULT_OK){ 

                      showBundle=data.getExtras();

                     //從返回的Intent中獲得

                     Bundle Name=showBundle.getString("myName");

                     //從bundle中獲得相應數據 

                     text.setText("the name get from the second layout:/n"+Name); 

             }

       }

 1.第一個參數是你開啓請求Intent時的對應請求碼,可以自己定義。 

2.第二個參數是目標Activity返回的驗證結果碼 

3.第三個參數是目標Activity返回的Intent 目標Activity 中發送請求結果代碼,連同源Activity 請求的數據一同綁定到Bundle中通過Intent 傳回源請求Activity 中  

backIntent=new Intent(); 

stringBundle=new Bundle(); 

stringBundle.putString("myName", Name); 

backIntent.putExtras(stringBundle); 

setResult(RESULT_OK, backIntent);

//返回Activity結果碼 

finish();

 

 

 

取得手機屏幕大小  

 DisplayMetrics displaysMetrics=new DisplayMetrics(); 

getWindowManager().getDefaultDisplay().getMetrics(displaysMetrics); 

displaysMetrics.widthPixels; 

displaysMetrics.heightPixels 

 

UI相關 

標題欄隱藏 

 

 在Activity.setCurrentView();之前調用此方法

 private void HideTitle() { 

requestWindowFeature(Window.FEATURE_NO_TITLE); } 

 狀態欄隱藏(全屏) 

 

在Activity.setCurrentView();之前調用此方法  

 private void HideStatusBar() { 

       //隱藏標題

       requestWindowFeature(Window.FEATURE_NO_TITLE); 

      //定義全屏參數 

      int flag=WindowManager.LayoutParams.FLAG_FULLSCREEN; 

      //獲得窗口對象 

      Window myWindow=this.getWindow(); 

     //設置Flag標識 myWindow.setFlags(flag,flag); } 

 

 

爲程序添加Menu 菜單  

 public boolean onCreateOptionsMenu(Menu menu) { 

       boolean result = super.onCreateOptionsMenu(menu); 

       menu.add(0, INSERT_ID_Play, 0, R.string.menu_toPlay); 

       menu.add(0, INSERT_ID_Stop, 0, R.string.menu_toStop); 

       return result; 

}

 

 

時間相關 

 /* 取得系統時間*/ 

long Time = System.currentTimeMillis(); 

myCalendar = Calendar.getInstance (); 

myCalendar.setTimeInMillis(Time); 

my_Hour = myCalendar.get(Calendar.HOUR); 

my_Minute = myCalendar.get(Calendar.MINUTE); 

my_Second = myCalendar.get(Calendar.SECOND); 

   

 

 /* 從Calendar抽象基類獲得實例對象,並設置成中國時區*/ 

my_Calendar = Calendar.getInstance(Locale.CHINA); 

/* 從日曆對象中獲取當前的:年、月、日、時、分*/ 

my_Year = my_Calendar.get(Calendar.YEAR); 

my_Month = my_Calendar.get(Calendar.MONTH); 

my_Day = my_Calendar.get(Calendar.DAY_OF_MONTH); 

my_Hour = my_Calendar.get(Calendar.HOUR_OF_DAY); 

my_Minute = my_Calendar.get(Calendar.MINUTE);

DatePicker 可以監聽系統日期的改變 DatePicker my_datePicker; 

 /* findViewById()從XML中獲取UI元素對象*/ 

 my_datePicker = (DatePicker) findViewById(R.id.my_DatePicker); 

 /*爲日曆設置器添加點擊事件監聽器,處理設置日期事件*/ 

 my_datePicker.init(my_Year, my_Month, my_Day, new  DatePicker.OnDateChangedListener(){

        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

       /*日期改變事件處理*/ 

    }

}); 

   TimePicker監聽系統時間改變  

  /* 定義程序用到的UI元素對象:時間設置器*/ 

  TimePicker my_timePicker; 

  /* findViewById()從XML中獲取UI元素對象*/

  my_timePicker = (TimePicker) findViewById(R.id.my_TimePicker); 

  /* 把時間設置成24小時制*/ 

  my_timePicker.setIs24HourView(true); 

  /*爲時間設置器添加點擊事件監聽器,處理設置時間事件*/ 

  my_timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener(){ 

       public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { 

          /*時間改變事件處理*/

       }

}); 

    OnDateSetListener 監聽日期設定  

    private OnDateSetListener myDateSetListener=new OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {}     };

   OnTimeSetListener 時間改變設置事件監聽器

 private OnTimeSetListener myTimeSetListener=new OnTimeSetListener(){

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { }

};

 

 

文件相關 文件讀寫操作 

FileInputStream in = this.openFileInput("test2.txt");

//打開文件"test2.txt" 

…… 

in.close();

 1.Context.openFileOutput(String name,int mode)開啓一個與應用程序聯繫的私有文件輸出流 

2.當文件不存在時該文件將被創建 

3.文件輸出流可以在添加模式中打開,這意味新的數據將被添加到文件的末尾

FileOutputStream out = this.openFileOutput("test2.txt",MODE_APPEND); 

//打開文件"test2.txt"進行寫操作、使用MODE_APPEND 在添加模式中打開文件 

out.close();//關閉輸出流

讀取靜態資源文件 

1. 要打開打包在應用程序中的靜態文件,使用resources.openRawResource(R.raw.mydatafile) 

2.該文件必須放在文件夾res/raw/中 

InputStreamin = this.getResources().openRawResource(R.raw.my); 

… 

//獲得Context資源 in.close();

//關閉輸入流

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章