【问题标题】:The keyboard resets itself after every single input每次输入后键盘都会自行重置
【发布时间】:2018-03-26 09:34:20
【问题描述】:

目前认为这与TextWatcher 类有关。

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

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

我希望键盘在打字时保持一致。

感谢任何帮助。

下面是我的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>

【问题讨论】:

  • 对我来说看起来不错。你在布局文件中使用过可聚焦和输入类型吗??
  • 我已经尝试了布局文件中的两个东西,但它们对此没有影响。
  • 好的。在 TextWatcher 的 afterTextChanged 中使用 editText.requestFocus()。它很脏,但可能对你有用。
  • 不,它没有。
  • 你能在这里发布你的布局xml和清单吗?

标签: android textwatcher


【解决方案1】:

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

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

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

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

希望能帮助任何人!)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多