EditTextView的介绍和使用
2016-05-06 13:06
531 查看
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
<EditText
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入单位"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:singleLine="true"
android:imeOptions="actionSearch"
/>
对于事件捕捉:(像下一项等一些就不用设置什么事件的捕捉了)
[java] view
plain copy
companySearchET.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
final String searchStr = companySearchET.getText().toString().trim();
if (actionId==EditorInfo.IME_ACTION_SEARCH
||(event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER)) {
//do something;
if(searchStr == null || "".equals(searchStr)){
Toast.makeText(CompanyChangeActivity.this, "请输入单位关键字", Toast.LENGTH_SHORT).show();
Log.e("这里", "这里");
return true;
}else{
}
return true;
}
return false;
}
});
参考:/article/1560725.html
相关文章推荐
- Oracle中查询各种对象的方法小结
- 第1章 UML基础:类的关系
- 第十周-点类族设计项目1
- 我经历的IT公司面试及离职感受
- Yii2.0 rules验证规则大全
- web.xml 配置
- ExtJS自定义控件 之一:datetimefield控件
- 上传文件功能-1
- 二叉树的非递归遍历
- NYOJ 题目77 开灯问题
- 2、Ajax与Java通过POST方式交互
- iOS开发-进阶:语言国际化及代码切换语言
- img图片下多余空白解决方案
- 【HUSTOJ】1072: 拐角V
- Android 打造编译时注解解析框架 这只是一个开始
- html页面中显示审查元素时Element.style
- Ubuntu&红帽的vim通用配置(文件头+一键编译执行C/C++/Java单文件)
- 《C++精英内参-程序员高效指南》-6笔记本键盘的选择
- Java笔试面试题整理第六波(修正版)
- 写出内容到xml文档