android小知识

1、资源文件drawable中创建selector布局时资源放置的位置会影响是否存在按下效果:

<span style="font-size:14px;"><span style="font-size:14px;"><item android:drawable="@mipmap/biz_navigation_tab_discovery_selected" android:state_checked="true"/>      //设置按下的效果,必须放在下行正常设置的前面,不然没有效果
    <item android:drawable="@mipmap/biz_navigation_tab_discovery"/>      //正常状态</span></span>


2、list的item选项点击使选中item置顶的动态效果,只需要在onitemClicklistener中引用listview对象实例设置setSelection(int position)这样就会出现item向上移动置顶的动态效果了



3、身份证匹配(正则表达式):

一、15位身份证号

二、18位身份证号(前17位位数字,最后一位为字母x)

三、18为身份证号(18位都是数字)

<span style="font-size:14px;">public boolean personIdValidation(String text) {
        Pattern idNumPattern = Pattern.compile("^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$");
        //通过Pattern获得Matcher
        Matcher idNumMatcher = idNumPattern.matcher(text);
        //判断用户输入是否为身份证号
        return  idNumMatcher.matches();
    }</span>

  • 4、手机号匹配:
  • /**
       * 手机号码
       * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
       * 联通:130,131,132,152,155,156,185,186
       * 电信:133,1349,153,180,189
       */
  • <span style="font-size:14px;">public static boolean isMobileNO(String mobiles){
    
        Pattern p = Pattern.compile("^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$");
    
        Matcher m = p.matcher(mobiles);
    
    
        return m.matches();
    
        }
    </span>

    5、自定义(shape)画边框,在drawable中创建xml文件:
  • <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <!-- 连框颜色值 -->
        <item>
            <shape>
                <solid android:color="#E5D8B0" />
            </shape>
        </item>
        <!-- 主体背景颜色值 -->
        <item android:bottom="3dp"> <!--设置只有底部有边框-->
            <shape>
                <solid android:color="#343949" />
            </shape>
        </item>
    </layer-list></span>


  • 6、listivew中item布局设置margin属性无效解决办法

  • 在item最外层布局多添加一层父布局,在子布局中设置就可以了:

  • <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="100dp">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_marginLeft="@dimen/margin_20dp"                //在此处设置margin属性就有效果了,放在最外层是无效的
            android:layout_marginRight="@dimen/margin_20dp"
            android:background="@color/write_color">
    
            <ImageView
                android:id="@+id/head_zixun_item"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:layout_alignParentRight="true"
                android:layout_margin="@dimen/margin_10dp"
                android:scaleType="centerCrop" />
    
            <TextView
                android:id="@+id/index_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="@dimen/margin_10dp"
                android:layout_toLeftOf="@id/head_zixun_item"
                android:text="上证指数"
                android:textSize="@dimen/activity_vertical_margin" />
    
            <TextView
                android:id="@+id/index_num"
    
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_gravity="center"
                android:layout_marginBottom="@dimen/margin_15dp"
                android:layout_marginLeft="@dimen/margin_10dp"
                android:text="12:24" />
    
            <TextView
                android:id="@+id/index_persent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_gravity="center"
                android:layout_margin="@dimen/margin_10dp"
                android:layout_toLeftOf="@id/head_zixun_item"
                android:drawablePadding="@dimen/margin_8dp"
                android:drawableRight="@mipmap/pinglun"
                android:text="13245" />
        </RelativeLayout>
    </RelativeLayout></span>
    7、java editText 设置密码显示与否
  • mPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);     //显示密码
    <pre name="code" class="java">mPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);     //显示小圆点

    8、scroolView与listview连用抢占焦点(每次进入的时候都是默认焦点在listview默认选项中)
  • (1)在Scroolview布局中最外层中添加一行代码即可
  • <pre name="code" class="html"><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    
        >
    
        <!--android:descendantFocusability="blocksDescendants"  Scrollview与listview一进入就抢占焦点解决-->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:descendantFocusability="blocksDescendants">


     
  • <pre name="code" class="html"><ListView
                android:id="@+id/class_listview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/my_class_txt" />

       (2)在listview的item中设置两个属性(但是listview也变为了不可点击,效果不好)
  •  android:focusable="true"
            android:focusableInTouchMode="true"
    

    9、

    代码里中文显示正常,真机运行后中文显示乱码,解决办法:

    build.gradle中添加一句

    android {
        compileOptions.encoding = "GBK"
    }

    10、使用edittext软件盘往上顶的问题
  • (1)在    manifest中  添加(打开页面是自动弹出软键盘)
    android:windowSoftInputMode="adjustResize"
     (2)在    manifest中  添加(打开页面是不弹出软键盘)
  • android:windowSoftInputMode="adjustUnspecified|stateHidden"
  • xml文件中 想让哪部分往上顶就在外部添加一个scrollView,例如我只让edittext往上顶,示例代码如下:

  • <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <RelativeLayout
            android:id="@+id/title_layout"
            android:layout_width="match_parent"
            android:background="@android:color/holo_orange_dark"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:padding="10dp"
                android:text="标题栏"/>
        </RelativeLayout>
        
                <ScrollView
                    android:id="@+id/scroll_layout"
                    android:layout_width="match_parent"
                    android:layout_alignParentBottom="true"
                    android:layout_height="wrap_content">
                <EditText
                        android:id="@+id/edit_txt"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                </ScrollView>
                <ListView
                    android:id="@+id/list_test"
                    android:layout_below="@+id/title_layout"
                    android:layout_above="@+id/scroll_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
    </RelativeLayout>
    

    11、自定义view中绘制矩形,继承View(我继承过Textview和EditText都绘制不出来)
  • /**
             * 以左上角为坐标原点(左边为y起始边(点),上边为x轴起始边(点)),
             * left  距离左边的y轴线   (矩形的左边)
             * top,  距离顶部的x轴线  (矩形的上边)
             * right   距离左边的y轴线 (矩形的右边)
             * bottom   距离顶部的x轴线(矩形的下边)
             *
             * 以此画矩形
             */
            RectF rectF = new RectF();
            rectF.left = 100;
            rectF.top = 100;
            rectF.right = 470;    //
            rectF.bottom = 200;
            canvas.drawRoundRect(rectF,0,0,mPaint);
    12、Android 跳转界面出现闪一下的问题(我这儿出现这个问题的原因是:在子线程中调用了startActivity(),并且finish造成闪一下的原因)
  • 解决办法:
  • 跳转界面时放在主线程中跳转。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值