2012-07-28 22 views
0

如何限制點後的字符數(顯示在我的TextView中) 看到2種方法將非常重要:在java中和在TextView XML中實際上(如果可能的話) 謝謝!限制在Java中的點後的字符

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    inivVars();  
} 
private void inivVars(){...} 

public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch(v.getId()){ 
    case R.id.bCount: 

     try{ 
     checkT1 = et1.getText().toString(); 
     checkT2 = et2.getText().toString(); 
     checkK1 = ek1.getText().toString(); 

     det1 = Float.parseFloat(checkT1); 
     det2 = Float.parseFloat(checkT2); 
     dek1 = Float.parseFloat(checkK1); 
     float x = det2/(det1/100)/100*dek1; 
     String sx = String.valueOf(x); 
     text.setText(sx); 
     text.setTextColor(Color.BLUE); 

     }catch(Exception e){ 
      Toast t = Toast.makeText(CivilProtectActivity.this, "enter all values or use suitable '.'", Toast.LENGTH_LONG); 
      t.show(); 

     } 

     break; 
    case R.id.bHide: 
     InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(et1.getWindowToken(), 0); 
     et1.setText(""); 
     et2.setText(""); 
     ek1.setText(""); 
     text.setText(""); 

     break; 
    }}} 

和TextView的XML,其顯示數據

<TextView 
    android:id="@+id/tvShowA" 
    android:layout_width="95dp" 
    android:layout_height="95dp" 
    android:textSize="22dp" 
    android:hint="X" 
    android:background="@drawable/sun" 
    android:gravity="center" 
    /> 

回答

0

使用Android:的minLength = 「30」 到最大字符長度設置爲30 (把要設置數)

示例

<TextView 
    android:id="@+id/tvShowA" 
    android:minLength="30" // See this 
    android:layout_width="95dp" 
    android:layout_height="95dp" 
    android:textSize="22dp" 
    android:hint="X" 
    android:background="@drawable/sun" 
    android:gravity="center" 
    /> 
+0

謝謝!你的建議使我得到了答案。其實我需要'android:maxLength =「4」'在我的textView中獲得_2,03_而不是_2,0340923409_ – Andrew 2012-07-28 10:59:09

+0

是@安德魯,你也告訴了我不動產。謝謝.. – 2012-07-28 11:03:12