Android 曾經的小Bug(常識)

開發中遇到的一些小問題,邊開發邊記錄:

1、ScrollView和ListView 去除滑動時的陰影滾動條:

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/layout_btn_touzi"
        android:layout_below="@+id/framelayout"
        android:fadingEdge="none"
        android:overScrollMode="never"
        android:scrollbars="none" >

<ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:fadingEdge="none"
        android:overScrollMode="never"
        android:scrollbars="none">
    </ListView>

2、EidtText 獲取焦點事件:

XML文件中 :

<EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

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

            android:textColorHint="#78dcd2"
            android:textColor="#06C1AE"
            android:background="#00000000"
            android:singleLine="true"
            android:textSize="17dp"
            android:hint="用戶名/手機號"
            android:paddingLeft="10dp" />

Java 代碼中:

editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.requestFocus();

editText.clearFocus(); //失去焦點
editText.requestFocus();//獲取焦點

3、做省市區 三級聯動時 引用 city.s3db數據庫 時報錯 :
原因 package 路徑錯誤,將demo中的package替換爲自己的package即可

4、CheckBox 更改默認顯示圖標 ,默認的圖標與UI設計不符合,需要變更,方法如下:
CheckBox:

 <CheckBox
                style="@style/msg_gray_hint"
                android:layout_marginLeft="10dp"
                android:button="@drawable/selector_btn_check"
                android:drawablePadding="5dp"
                android:text="我已閱讀並同意"/>

Selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- checked -->
    <item android:drawable="@drawable/check_true" android:state_checked="true"/>
    <!-- default -->
    <item android:drawable="@drawable/check_false" android:state_checked="false"/>

</selector>
在selector中放兩張圖片check_true、check_false,分別代表選擇和未選擇的圖標
有一點要說明的是文字與圖標之間的間隙,讓美工做圖片的時候 給留一點空隙(調試了半天還是那樣就取巧了)。

5. ListView 的item 點擊事件沒有響應時 添加如下代碼:
android:descendantFocusability=”blocksDescendants”

6. ListView 去除或者替換點擊的背景色:
android:listSelector=”@color/blue”

 <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:divider="@drawable/bg_line"
        android:fadingEdge="none"
        android:overScrollMode="never"
        android:listSelector="@color/blue"
        android:scrollbars="none" >

7. 隱藏鍵盤:
進入頁面先隱藏鍵盤,當EditText 獲得焦點時在彈出鍵盤
((InputMethodManager)et_user_password.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(LoginActivity.this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);

8. imageView.setClickable(false) 失效的解決辦法
設置一個控件的setClickable(false)後,發現沒有效果。
很奇怪的一個現象,最後發現是它在作怪

if (!isClickable()) {  
           setClickable(true);  
}  

所以要在 setOnClickListener ()之後設置setClickable(false)
如:

 edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imageView.isClickable()) {
                    update.setVisibility(View.GONE);
                    imageView.setClickable(false);
                } else {
                    update.setVisibility(View.VISIBLE);
                    imageView.setClickable(true);
                }
            }
        });
 imageView.setClickable(false);
    }

9、ListView中有button,checkbox,GridView的用法

1.如果listview的item中有button,checkbox的時候,往往如果不做處理的情況,button,checkbox會獲得焦   點,導致listview不相應OnItemLongClickListener()這個方法,搶佔了Item的獲得焦點的能力,解決的辦法有兩種:
    (1)在button,checkbox的xml佈局文件中添加代碼: android:focusable=“false” 即就可以相應OnItemLongClickListener()這個方法了。
    (2)在listview中的item的佈局文件中,在總的LinearLayout的屬性中添加一句話:android:descendantFocusability="blocksDescendants" 即把在LinearLayout裏面的focusable屬性都設置成了false, 就可以相應OnItemLongClickListener()這個方法了。

2、在ListView的item中有GridView,搶佔焦點的情況,解決辦法步驟如下:
(1) 現在ListView的Item的最外面的LinearLayout中設置:android:descendantFocusability=”blacksDescendants”
(2) 再在gridview的getView()方法中設置:
gridview.setClickable(false);
gridview.setPressed(false);
gridview.setEnabled(false);
即就可以相應OnItemLongClickListener()這個方法了。

10、Android EditText 輸入金額(小數點後兩位)

  editText.setFilters(new InputFilter[]{new InputFilter() {
                @Override
                public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                    if(source.equals(".") && dest.toString().length() == 0){
                        return "0.";
                    }
                    if(dest.toString().contains(".")){
                        int index = dest.toString().indexOf(".");
                        int mlength = dest.toString().substring(index).length();
                        if(mlength == 3){
                            return "";
                        }
                    }
                    return null;
                }
            }});
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章