【问题标题】:keyboard with numeric and basic math operations for an EditText用于 EditText 的具有数字和基本数学运算的键盘
【发布时间】:2020-02-29 22:26:21
【问题描述】:

我怎样才能拥有这个键盘

对于 EditText?

我尝试使用 InputType,但无法使用此键盘。

我创建了这个例子,显示答案并没有解决问题:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent">

        <!--number-->
        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="hint"
            android:inputType="number" />
    </com.google.android.material.textfield.TextInputLayout>

    <!--numberDecimal|numberSigned-->
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="numberDecimal|numberSigned"
        android:digits="0123456789.+-*/%()"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout" />

    <!--numberPassword-->
    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="numberPassword"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>

前两种输入类型的结果键盘是相同的,它们就像:

最后一个是这样的:

【问题讨论】:

  • 请附上此edittext的代码,如果存在此类问题,请尝试在另一台设备上进行测试
  • 我改进了这个问题。
  • 如果你想做一个计算器,那么你应该实现你自己的键盘,AFAIK。这并不难,只是一个带有一些TextViews的GridView。而且,是的,TextViews 确实响应点击。

标签: android android-edittext keyboard android-inputtype


【解决方案1】:

使用这个

<EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number" />

【讨论】:

    【解决方案2】:

    你可以用它来只显示数字:

    <EditText
            android:id="@+id/editTextNumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number" />
    

    或者当您使用 EditText 作为密码时使用它来显示数字:

    <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberPassword" />
    

    还请理解,大多数手机都有不同的键盘设计,因此您可能无法获得您在图片中提到的确切键盘

    【讨论】:

    • 我已经尝试过这些,但它们与我的屏幕截图不同:
    • 我明白,但这取决于您运行代码的设备。您使用的是什么模拟器或设备?
    • 我使用 Pixel 和 API 27
    • 你的第一张照片来自什么设备?
    • 正如我提到的,它来自Pixel API 27 的android 模拟器。如果 inputType 是文本,您可以使用 123? 更改键盘
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 2018-01-24
    相关资源
    最近更新 更多