Android 偶遇小問題解決方案集合

一、Android 如何讓EditText不自動獲取焦點

 

解決方案:找一個EditText的父級控件把EditText默認的行爲截斷了!設置html

android:focusable="true"
android:focusableInTouchMode="true">android

示例:eclipse

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
 

二、程序運行在後臺最小化

主要調用moveTaskToBack(true)方法,簡單的來講相似於點擊home鍵,具體理解請移步http://www.cnblogs.com/yishujun/p/5394518.htmlide

(1)此種方式經過Home鍵效果強行影響到Back鍵對Activity生命週期的影響佈局

@Override
public void onBackPressed() {
    Intent home = new Intent(Intent.ACTION_MAIN);
    home.addCategory(Intent.CATEGORY_HOME);
    startActivity(home);
}

(2)Android的moveTasktoBack()就是說讓進程activity棧在後臺去運行,相似最小化。可使用這個方法,不讓咱們的應用退出。ui

@Overridethis

public void finish() {spa

//super.finish(); //記住不要執行此句code

moveTaskToBack(true); //設置該activity永不過時,即不執行onDestroy()orm

(3)按返回鍵的時候不但願退出(默認就finish了),而是隻但願置後臺,就能夠調這個方法

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

if(keyCode == KeyEvent.KEYCODE_BACK){

moveTaskToBack(true);

return true;

}

return super.onKeyDown(keyCode, event);

}

(4)TabActivity中,要複寫Tab中的onfinish方法:
public void finish()
{
this.moveTaskToBack(true);
}

三、從eclipse轉到Android Studio後,Button的文本中,英文字母所有顯示爲大寫,無論text中寫的是大寫仍是小寫

解決方案:在Button的屬性里加上android:textAllCaps="false"就好了

 

四、 Android根據字符串型的資源名獲取對應資源id

解決方案:

方法一:

利用getResources().getIdentifier(String name,String defType,String defPackage) 獲取

public int  getResource(String imageName){
     Context ctx=getBaseContext();
     int resId = getResources().getIdentifier(imageName, "mipmap", ctx.getPackageName());
     //若是沒有在"mipmap"下找到imageName,將會返回0
     return resId;
}
方法二:

使用反射機制獲取

public int  getResource(String imageName){
    Class mipmap = R.mipmap.class;
    try {
        Field field = mipmap.getField(imageName);
        int resId = field.getInt(imageName);
        return resId;
    } catch (NoSuchFieldException e) {//若是沒有在"mipmap"下找到imageName,將會返回0
        return 0;
    } catch (IllegalAccessException e) {
        return 0;
    }

}

PS:根據資源id獲取資源名稱:

String resName = getResources().getResourceName(resId);

五、getDrawable過期的替代方法

mDrawable = getResources().getDrawable(resourcesId); 方法過時
 
解決方案:
 
方法一:public Drawable getDrawable(int id, @Nullable Theme theme) 
 
方法二: mDrawable = ContextCompat.getDrawable(this,resourcesId); 
 

六、獲取版本號

解決方案:Build.VERSION.SDK_INT

七、字節大小相互轉換

解決方案:

1 byte = 8 bit

1 KB = 1024 bytes

1 MB = 1024 KB

1 GB = 1024 MB

1 TB = 1024 GB

位:「位(bit)」是電子計算機中最小的數據單位。每一位的狀態只能是0或1。
  字節:8個二進制位構成1個「字節(Byte)」,它是存儲空間的基本計量單位。1個字節能夠儲存1個英文字母或者半個漢字,換句話說,1個漢字佔據2個字節的存儲空間。
  字:「字」由若干個字節構成,字的位數叫作字長,不一樣檔次的機器有不一樣的字長。例如一臺8位機,它的1個字就等於1個字節,字長爲8位。若是是一臺16位機,那麼,它的1個字就由2個字節構成,字長爲16位。字是計算機進行數據處理和運算的單位。
  KB:在通常的計量單位中,一般K表示1000。例如:1千米= 1000米,常常被寫爲1km;1公斤=1000克,寫爲1kg。一樣K在二進制中也有相似的含義。只是這時K表示1024,也就是2的10次 方。1KB表示1K個Byte,也就是1024個字節。
  MB:計量單位中的M(兆)是10的6次方,見到M天然想起要在該數值的後邊續上六個0,即擴大一百萬倍。在二進制中,MB也表示到了百萬級的數量級,但1MB不正好等於1000000字節,而是1048576字節,即 1MB = 2E+20 Bytes = 1048576Bytes。

public static String convertFileSize(long size) {
long kb = 1024;
long mb = kb * 1024;
long gb = mb * 1024;
if (size >= gb) {
return String.format("%.1f GB", (float) size / gb);
} else if (size >= mb) {
float f = (float) size / mb;
return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
} else if (size >= kb) {
float f = (float) size / kb;
return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
} else
return String.format("%d B", size);
}

八、Android TextView 中間省略

解決方案:設置 android:ellipsize

android:ellipsize屬性的具體值介紹以下:
android:ellipsize = "end"   省略號在結尾
android:ellipsize = "start"   省略號在開頭
android:ellipsize = "middle"     省略號在中間
android:ellipsize = "marquee"  跑馬燈
添加了android:ellipsize這個屬性後,最好仍是添加上:
android:singleline = "true"

九、Listview item 包含CheckBox、CheckBox、Button等點擊該項無效

簡而言之,這是一個View的焦點搶佔問題。因爲裏面有Item裏面包含了一個CheckBox(相似狀況還包括Button、ImageButton等自己帶有click等事件的View)

咱們要作的,就是處理好Adapter裏面佈局的XML根佈局的descendantFocusability屬性,該屬性是當一個爲view獲取焦點時,定義viewGroup和其子控件二者之間的關係。

android:descendantFocusability
Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.
Must be one of the following constant values:
beforeDescendants:viewgroup會優先其子類控件而獲取到焦點
afterDescendants:viewgroup只有當其子類控件不須要獲取焦點時才獲取焦點
blocksDescendants:viewgroup會覆蓋子類控件而直接得到焦點

通常來講咱們使用第三種 blockDescendants,簡單的解決辦法就是在自定義item的佈局中的根佈局中指定 android:descendantFocusability="blocksDescendants"

相關文章
相關標籤/搜索