2017-05-25 62 views
1

如何在我的xml中爲特定值的預定義輸入創建輸入?例如,我想要一個用戶輸入可以是120/80的血壓,所以在我的輸入中,我希望用戶輸入已經放置正斜槓的輸入字段。請指教。 這是我Android預定義輸入

enter image description here

我到最接近的是這

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:weightSum="3" 
      android:gravity="start" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="20dp" 
      android:layout_marginTop="10dp" 
      android:orientation="horizontal"> 

     <android.support.design.widget.TextInputLayout 
       android:layout_weight="0.8" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

      <android.support.design.widget.TextInputEditText 
        android:id="@+id/patientBloodPressure1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:focusable="true" 
        android:imeOptions="actionNext" 
        android:maxLines="1" 
        android:textSize="14sp" /> 

     </android.support.design.widget.TextInputLayout> 

     <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1.45" 
       android:layout_gravity="center_horizontal|center_vertical" 
       android:gravity="center" 
       android:padding="2dp" 
       android:text="@string/slash" 
       android:textSize="14sp" 
       android:textStyle="bold" /> 


     <android.support.design.widget.TextInputLayout 
       android:layout_weight="0.75" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

      <android.support.design.widget.TextInputEditText 
        android:id="@+id/patientBloodPressure2" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:focusable="true" 
        android:imeOptions="actionNext" 
        android:maxLines="1" 
        android:textSize="14sp" /> 

     </android.support.design.widget.TextInputLayout> 


    </LinearLayout> 
+0

讓我把它做好:你想要兩個單獨的字段除以斜槓填充默認值,而不是提示。對? – gonczor

+0

不完全,暗示也可以工作。我只想要以上,想知道是否有更簡單的方法。 – sammyukavi

回答

0

很少有選項字符串將穿越其長度。 首先,你可以嘗試結合2 EditText上,一個斜線前和一個後(並確保他們看起來像一個單一的一個)

第二個選擇是使用TextWatcher,幾乎就像他們在信用卡到期辦日期

editText.addTextChangedListener(new TextWatcher() { 
@Override 
public void afterTextChanged(Editable s) { 
    // TODO Auto-generated method stub 
} 

@Override 
public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
    // TODO Auto-generated method stub 
} 

@Override 
public void onTextChanged(CharSequence s, int start, int before, int count) { 
    if (before == 1 && count == 2 && s.charAt(s.length()-1) != '/') { 
     editText.setText(editText.getText() += "/"; 
    } 
    if (editText.getText().toString().toCharArray().length < 3) { 
     editText.setText(editText.getText().toString().replace("/", "")); 
    } 
} 
}); 

而在去年,創建自定義的EditText像這樣的: https://github.com/woxingxiao/XEditText

0

如果你想這樣做簡單,

你應該使用文本觀察家也幫助您在運行時驗證值。

您可以在textwatcher如增加您的驗證:

分割字符串成兩個部分並檢查int類型,這些屬於您的相應值與否。

還添加了一個分離器( - )時上述3

0

使用TextWatcher將是非常困難。您可能會遇到一些邊緣病例,血壓低的人應該能夠輸入80/50,在這種情況下,您不能使用3位數分割來自動添加/

我建議你使用2個獨立的字段,使用android:hint來提供更好的描述。像

Blood Pressure (TextView Label) 
_____MAX_____/______MIN______