Android之基本常見知識(持續更新)

來源:互聯網
上載者:User
//能夠取得螢幕的資訊 

DisplayMetrics dm = new DisplayMetrics(); 

    getWindowManager().getDefaultDisplay().getMetrics(dm);

dm.widthPixels;                //取得寬像素
    dm.heightPixels;       //取得高像素

 

//擷取當地的日曆 

Calendar c=Calendar.getInstance();    mYear=c.get(Calendar.YEAR);//擷取年份    mMonth=c.get(Calendar.MONTH);//擷取月份 mDay=c.get(Calendar.DAY_OF_MONTH);//擷取號數    mHour=c.get(Calendar.HOUR_OF_DAY);//擷取小時

    mMinute=c.get(Calendar.MINUTE); //擷取分鐘

 

//把EditText的內容設為可視或隱藏

/* 設定EditText的內容為可見的 */

     editText.setTransformationMethod(

     HideReturnsTransformationMethod.getInstance());

 

     /* 設定EditText的內容為隱藏的 */

     editText.setTransformationMethod(

     PasswordTransformationMethod.getInstance()); 

 

//啟動activity時不自動彈出軟鍵盤

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);  

 

//設定全屏(在setContentView之前設定)        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

 
//設定取消全屏

WindowManager.LayoutParams attrs = getWindow().getAttributes();  

attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().setAttributes(attrs); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

 

//設定無標題 (在setContentView之前設定)

        requestWindowFeature(Window.FEATURE_NO_TITLE); 

 

//設定禁止手機橫屏(在setContentView之前設定)

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); 

 

//調用手機預設的攝像功能,而且可以設定儲存位置

Intent i = new Intent("android.media.action.IMAGE_CAPTURE");  

i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment .getExternalStorageDirectory(),"pic.jpg")));

 

//解決中文亂碼方法 

A. 使用getBytes("") 來對漢字進行重編碼,得到它的位元組數組

B. 再使用new String(Bytes[] , "解碼方式")  來對位元組數組進行相應的解碼

 

//在Android中輕鬆實現橫豎屏的布局 

豎屏的布局一般在layout下面設定;橫屏的布局則在layout的同等級檔案夾建立名字layout-land的檔案夾。模擬器可以使用Ctrl+F11進行快速切換。 

 

//Android橫豎屏切換不重啟Activity

androidmanifest.xml中的activit元素加入這個屬性android:configChanges="orientation|keyboardHidden" 

然後在Activity中重載以下方法:

          public void onConfigurationChanged(Configuration newConfig) {

     // TODO Auto-generated method stub
  if (newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE) {
            setContentView(R.layout.imageswitch);
            //橫屏
        } else {
            setContentView(R.layout.editcontact);//豎屏
        }
         super.onConfigurationChanged(newConfig);

}   

    
卸載程式:

Uri packageURI = Uri.parse("package:com.demo.CanavaCancel");   

Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);   startActivity(uninstallIntent);

安裝apk:

String str = "/CanavaCancel.apk"; String fileName = Environment.getExternalStorageDirectory() + str; Intent intent = new Intent(Intent.ACTION_VIEW);  intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive"); startActivity(intent);

 

相關文章

聯繫我們

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