Android开发之EditText属性详解

1、EditText输入的文字为密码形式的设置

(1)通过.xml里设置:

 把该EditText设为:android:password="true" // 以”.”形式显示文本

(2)在代码里设置:

通过设置EditText的setTransformationMethod()方法来实现隐藏密码或这显示密码。

editText.setTransformationMethod(PasswordTransformationMethod.getInstance());//设置密码为不可见。

2、(1)EditText输入的文字为电话号码

Android:phoneNumber=”true”  //输入电话号码

3、EditText字数限制的设置

(1)在.xml中设置:android:maxLength=“50” 

(2)代码中设置:   

editText.setFilters(new InputFilter[]{newInputFilter.LengthFilter(100)});

4、EditText设置字体

android:typeface="monospace" //设置字型。字形有:normal, sans, serif,monospace

5、EditText是否可编辑

Android:editable // 是否可编辑

6、在EditText中软键盘的调起、关闭

(1)EditText有焦点(focusable为true)阻止输入法弹出

 editText=(EditText)findViewById(R.id.txtBody);

   editText.setOnTouchListener(new OnTouchListener(){  

         public boolean onTouch(View v, MotionEvent event){ 

            editText.setInputType(InputType.TYPE_NULL); //关闭软键盘     

            return false;

         }

    });

(2)当EidtText无焦点(focusable=false)时阻止输入法弹出

 InputMethodManager imm =

(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); 

 imm.hideSoftInputFromWindow(editText.getWindowToken(),0);

(3)调用数字键盘并设置输入类型和键盘为英文

etNumber.setInputType(InputType.TYPE_CLASS_NUMBER); //调用数字键盘

rlEditText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);//设置输入类型和键盘为英文
或者:android:inputType="textUri|textMultiLine"

(4)android:focusable="false"//键盘永远不会弹出


<activity android:name=".AddLinkman"android:windowSoftInputMode="adjustUnspecified|stateHidden"/>//不自动弹出键盘

 

//关闭键盘(比如输入结束后执行)
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(etEditText.getWindowToken(), 0);

 

//自动弹出键盘

((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);

etEditText.requestFocus();//让EditText获得焦点,但是获得焦点并不会自动弹出键盘

7、android:layout_gravity和android:gravity的区别

(1)android:layout_gravity是本元素对父元素的重力方向。

(2)android:gravity是本元素所有子元素的重力方向。

8、android:padding和android:layout_margin区别

这两个都可以设置边距,但有细微的区别:

(1)android:padding是相对父view的边距

(2)android:layout_margin是相对同一级View的边距

例:LinearLayout是水平布局,下面有两个按钮,

(a)如果右边的按钮想距左边的按钮15px,因为这两个按钮是同一级的,应该用android:layout_margin;

(b)如果右边的按钮想距左边的距离为350px,应该用android:padding

9、android:numeric//只接受数字

android:numeric来控制输入的数字类型,一共有三种分别为integer(正整数)、signed(带符号整数,有正负)和decimal(浮点数)。

10、Enter键图标的设置

软键盘的Enter键默认显示的是“完成”文本,我们知道按Enter建表示前置工作已经准备完毕了,要去什么什么啦。比如,在一个搜索中,我们输入要搜索的文本,然后按Enter表示要去搜索了,但是默认的Enter键显示的是“完成”文本,看着不太合适,不符合搜索的语义,如果能显示“搜索”两个字或者显示一个表示搜索的图标多好。事实证明我们的想法是合理的,Android也为我们提供的这样的功能。通过设置android:imeOptions来改变默认的“完成”文本。这里举几个常用的常量值:

(1)actionUnspecified未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED效果:

(2)actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE效果:

(3)actionGo去往,对应常量EditorInfo.IME_ACTION_GO 效果:

(4)actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH效果: 

(5)actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND效果:

(6)actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT效果:

(7)actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE效果:

 

11、使用android:imeOptinos可对Android自带的软键盘进行一些界面上的设置:

android:imeOptions="flagNoExtractUi" //使软键盘不全屏显示,只占用一部分屏幕
同时,这个属性还能控件软键盘右下角按键的显示内容,默认情况下为回车键
android:imeOptions="actionNone" //输入框右侧不带任何提示
android:imeOptions="actionGo"   //右下角按键内容为'开始'
android:imeOptions="actionSearch" //右下角按键为放大镜图片,搜索
android:imeOptions="actionSend"   //右下角按键内容为'发送'
android:imeOptions="actionNext"  //右下角按键内容为'下一步'
android:imeOptions="actionDone" //右下角按键内容为'完成'

12、限定edittext能输入数字和字母,并且默认输入为数字,如身份证号码

android:inputType="number"
android:digits="0123456789xyzXYZ"

13、软键盘的调起导致原来的界面被挤上去,或者导致界面下面的tab导航被挤上去,解决方法如下

解决方法:

使用Manifest中的Activity的android:windowSoftInputMode的"adjustPan"属性。

另外注意:有关软键盘的问题可参考android:windowSoftInputMode中属性。

14、edittext光标详解
edittext.requestFocusFromTouch();//让光标放入到点击位置。
edittext.requestFocus();//默认方式获得焦点

EditText editor = (EditText)getCurrentView();//光标处插入
int cursor = editor.getSelectionStart();
editor.getText().insert(cursor,delta);

 

让光标移到末端(这样文字就会向前显示)
EditText et = ...
String text = "text";
et.setText(text);
et.setSelection(text.length());

 

android:cursorVisible="false" 隐藏光标

android:background="#00000000"//不要文本框背景 

默认的EditView是无法显示图片的。所以想要实现这个功能得需要我们自己为其添加一个方法。

在这里我们采用SpannableString和ImageSpan两个类来实现这一功能。

先上效果图: 

main.xml布局文件。我们使用自己定义的EditText

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<easy.stu.MyEditText
android:id="@+id/myEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="insert" />

</LinearLayout>

MyEditText.java

package easy.stu;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.widget.EditText;

public class MyEditText extends EditText {
    public MyEditText(Context context) {
        super(context);
    }
    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public void insertDrawable(int id) {
        final SpannableString ss = new SpannableString("easy");
        //得到drawable对象,即所要插入的图片
        Drawable d = getResources().getDrawable(id);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        //用这个drawable对象代替字符串easy
        ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
        //包括0但是不包括"easy".length()即:4。[0,4)。值得注意的是当我们复制这个图片的时候,实际是复制了"easy"这个字符串。
        ss.setSpan(span, 0, "easy".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        append(ss);
    }
}

MyActivity.java

package easy.stu;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MyActivity extends Activity {
    /** Called when the activity is first created. */
    Button b;
    MyEditText e;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b = (Button) findViewById(R.id.myButton);
        e = (MyEditText) findViewById(R.id.myEdit);
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                e.insertDrawable(R.drawable.easy);
            }
        });
    }
}

附上代码下载地址:

http://download.csdn.net/detail/leasystu/4059116

 

 

1、输入法Enter键图标的设置:

软件盘的界面替换只有一个属性android:imeOptions,这个属性的可以取的值有normal,actionUnspecified,actionNone,actionGo,actionSearch,actionSend,actionNext,actionDone,例如当值为actionNext时enter键外观变成一个向下箭头,而值为actionDone时enter键外观则变成了“完成”两个字。 
我们也可以重写enter的事件

 

软键盘的Enter键默认显示的是“完成”文本,通过设置android:imeOptions来改变默认的“完成”文本。这里举几个常用的常量值:

actionUnspecified  未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.  
actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE 
actionGo 去往,对应常量EditorInfo.IME_ACTION_GO
actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH    
actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND   
actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT   
actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE  

 

(EditorInfo.inputType & EditorInfo.TYPE_CLASS_MASK)可以是许多不同的值,包括: 
TYPE_CLASS_NUMBER 
TYPE_CLASS_DATETIME 
TYPE_CLASS_PHONE 
TYPE_CLASS_TEXT

 

2、事件捕捉处理:

可以通过setOnEditorActionListener设置事件处理。

final EditText input = new EditText(this);   
input.setSingleLine(true); //android:singleLine=”true”  
   input.setImeOptions(EditorInfo.IME_ACTION_SEND);  
   input.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_PASSWORD);  
   input.setOnEditorActionListener(new TextView.OnEditorActionListener() {  
       public boolean onEditorAction(TextView v, int actionId,    
               KeyEvent event)  {    
        Log.d(TAG, ""+actionId+","+event);  
           if (actionId==EditorInfo.IME_ACTION_SEND  
                ||(event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER)) {    
            //do something;  
            return true;  
           }    
           return false;    
       }    
   });

3、editor密码隐藏,怎么写?

有2种方法处理:

代码方法:input.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_PASSWORD);

layout配置方法:android:inputType="textPassword"

 

4、activity加载完成后,edit输入框会自动弹出输入法,可以通过以下代码屏蔽:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

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

 

6、让 EditText失去焦点,使用EditText的clearFocus方法 
例如:EditText edit=(EditText)findViewById(R.id.edit); 
           edit.clearFocus();

 

7、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>

8、设置光标到指定位置

EditText et = (EditText) findViewById(R.id.etTest);
et.setSelection(2);
//设置光标不显示,但不能设置光标颜色
et.setCursorVisible(false);

//获得焦点时全选文本
et.setSelectAllOnFocus(true);

et.requestFocus(); //请求获取焦点
et.clearFocus(); //清除焦点

使用EditText的setError提示
et.setError("邮箱"); 
  

自定义图标的setError提示
Drawable dr = getResources().getDrawable(R.drawable.ic_launcher);
dr.setBounds(0, 0, 10, 10); //必须设置大小,否则不显示
et.setError("有错误提示", dr);

et.setInputType(InputType.TYPE_CLASS_PHONE);//只能输入电话号码
et.setInputType(InputType.TYPE_CLASS_NUMBER);
//只能输入数字
et.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);//只能输入邮箱地址
et.setInputType(InputType.TYPE_NULL); // 禁止输入(不弹出输入法)

XML实现案例
<EditText android:id="@+id/etTest" android:inputType="number"
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/>

 

转 : http://www.cnblogs.com/zyw-205520/archive/2013/02/15/2912865.html

转 :http://blog.csdn.net/leasystu/article/details/7271814

转 : http://blog.csdn.net/tianxiangshan/article/details/8045119

posted @ 2013-04-09 14:04  编程小爬虫  阅读(1355)  评论(0编辑  收藏  举报