每次输入后键盘都会自动复位

时间:2017-10-14 10:45:53

标签: android textwatcher

到目前为止,我认为这与TextWatcher类有关。

每当我开始输入EditText框字段时,键盘就会自行重置。

如果我想输入一个完整的大写单词,我必须在输入每个单词后再次按下我的大写字母。与数字相同,当我按任意数字后切换到数字输入时,键盘会回到正常的qwerty pad。

我希望键盘在输入时保持一致。

感谢任何帮助。

以下是我的TextWatcher班级。

public class MyTextWatcher implements TextWatcher {
    private static final String TAG = "MyTextWatcher";
    private View view;
    EditText qtyView;

    public MyTextWatcher(View view) {
        this.view = view;
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        Log.d(TAG, "beforeTextChanged: " + "before");

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        Log.d(TAG, "onTextChanged: " + "onTextChanged");

    }

    public void afterTextChanged(Editable s) {
        String qtyString = s.toString().trim();

        qtyView = (EditText) view.findViewById(R.id.edtFillupDetails);
        ListViemItems listItems = (ListViemItems) qtyView.getTag();

        if (!listItems.getEditTextVal().equals(qtyString)) {

            listItems.setEditTextVal(qtyString);
            if (!listItems.getEditTextVal().equals("")) {

                int position = qtyView.getSelectionStart();
                qtyView.setText(listItems.getEditTextVal());

                qtyView.setSelection(qtyString.toString().trim().length());

            } else {
                qtyView.setText("");
            }
        }

        return;
    } 
}

我的XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="7dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/txtFillupDetails"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1.5"
            android:gravity="start|center"
            android:textColor="#333"
            android:textSize="16dp" />

        <EditText
            android:id="@+id/edtFillupDetails"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2.5"
            android:background="@drawable/edit_text_croner"
            android:clickable="true"
            android:padding="@dimen/registrnpadding"
            android:shadowRadius="2" />

    </LinearLayout>
</LinearLayout>

我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.lunetta.etro.e_tro">

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo1"
        android:label="@string/app_name"
        android:roundIcon="@drawable/logo1"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"
            android:windowSoftInputMode="adjustResize|stateHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".Fillup_Details"
            android:label="Pre-Departure Details"
            android:windowSoftInputMode="adjustPan">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>       
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

在另一个帖子中找到了解决方案:

edittext.settext() changes the keyboard type to default [ from ?123 to ABC]

问题出在刷新键盘的代码的 editText.setText(text) 部分。要修复它,试试这个:

editText.getText().clear()
editText.append(text)

希望能帮助到任何人!)