EditText的属性介绍

EditText去边框,去下划线
EditText的background属性设置为@null就搞定了:android:background=”@null”

EditText不自动获取焦点
在EditText的父Layout中,加入下面的两个属性即可:
android:focusable=”true”
android:focusableInTouchMode=”true”

设置光标位置
EditText的setSelection(int length)光标位置

光标颜色
EditText有一个属性:android:textCursorDrawable,这个属性是用来控制光标颜色的
android:textCursorDrawable=’@null’,’@null’作用是让光标颜色和text color一样

只能输入数字或者某些特定字符
如何设置EditText,使得只能输入数字或者某些字母呢?

一、设置EditText,只输入数字:

方法1:直接生成DigitsKeyListener对象就可以了。

editText.setKeyListener(new DigisKeyListener(false,true));

方法2:在EditText中设置属性,android:numeric=”integer”即只能输入整数,如下

<EditText
    android:singleLine="true"
    android:numeric="integer"
/>

方法3:新建一个char[],在里面添加允许输入的字符。如下

editText.setKeyListener(new NumberKeyListener(){
    protected char[] getAcceptedChars(){
        return new char[]{'1','2','3','4','5','6','7','8','9','0'};
    }
    public int getInputType() {    //这个方法是控制输入法键盘的,这样输入法就只会提供电话号码的键盘
        return android.text.InputType.TYPE_CLASS_PHONE;
    }
 });

二、设置EditText只能输入某些字母,如下面设置edtitext只能输入A—D,a—f这些字母。方法如下:

editText.setKeyListener(new NumberKeyListener(){
    protected char[] getAcceptedChars()     //这个方法是返回自定义的字符的,这样在手机输入时,只有定义的字符才能写到EditText中,其他的字符是写不进去的
    {
        char numberChars[]={'a','b','c','d','e','f','A','B','C','D'};
        return numberChars;
    }
    public int getInputType() {     //控制输入法键盘的,不要控制的话就返回0就好了
        return 0;
   }
});

EidtText属性android:scrollbars=”vertical” 的神奇
下面两张图的别就是因为在内容的EditText中加了一个属性:android:scrollbars=”vertical”
前:
使用前
内容的EditText中加了一个属性:android:scrollbars=”vertical”
后:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值