【小知识点总结】EditText的属性设置

android:digits="1234567890.+-*/%\n()"
限制输入框中只能输入自己定义的这些字符串 如果输入其它将不予以显示

android:numeric="integer"
限制输入框中只能输入数字

android:phoneNumber="true"
限制输入框中只能输入手机号码

android:password="true"
限制输入框中输入的任何内容将以"*"符号来显示

android:hint="默认文字"
输入内容前默认显示在输入框中的文字

android:textColorHint="#FF0000"
设置文字内容颜色

android:enabled="false"
设置输入框不能被编辑

如果还有一些特殊的限制,比如我做一个项目只能输入数字,且输入0之后再输入1,则只显示1,这就需要单独去进行设置了,也非常的简单。
给EditText添加一个监听事件,当检测到里面的内容变化以后,根据需求,修改相关的内容就可以了。
使用EditText的addTextChangedListener(TextWatcher watcher)方法对EditText实现监听,TextWatcher是一个接口类,所以必须实现TextWatcher里的抽象方法

当EditText里面的内容有变化的时候,触发TextChangedListener事件,就会调用TextWatcher里面的抽象方法。
public class MainActivity extends Activity {
    private EditText text;
    String str;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        text = (EditText)findViewById(R.id.text);
        text.addTextChangedListener(textWatcher);
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            Log.d("TAG","afterTextChanged--------------->");
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
            Log.d("TAG","beforeTextChanged--------------->");
        }
         @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            Log.d("TAG","onTextChanged--------------->");
            str = text.getText().toString();
            try {
                //if ((heighText.getText().toString())!=null)
                Integer.parseInt(str);
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    };
}

该方法可以监听到Edittext的变化,我在onTextChanged里面监听s值得变化,然后做修改以后再setText到EditText里面,不过这时候经常会遇见光标跑到最前面的情况,很恶心,随意每次setText的时候都需要用ev.setSelection(str.length())去重新设置光标位置为str字符串的最后。


==========================================================================

设置EditText,只输入数字,或者特定字符:

方法1:生成DigitsKeyListener对象
editText.setKeyListener(new DigitsKeyListener());

方法2:在EditText中设置属性,android:numeric="integer"即只能输入整数,如下 
<EditText 
android:text="" 
android:layout_width="150dip"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:textSize="20dip"
android:numeric="integer"/>

方法3:新建一个char[],在里面添加允许输入的字符。如下
editText.setKeyListener(new NumberKeyListener(){
protected char[] getAcceptedChars(){
char[] mychar = {'a','b','c','1','2','3'};
return mychar;
}
});

另:“请输入用户名”、“请输入密码”,在输入框中经常会有这些提示信息告诉用户这里输入的是什么内容,因此在Android中对应的为输入框控件EditText提供了提示信息的功能。
输入框提示信息设置有两种方法,分别是布局中定义,和代码中实现。
A、布局中定义提示信息android:hint="输入名称"
B、代码中实现提示信息editText.setHint("输入名称") ;
通过以上任一种方式的设定后,提示信息将会以黯淡的颜色显示在输入框中,当用户点击时候提示信息就会自动消失


==========================================================================

EditText不显示光标怎么解决

解决方法有以下几种
1.在Edittext中加入以下属性
android:cursorVisible="true"

android:textCursorDrawable="@null"

2.在Edittext中加入以下属性
android:cursorVisible="true"android:textCursorDrawable="@drawable/test_cursor"
对应的drawable文件
<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">    

<size android:width="1dp" />

<span style="font-family: Arial, Helvetica, sans-serif;">

<!-- 光标宽度可以自己定义 --></span>    

<solid android:color="#008000"  />

<!-- 光标颜色可以自己定义 --></shape>

3.如果以上没有效果就请用这个,
明确指定EditText的inputType属性值inputType属性中的textCapSentences
不要用这个,国内手机好像没有用到这个,个人证实而已,用text或者textMultiLine
android:inputType="text|textMultiLine"

本人用的是第1解决方法

==========================================================================

EditText属性描述
android:layout_gravity="center_vertical"//设置控件显示的位置:默认top,这里居中显示,还有bottom
android:hint="请输入数字!"//设置显示在空间上的提示信息
android:numeric="integer"//设置只能输入整数,如果是小数则是:decimal
android:maxLength="8"  //限制输入长度为8
android:singleLine="true"//设置单行输入,一旦设置为true,则文字不会自动换行。
android:gray="top" //多行中指针在第一行第一位置et.setSelection(et.length());//调整光标到最后一行
android:autoText //自动拼写帮助
android:capitalize //首字母大写
android:digits //设置只接受某些数字
android:singleLine //是否单行或者多行,回车是离开文本框还是文本框增加新行
android:numeric //只接受数字
android:password //密码
android:phoneNumber // 输入电话号码
android:editable //是否可编辑
android:autoLink=”all” //设置文本超链接样式当点击网址时,跳向该网址
android:password="true"//设置只能输入密码
android:textColor = "#ff8c00"//字体颜色
android:textStyle="bold"//字体,bold, italic, bolditalic
android:textSize="20dip"//大小
android:capitalize = "characters"//以大写字母写
android:textAlign="center"//EditText没有这个属性,但TextView有
android:textColorHighlight="#cccccc"//被选中文字的底色,默认为蓝色
android:textColorHint="#ffff00"//设置提示信息文字的颜色,默认为灰色
android:textScaleX="1.5"//控制字与字之间的间距
android:typeface="monospace"//字型,normal, sans, serif, monospace
android:background="@null"//空间背景,这里没有,指透明
android:layout_weight="1"//权重 在控制控   件显示的大小时蛮有用的。
android:textAppearance="?android:attr/textAppearanceLargeInverse"//文字外观,这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。


转载地址:http://blog.csdn.net/victoryckl/article/details/8656928

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值