Android 预定义输入

Android predefined input

如何在 xml 中为特定值的预定义输入创建输入?例如,我希望用户输入可以是 120/80 的血压,因此在我的输入中我希望用户输入一个已经放置了正斜杠的输入字段。请指教。 这就是我的

我最接近的是这个

<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>

如果你想做到简单,

您应该使用文本观察器,它还可以帮助您在运行时验证值。

您可以在 textwatcher 中添加验证,例如:

将字符串分成两部分并检查它们是否属于您各自的值的 int 值。

当字符串的长度超过 3 时也添加一个分隔符 (-)

选项很少。 首先,您可以尝试组合 2 个编辑文本,一个用于斜线之前,一个用于斜线之后(并确保它们看起来像一个)

第二种选择是使用 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("/", ""));
    } 
} 
});

最后,创建一个像这样的自定义编辑文本: https://github.com/woxingxiao/XEditText

使用 TextWatcher 会非常困难。您可能会遇到一些边缘情况,低血压的人应该能够输入 80/50,在这种情况下,您不能使用 3 位拆分来自动添加值之间的 /

我建议您像以前那样使用 2 个单独的字段,使用 android:hint 来提供更好的描述。喜欢

Blood Pressure (TextView Label)
_____MAX_____ / ______MIN______