2012-07-11 46 views
2

我正在开发一个包含EditText的Android应用程序。Android SetText更改键盘类型

我控制什么是通过调用

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功能写在EDITTEXT我用et.setText("some Text"); 的问题是,该文本更改键盘后也发生了变化,例如,如果symbol键盘是打开并使用了setText,它会自动更改为qwerty

我该如何解决这个问题?

+0

请添加更多code.I尝试这并得到“java.lang.StackOverflowError” – hasanghaforian 2012-07-11 09:33:44

+0

检查编辑..我添加了更多代码 – 2012-07-11 10:01:49

+0

你想使用特殊键盘吗? – hasanghaforian 2012-07-11 11:42:43

回答

2

我无法找到针对您问题的直接解决方案,但您可以做一些事情来清除您的问题!使用编辑文本上的文本视图(例如,您可以使用RelativeLayout在编辑文本上设置文本视图),并将编辑文本字体颜色设置为白色(或编辑文本的背景色),以便用户看不到它是真实文本。你观察到的文字变化

<?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>  

活动:我希望这个片段可以帮助您:
main.xml中(在res /布局)

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


}