Android SetText changes keyboard type

2019-02-25 20:56发布

问题:

I'm developing an android application which contains an 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");
    }
});

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?

回答1:

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>     

Activity that you observe text changes:

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");
            }
        });
    }


}