2017-05-25 28 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______