2016-07-27 30 views
-1

我有,我有這個片段的活動....如何在java和android中使用trim?

et = (EditText) findViewById(R.id.et); 
tv = (TextView) findViewById(R.id.textView8); 
String name =et.getText().toString(); 
String a ="a"; 
String b ="b"; 
String c ="c"; 
String d ="d"; 
String e ="e"; 
String f ="f"; 
String g ="g"; 
String h ="h"; 
String a1 ="\u24B6"; 
String b1 ="\u24B7"; 
String c1 ="\u24B7"; 
String d1 ="\u24B9"; 
String e1 ="\u24BB"; 
String f1 ="\u24BB"; 
String g1 ="\u24BD"; 
String h1 ="\u24BD"; 
name =name.replaceAll(a,a1); 
name =name.replaceAll(b,b1); 
name =name.replaceAll(c,c1); 
name =name.replaceAll(d,d1); 
name =name.replaceAll(e,e1); 
name =name.replaceAll(f,f1); 
name =name.replaceAll(g,g1); 
name =name.replaceAll(h,h1); 
tv.setText(name.trim()); 

當寫一個b或編輯文本顯示空間Ç在文本視圖

是什麼解?

+0

可能是用過的字體不支持使用的unicode字符?爲了測試,嘗試使用不同的Unicode字符。此外,我會使用replace(char,char)而不是'replaceAll',它使用正則表達式(可能會使unicode出現問題?) – Robert

回答

0

您只從EditText開始閱讀價值 - 當您開始活動時。有一個完整的工作示例:輸入EditText時文本被替換。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final EditText et = (EditText) findViewById(R.id.et); 
    final TextView tv = (TextView) findViewById(R.id.textView8); 
    final String a = "a"; 
    final String b = "b"; 
    final String c = "c"; 
    final String d = "d"; 
    final String e = "e"; 
    final String f = "f"; 
    final String g = "g"; 
    final String h = "h"; 
    final String a1 = "\u24B6"; 
    final String b1 = "\u24B7"; 
    final String c1 = "\u24B7"; 
    final String d1 = "\u24B9"; 
    final String e1 = "\u24BB"; 
    final String f1 = "\u24BB"; 
    final String g1 = "\u24BD"; 
    final String h1 = "\u24BD"; 

    et.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) { 

     } 

     @Override 
     public void onTextChanged(final CharSequence s, final int start, final int before, final int count) { 
      String name = et.getText().toString(); 
      name = name.replaceAll(a, a1); 
      name = name.replaceAll(b, b1); 
      name = name.replaceAll(c, c1); 
      name = name.replaceAll(d, d1); 
      name = name.replaceAll(e, e1); 
      name = name.replaceAll(f, f1); 
      name = name.replaceAll(g, g1); 
      name = name.replaceAll(h, h1); 
      tv.setText(name.trim()); 
     } 

     @Override 
     public void afterTextChanged(final Editable s) { 

     } 
    }); 
} 
+0

此活動在運行應用程序時停止 – pouyakarimdost

+0

您會得到什麼錯誤? – pxsx

+0

對不起應用程序停止了 – pouyakarimdost

相關問題