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;
                }
            }});
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章