EditTextView的介绍和使用

EditText类的基本结构

EditText 和TextView 的功能基本类似,他们之间的主要区别在于EditText 提供了可编辑的文本框。
类的继承关系图:java.lang.Object ------android.view.View----android.widget.TextView------android.widget.EditText

直接子类:AutoCompleteTextView, ExtractEditText

间接子类:MultiAutoCompleteTextView

常用的用法


标签的主要属性

设置输入的类型的 android:inputType=""

<span style="font-size:14px;">android:inputType="textCapSentences"//仅第一个字母大小
android:inputType="textAutoCorrect"android:inputType="textAutoComplete"//前两个自动完成
android:inputType="textMultiLine"//多行输入
android:inputType="textImeMultiLine"//输入法多行(不一定支持)
android:inputType="textNoSuggestions"//不提示
android:inputType="textUri"//URI格式
android:inputType="textEmailAddress"//电子邮件地址格式
android:inputType="textEmailSubject"//邮件主题格式
android:inputType="textShortMessage"//短消息格式
android:inputType="textLongMessage"android:inputType="textPersonName"//人名格式
android:inputType="textPostalAddress"//邮政格式
android:inputType="textPassword"//密码格式
android:inputType="textVisiblePassword"//密码可见格式
android:inputType="textWebEditText"//作为网页表单的文本格式
android:inputType="textFilter"//文本筛选格式
android:inputType="textPhonetic"//拼音输入格式
android:inputType="number"//数字格式
android:inputType="numberSigned"//有符号数字格式
android:inputType="numberDecimal"//可以带小数点的浮点格式
android:inputType="phone"//拨号键盘
android:inputType="datetime"android:inputType="date"//日期键盘
android:inputType="time"//时间键盘</span>

使用:

(1)xml中的使用:

<span style="font-size:14px;"><!-- 用于输入数字的文本框 -->
<EditText android:id="@+id/editText1" android:inputType="date"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:maxLength="40" android:hint="输入电话号码" android:textColorHint="#FF000000"
android:phoneNumber="true" android:imeOptions="actionGo"></EditText>
<!-- 用于输入密码的文本框 -->
<EditText android:id="@+id/editText2" android:inputType="date"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:maxLength="40" android:hint="输入密码" android:textColorHint="#FF000000"
android:password="true" android:imeOptions="actionSearch"></EditText></span>

(2)EditText的常用的监听之addTextChangedListener(new TextWatcher())

这里例子:监听edittext的字数超过140设置监听

<span style="font-size:14px;">EditText content;//定义一个文本输入框
TextView hasnum;// 用来显示剩余字数
int num =140;//限制的最大字数  
 content =(EditText) findViewById(R.id.et_content);
 hasnumTV =(TextView) findViewById(R.id.tv_num);
 hasnumTV.setText(num+"");

//下面为EditText文本框添加监听

 content.addTextChangedListener(newTextWatcher(){
privateCharSequence temp;
privateint selectionStart;
privateint selectionEnd;
publicvoid beforeTextChanged(CharSequence s,int start,int count,int after){
}

publicvoid onTextChanged(CharSequence s,int start,int before,int count){
   temp = s;
}

publicvoid afterTextChanged(Editable s){
int number = num - s.length();
   hasnumTV.setText(""+ number);
   selectionStart = content.getSelectionStart();
   selectionEnd = content.getSelectionEnd();
if(temp.length()> num){
    s.delete(selectionStart -1, selectionEnd);
int tempSelection = selectionEnd;
    content.setText(s);
    content.setSelection(tempSelection);//设置光标在最后
}
}
});</span>

edittext和软件盘的弹出关系

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);

EditText始终不弹出软件键盘

例:EditText edit=(EditText)findViewById(R.id.edit);
       edit.setInputType(InputType.TYPE_NULL);

相对布局中位于底部的控件不让他被软键盘弹出(底部的控件位于软键盘的顶部)

在所在activity上加 <activity android:name=".activity.wode.MeDetailsActivity"  android:windowSoftInputMode="adjustPan"/>
(注意,此时在activity的内容中 不能设置下面类似的东西
//设置软件盘不弹出
  //getActivity().getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);)

EditText更改软件盘的enter键的功能

EditText通过设置 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
注意:xml中必须要加上 android:singleLine="true" 才行
xml的写法
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <EditText   
  2.         android:layout_marginTop="10dp"  
  3.         android:layout_width="match_parent"  
  4.         android:layout_height="wrap_content"  
  5.         android:hint="输入单位"  
  6.         android:layout_marginLeft="10dp"  
  7.         android:layout_marginRight="10dp"  
  8.         android:singleLine="true"   
  9.         android:imeOptions="actionSearch"  
  10.         />  

对于事件捕捉:(像下一项等一些就不用设置什么事件的捕捉了)

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. companySearchET.setOnEditorActionListener(new TextView.OnEditorActionListener() {    
  2.            public boolean onEditorAction(TextView v, int actionId,      
  3.                    KeyEvent event)  {      
  4.                final String  searchStr = companySearchET.getText().toString().trim();  
  5.                if (actionId==EditorInfo.IME_ACTION_SEARCH    
  6.                     ||(event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER)) {      
  7.                 //do something;    
  8.                    if(searchStr == null || "".equals(searchStr)){  
  9.                         Toast.makeText(CompanyChangeActivity.this"请输入单位关键字", Toast.LENGTH_SHORT).show();  
  10.                         Log.e("这里""这里");  
  11.                         return true;  
  12.                     }else{  
  13.                     }  
  14.                 return true;    
  15.                }      
  16.                return false;      
  17.            }      
  18.        });   
参考: http://blog.csdn.net/howlaa/article/details/36895021

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值