【问题标题】:Android SetText changes keyboard typeAndroid SetText 更改键盘类型
【发布时间】:2012-07-11 09:01:39
【问题描述】:

我正在开发一个包含EditText 的android 应用程序。

我通过调用来控制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

我该如何解决这个问题?

【问题讨论】:

  • 请添加更多代码。我试试这个并得到“java.lang.StackOverflowError”
  • 检查编辑..我添加了更多代码
  • 您要使用特殊键盘吗?
  • 我认为stackoverflow.com/a/26373852/845935 是更好的解决方案。为我工作。

标签: android keyboard settext


【解决方案1】:

我无法为您的问题找到直接的解决方案,但您可以做一些事情来解决您的问题!使用编辑文本上的文本视图(例如,您可以使用 RelativeLayout 在编辑文本上设置文本视图),并将编辑文本字体颜色设置为白色(或编辑文本的背景颜色),以便用户看不到它的真实文本。我希望这个 sn-p 对你有帮助:
ma​​in.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");
            }
        });
    }


}

【讨论】:

    猜你喜欢
    • 2012-04-22
    • 1970-01-01
    • 2011-07-28
    • 2014-08-06
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    相关资源
    最近更新 更多