EditText 属性大全 及 与软键盘 控制的关联

来源:互联网 发布:软软喵 知乎 编辑:程序博客网 时间:2024/04/29 16:42
 

EditText 属性

 

EditText继承关系:View-->TextView-->EditText。
EditText的属性很多,这里介绍几个:
android:layout_gravity="center_vertical"
设置控件显示的位置:默认 top,这里居中显示,还有bottom
android:hint="请输入数字!"
设置显示在空间上的提示信息
android:numeric="integer"
设置只能输入整数,如果是小数则是:decimal
android:singleLine="true"
设置单行输入,一旦设置为true,则文字不会自动换行。
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"

 

EditText始终不弹出软件键盘

 

1.EditText默认不弹出软件键盘
方法一:
在 AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为 adjustUnspecified|stateHidden
例如:<activity android:name=".Main"
                  android:label="@string/app_name"
                  android:windowSoftInputMode="adjustUnspecified|stateHidden"
                  android:configChanges="orientation|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
方法二:
让 EditText失去焦点,使用EditText的clearFocus方法
例如:EditText edit=(EditText)findViewById(R.id.edit);
           edit.clearFocus();
方法三:
强制隐藏Android输入法窗口
例如:EditText edit=(EditText)findViewById(R.id.edit);  
           InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
           imm.hideSoftInputFromWindow(edit.getWindowToken(),0);

2.EditText始终不弹出软件键盘
例:EditText edit=(EditText)findViewById(R.id.edit);
       edit.setInputType(InputType.TYPE_NULL);

 
 
 
设置EditText不可编辑
 

本来这个也不算个大问题,但我开发中确实遇到了,特开此贴,希望能帮到一些android新手。

可能有人会说,设置EditText不可编辑那还不简单,直接设置

Xml代码
  1. android:editable="false"  
view plaincopy to clipboardprint?
  1. <span style="font-size:16px;color:#ff0000;">android:editable="false"</span>  

 就可以了,如果你一直是这么做的,那么你就不用看下去了,在android2.2中直接设置  android:editable="false"是不起作用的,(我在模拟器和真机环境中测试都不起作用) 在android2.2中 editable是depreceated的,大家可以看看api文档http://developer.android.com/reference/android/R.attr.html#editable

 

我自己式了一下 但貌似可行  but以备后患

 

     那么我们是不是就不可以设置EditText的编辑状态呢,当然不是,我们可以设置

 

Xml代码
  1. android:focusable="false"  
view plaincopy to clipboardprint?
  1. <span style="font-size:16px;color:#ff0000;">android:focusable="false"</span>  

   这是什么意思呢,很简单,不让它获得输入焦点(不要告诉我你不知道什么是焦点),既然得不到焦点,当让就无法输入了,我在http://stackoverflow.com/questions/4824676/making-not-editable-edittext-component 上也到的这样的解决方式。

   那么为什么要这么做呢,为什么不要   android:editable了呢,这是一个用户体验的问题。android是一个移动平台,大多数是没有键盘的,都是屏幕软键盘,如果你设置EditText为不可编辑,那么就不应该打开软键盘,而不是打开软键盘了又无法输入。

 

   似乎到了这里就没有说的了,请你不要着急,这样设置后有一个问题,无法选中EditText的内容,因为它无法获得焦点 ,这个问题我也不知道怎么解决,希望大大们能给解决下。

 

    如何在java代码中控制EditText不可编辑呢,因为EditText没有setEditable这样的方法,这个问题坛子里已经有很多高手解决了,使用InputFilter

具体如下:

 

Java代码
  1. 你的EditText.setFilters(new InputFilter[] {   
  2.             new InputFilter() {   
  3.                 public CharSequence filter(CharSequence source, int start,   
  4.                         int end, Spanned dest, int dstart, int dend) {   
  5.                     return source.length() < 1 ? dest.subSequence(dstart, dend) : "";   
  6.                 }   
  7.             }   
  8.         });  
view plaincopy to clipboardprint?
  1. 你的EditText.setFilters(new InputFilter[] {  
  2.             new InputFilter() {  
  3.                 public CharSequence filter(CharSequence source, int start,  
  4.                         int end, Spanned dest, int dstart, int dend) {  
  5.                     return source.length() < 1 ? dest.subSequence(dstart, dend) : "";  
  6.                 }  
  7.             }  
  8.         });  
原创粉丝点击