【问题标题】:How to set only integer numbers for EditText android (without comma)如何仅为 EditText android 设置整数(不带逗号)
【发布时间】:2014-06-11 10:15:17
【问题描述】:

如何只为 EditText 设置整数? (我想避免用户输入逗号)

【问题讨论】:

标签: android android-edittext


【解决方案1】:

在您的 xml 中尝试以下属性:

android:inputType="numberSigned"

【讨论】:

  • 谢谢,编程方式?
  • customEditText.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);试试这个
  • 不要设置InputType.TYPE_NUMBER_FLAG_DECIMAL,因为这将允许小数。你可能想使用 setInputType() 而不是 setRawInputType() see here
【解决方案2】:

您可以在xml中设置以下内容:

android:inputType="numberSigned"

以编程方式:

editText.setInputType(InputType.TYPE_CLASS_NUMBER);

【讨论】:

  • 谢谢,编程方式?
【解决方案3】:

以编程方式您可以这样实现:

edittext.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_CLASS_NUMBER);

【讨论】:

    【解决方案4】:

    xml 中使用android:inputType="numberSigned":“EditText” 或者

    Activity

                       EditText et=(EditText)findViewById(R.id.et_edit);
                       et.setInputType(InputType.TYPE_CLASS_NUMBER);
    

    【讨论】:

      【解决方案5】:
         <EditText
              android:id="@+id/editText1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true"
              android:layout_marginTop="52dp"
              android:ems="10"
              android:inputType="numberSigned" >>
          </EditText>
      

      【讨论】:

        【解决方案6】:

        以上答案都是正确的。你也可以从 java 动态地做到这一点 -

        EditText ed=(EditText)findViewById(R.id.edit);
        ed.setInputType(InputType.TYPE_CLASS_NUMBER);
        

        【讨论】:

          【解决方案7】:

          在 XML 中使用这段代码

          android:inputType="number"

          【讨论】:

            猜你喜欢
            • 2011-06-06
            • 2016-07-09
            • 1970-01-01
            • 2012-11-28
            • 2014-12-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多