Android SetText changes keyboard type
Asked Answered
A

1

2

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?

Azide answered 11/7, 2012 at 9:1 Comment(4)
Please add more code.I try this and get "java.lang.StackOverflowError"Cruise
check the edit .. I've added more codeAzide
Do you want to use special keyboard?Cruise
I think https://mcmap.net/q/691187/-edittext-settext-changes-the-keyboard-type-to-default-from-123-to-abc is better solution. Works for me.Epiboly
C
2

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


}
Cruise answered 12/7, 2012 at 3:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.