【问题标题】:Android predefined inputAndroid 预定义输入
【发布时间】:2017-10-25 17:52:20
【问题描述】:

如何在我的 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>

【问题讨论】:

  • 让我做对了:您希望两个单独的字段除以斜线,填充默认值而不是提示。对吗?
  • 不完全是,提示也可以。我只想要上面的,想知道有没有更简单的方法。

标签: android android-layout android-xml android-input-method


【解决方案1】:

如果你想做的很简单,

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

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

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

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

【讨论】:

    【解决方案2】:

    选项很少。 首先,您可以尝试组合 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

    【讨论】:

      【解决方案3】:

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

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

      Blood Pressure (TextView Label)
      _____MAX_____ / ______MIN______
      

      【讨论】:

        猜你喜欢
        • 2016-01-09
        • 1970-01-01
        • 1970-01-01
        • 2017-03-19
        • 2020-10-01
        • 2013-08-31
        • 2016-12-10
        • 2018-11-28
        • 1970-01-01
        相关资源
        最近更新 更多