2012-07-11 91 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"); 
      } 
     }); 
    } 


}