美文网首页
EditText的使用体验

EditText的使用体验

作者: 我是你森哥哥 | 来源:发表于2017-06-07 21:51 被阅读0次

1.Android软键盘的监听
actionNone : 回车键,按下后光标到下一行
actionGo : Go,
actionSearch : 放大镜
actionSend : Send 发送
actionNext : Next 下一项
actionDone : Done,确定/完成,隐藏软键盘,即使不是最后一个文本输入框

    android:singleline="true"    必须加上这两行
    android:imeOptions="actionDone"
    EditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {});设置监听

   et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                startActivity(new Intent(getContext(), SearchActivity.class).putExtra("search", et_search.getText().toString()));
                return false;
            }
        });



@Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean isOK = true;
        switch (actionId) {
            case EditorInfo.IME_ACTION_NONE:
                Toast.makeText(mContext, "点击-->NONE", Toast.LENGTH_SHORT).show();
                break;
            case EditorInfo.IME_ACTION_GO:
                Toast.makeText(mContext, "点击-->GO", Toast.LENGTH_SHORT).show();
                break;
            case EditorInfo.IME_ACTION_SEARCH:
                Toast.makeText(mContext, "点击-->SEARCH", Toast.LENGTH_SHORT).show();
                break;
            case EditorInfo.IME_ACTION_SEND:
                Toast.makeText(mContext, "点击-->SEND", Toast.LENGTH_SHORT).show();
                break;
            case EditorInfo.IME_ACTION_NEXT:
                Toast.makeText(mContext, "点击-->NEXT", Toast.LENGTH_SHORT).show();
                break;
            default:
                isOK = false;
                break;
        }

2.设置输入文本类型inputType

//文本类型,多为大写、小写和数字符号。
android:inputType="none"
android:inputType="text"
android:inputType="textCapCharacters" 字母大写
android:inputType="textCapWords" 首字母大写
android:inputType="textCapSentences" 仅第一个字母大写
android:inputType="textAutoCorrect" 自动完成
android:inputType="textAutoComplete" 自动完成
android:inputType="textMultiLine" 多行输入
android:inputType="textImeMultiLine" 输入法多行(如果支持)
android:inputType="textNoSuggestions" 不提示
android:inputType="textUri" 网址
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" 时间键盘

3.输入框样式的改变

(1) 隐藏输入框和下划线
android:background="@null"或者
android:background="#00000000"或者
(2)修改光标的颜色
android:textCursorDrawable="@null"
当值为@null时,颜色与textcolor一致,可以自己修改颜色。
android:textCursorDrawable="@drawable/color_cursor"
(3)修改下划线的颜色

1.在你的build.gradle中添加最新的appcompat库
dependencies {
compile 'com.android.support:appcompat-v7:X.X.X' // X.X.X 为最新的版本号 
}

2.让你的activity继承android.support.v7.app.AppCompatActivity

public class MainActivity extends AppCompatActivity {
...
}

3.在任何layout.xml文件中声明您的EditText
<EditText 
android:layout_width="match_parent" 
android:layout_height="wrap_content"
android:hint="Hint text"/>

4.在styles.xml文件中声明自定义样式
<style name="MyEditText" parent="Theme.AppCompat.Light"> 
<item name="colorControlNormal">@color/green</item> 
<item name="colorControlActivated">@color/red</item>
</style>

5.通过android:theme属性将此样式应用于您的EditText
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hint text"
android:theme="@style/MyEditText"/>

修改下划线的另一种实现方式

修改全局EditText的下划线  不方便局部自定义
<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorAccent">@color/colorPrimary</item>
        <!-- AppCompatEditText默认状态状态设置底线颜色 -->
        <item name="colorControlNormal">#3F51b5</item>  
        <!-- AppCompatEditText选择的底线颜色 -->
        <item name="colorControlActivated">#c6174e</item>

    </style>

设置activity不自动弹出软键盘

<activity  

            android:name="smalt.manger.sms.SmsShowDetailItemsActivity"  

            android:windowSoftInputMode="stateHidden|stateAlwaysHidden" >  

</activity> 



设置  

android:focusable=""  

android:focusableInTouchMode=""  

属性为false 
```

相关文章

网友评论

      本文标题:EditText的使用体验

      本文链接:https://www.haomeiwen.com/subject/keuofxtx.html