安卓的setText改变键盘类型键盘、类型、setText

2023-09-07 15:53:22 作者:童话再美也只是童话而已

我开发一个Android应用程序,其中包含一个的EditText

I'm developing an android application which contains an EditText.

我控制什么是通过调用写在EDITTEXT

I control what is written in the editText by calling

et.addTextChangedListener(new TextWatcher() {
    public void afterTextChanged(Editable s) {
        String message = et.getText().toString();
        if(s.toString().compareTo(before)!=0){
            et.setText(message);
            et.setSelection(et.getText().length());
        }
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        before = et.getText().toString();
        System.out.println("After");
    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        System.out.println("ON");
    }
});

在afterTextChanged功能我用 et.setText(一些文本); 问题是,该文本更改键盘后也发生了变化,例如,如果符号键盘开了,我用的setText,它会自动更改为 QWERTY

In afterTextChanged function I use et.setText("some Text"); the problem is that,after the text is changed the keyboard also changed,for example if the symbol keyboard was opened and I used setText, it will automatically changed to qwerty.

我该如何解决这个问题呢?

How can I solve this problem?

推荐答案

我无法找到你的问题直接的解决方案,但你可以做一些事情,清楚您的问题!使用文本视图是在您编辑文本(例如,你可以使用RelativeLayout的设置上编辑文本的文本视图),并设置编辑文本字体颜色为白色(或编辑文字的背景色),使用户无法看到它是真实的文字。我希望这个片段可以帮助您: main.xml中(在RES /布局):

I could not find direct solution for your problem,but you can do some things that clear your problem! Use a text view that is on your edit text(for example you can use RelativeLayout to set text view on edit text),and set your edit text font color white(or edit text's background color)so user can not see it's real text.I hope this snippet help you: main.xml(in res/layout):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activityRoot"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MyTextView" />

</RelativeLayout>     

活动,您观察到的文字变化:

public class TestproGuardActivity extends Activity {

    EditText et;
    TextView tv1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv1 = (TextView)findViewById(R.id.textView1);
        et = (EditText)findViewById(R.id.editText1);

        et.addTextChangedListener(new TextWatcher() {
            private String before = "hasan";
            public void afterTextChanged(Editable s) {
               String message = et.getText().toString();
               if(s.toString().compareTo(before )!=0){
//               et.setText(message);
                   tv1.setText(message);
//               et.setSelection(et.getText().length());
               }
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after){

                before = et.getText().toString();

                System.out.println("After");   

            }
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                System.out.println("ON");
            }
        });
    }


}