天天看點

Android 常用布局控件 EditText(三)

有時候,需要對 EditText 中的文字進行一些操控,與監聽,下面我們來看看又那些吧!

1、禁止輸入、允許輸入

2、設定輸入限制

3、設定隻能輸入數字

4、光标位置設定

5、輸入監聽

6、預設進入界面不彈出輸入框

android:focusable="true"
android:focusableInTouchMode="true"      

實作代碼

activity_edit_text.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.EditTextActivity"
    android:focusable="true"
    android:focusableInTouchMode="true">
    <!--android:focusable="true"
        android:focusableInTouchMode="true"
        在 EditText 的父容器加入這兩個屬性,則預設不顯示光标-->

    <EditText
        android:id="@+id/et_input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:hint="控制能否輸入"/>
    <!--background @null控制下劃線為空-->

    <Button
        android:id="@+id/btn_no"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/et_input"
        android:text="不能輸入"/>

    <Button
        android:id="@+id/btn_yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/btn_no"
        android:text="可以輸入"/>

    <EditText
        android:id="@+id/et_decimal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/et_input"
        android:digits="0123456789."
        android:hint="隻能輸入:0123456789." />
    <!--通過 android:digits="0123456789."控制,隻能輸入 digits 裡的值(可以自定義) -->

    <EditText
        android:id="@+id/et_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        app:layout_constraintTop_toBottomOf="@+id/et_decimal"
        android:hint="隻能輸入正整數"/>
    <!--通過 android:inputType="number" 控制,隻能輸入number-->

    <EditText
        android:id="@+id/et_cursorr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/et_number"
        android:hint="光标位置設定"/>

    <EditText
        android:id="@+id/et_listener"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/et_cursorr"
        android:hint="輸入監聽"/>

</android.support.constraint.ConstraintLayout>
           

EditTextActivity.java

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.self.control.R;
import com.self.control.base.BaseActivity;

import butterknife.BindView;

public class EditTextActivity extends BaseActivity {

    @BindView(R.id.et_input)
    EditText etInput;
    @BindView(R.id.btn_no)
    Button btnNo;
    @BindView(R.id.btn_yes)
    Button btnYes;
    @BindView(R.id.et_decimal)
    EditText etDecimal;
    @BindView(R.id.et_number)
    EditText etNumber;
    @BindView(R.id.et_cursorr)
    EditText etCursorr;
    @BindView(R.id.et_listener)
    EditText etListener;

    @Override
    public void widgetClick(View v) {
        switch (v.getId()){
            case R.id.btn_no:
                //設定不能夠輸入了,設定預設不能擷取光标
                etInput.setFocusable(false);
                break;
            case R.id.btn_yes:
                //設定可以輸入設定可以擷取光标
                etInput.setFocusable(true);
                etInput.setFocusableInTouchMode(true);
                break;
        }
    }

    @Override
    public void setListener() {
        btnNo.setOnClickListener(this);
        btnYes.setOnClickListener(this);
    }

    @Override
    public void initParms(Bundle parms) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_edit_text;
    }

    @Override
    public void initView(View view) {
        //設定光标位置 ,可以設定在最前面 0, 和最後面 content.length()  最好配合監聽一起用(如下面)
        etCursorr.setSelection(0);

        //輸入監聽
        etListener.addTextChangedListener(new TextWatcher() {
            @Override//s 更改前的文字、 start 有變動的字元串的序号、 count 被改變的長度(如果是增加,則為0)、 after 被修改的文字的長度(加了幾個)如果是删除的話則為0
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.e("etListener1",s+" "+start+" "+count+" "+after);
            }

            @Override//s 更改後的文字、 start 有變動的字元串的序号、 before 被改變的長度(如果是增加,則為0)、 count 被修改的文字的長度(加了幾個)如果是删除的話則為0
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.e("etListener2",s+" "+start+" "+before+" "+count);
            }

            @Override//s 修改後的文字
            public void afterTextChanged(Editable s) {
                Log.e("etListener3",s+"");
                etListener.setSelection(0);
            }
        });
    }

    @Override
    public void doBusiness(Context mContext) {

    }

}
           

GitHub:https://github.com/iscopy/Control