2

を含むAndroidアプリケーションを開発していますEditText

私はeditTextに書かれていることを呼び出すことによって制御します

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("some Text"); する問題は、テキストが変更された後、キーボードも変更されることです。たとえば、symbolキーボードを開いてsetTextを使用すると、自動的にに変更されqwertyます。

どうすればこの問題を解決できますか?

4

1 に答える 1

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


}
于 2012-07-12T03:14:58.093 に答える