安卓常用控件使用

2021-09-24  本文已影响0人  雨邪

1.EditText默认不弹出输入法,在xml布局父布局添加以下属性

android:focusable="true"
android:focusableInTouchMode="true"
或者AndroidManifest.xml-->activity节点中添加stateHidden

android:windowSoftInputMode="adjustResize"和
android:windowSoftInputMode="adjustPan"会自动弹出软键盘

<activity android:name=".MainActivity"
   android:windowSoftInputMode="adjustResize|stateHidden">
</activity>

2.EditText常用操作

editText.setText("123");//设置EditText控件的内容
editText.setSelection("123".length());//将光标移至文字末尾(需要放setInputType后)
editText.requestFocus();//获取焦点
editText.clearFocus();//失去焦点

3.修改回车为完成

        //把软键盘的回车,修改为完成,并监听点击事件
        editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
        editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    //隐藏输入框
                    KeyboardUtils.hideSoftInput(SelectListActivity.this);
                    //搜索处理
                    next();
                    return true;
                }
                return false;
            }
        });

4.TextView可滚动

        //把软键盘的回车,修改为完成,并监听点击事件
        editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
        editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    KeyboardUtils.hideSoftInput(SettingActivity.this);
                    next();
                    return true;
                }
                return false;
            }
        });

5.圆角边框背景stroke_bg_3b9dff_1dp.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 边框 -->
    <stroke
        android:width="1dp"
        android:color="#3b9dff" />
</shape>

6.圆角背景round_bg_3b9dff_5dp.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
   <!-- 圆角 -->
    <solid android:color="#3b9dff" />
    <corners android:radius="5dp" />
</shape>
上一篇下一篇

猜你喜欢

热点阅读