【问题标题】:Making EditText input only accept numbers not working使 EditText 输入只接受无效的数字
【发布时间】:2016-02-08 03:49:59
【问题描述】:

我的问题是我无法阻止我的EditText 输入接受字母数字。这是在 Android 上,它适用于我正在开发的应用程序。我只希望它接受数字。

我即时创建EditText。下面是创建它们的代码。这是整个 Java 文件的代码。

public class PartDetail extends Activity implements View.OnClickListener{

    private final int NUM_COL = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.part_detail);
        Bundle bundle = getIntent().getExtras();
        String btnName = bundle.getString("btnNameStored");
        String btnOrig = bundle.getString("btnOrig");

        TextView textView = (TextView) findViewById(R.id.txtBtnPushed);
        textView.setText(btnOrig);
        BufferedReader reader;
        InputStream is = null;

        // Get the name of the part picked and then grab the dimensions that are needed for that
        // part.

        try {

            is = getAssets().open(btnName);

            reader = new BufferedReader(new InputStreamReader(is));

            String line = reader.readLine();
            int lineLength = (line.length());

            //Used for debugging ---------------------------
            //System.out.println(" -- Text on Button --  " + btnName + " -- Line Length -- " +
            //        lineLength);
            // ------------------------------------

            TableLayout table = (TableLayout) findViewById(R.id.tblLayoutContent);

            while (line != null){

                TableRow tblRow = new TableRow(this);
                tblRow.setPadding(5, 30, 5, 5);
                table.addView(tblRow);
                line = line.toUpperCase();

                // sets the max number of col to 2 and iterates through the number of lines.
                // filling each cell with a Text Box with the name of each dimension of the part
                // that was picked.

                for (int col = 0; col < NUM_COL; col++) {
                    //This is the label of what measurement needs to be enter.
                    TextView lblName = new TextView(this);
                    // This is the number you enter for the measurement.
                    EditText txtPartMeasurement = new EditText(this);

                    // Set all the input attributes for the text boxes.
                    if (line == "QTY") {
                        txtPartMeasurement.setInputType(InputType.TYPE_CLASS_NUMBER);
                    }
                        else {
                        txtPartMeasurement.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
                    }

                    txtPartMeasurement.setTextSize(14);

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                        txtPartMeasurement.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
                    }
                    txtPartMeasurement.setEnabled(true);

                    // Set all the input attributes for the labels of what needs to be entered.
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                        lblName.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                    }
                    lblName.setBackgroundColor(getResources().getColor(R.color.colorPartMeasurements));
                    lblName.setFocusable(true);
                    lblName.setText(line);
                    lblName.setTextSize(14);

                    // Add the labels and text boxes to the grid.
                    tblRow.addView(lblName);
                    tblRow.addView(txtPartMeasurement);

                    // Get the next line in the file if there one.
                    line = reader.readLine();
                }
            };

        }
        catch (IOException e) {
            //Used for debugging --------------------------------------------
            //System.out.println("In the catch of the On Catch in PartDetail  --  " + btnName);
            // ----------------------------------------------------------
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {

    }
}

请注意,我将 lbl 的 TYPE_CLASS_NUMBER 设置为 EditText 是 QTY,否则我希望它为 boe TYPE_NUMBER_FLAG_DECIMAL

但这似乎不起作用。我已经在我的模拟器和我的真手机上试过了。不去。请让我知道我做错了什么。 谢谢。

这是 xml。请记住,我是动态创建大部分对象的。

  <?xml version="1.0" encoding="utf-8"?>
     <!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ~ Copyright (c) 2016. Unless otherwise noted, all my code is open source
-->

<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
          android:layout_width = "match_parent"
          android:layout_height = "match_parent" >

<TextView
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:text = "New Text"
    android:id = "@+id/txtBtnPushed"
    android:textSize = "42sp"
    android:layout_alignParentTop = "true"
    android:layout_centerHorizontal = "true" />

<ImageView
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:id = "@+id/imgPartPicked"
    android:layout_below = "@+id/txtBtnPushed"
    android:layout_centerHorizontal = "true"
    android:src = "@mipmap/straight_tap"
    android:minWidth = "150dp"
    android:minHeight = "150dp" />
<ScrollView
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    android:layout_below = "@+id/imgPartPicked"
    android:layout_alignParentLeft = "true"
    android:layout_alignParentStart = "true">

    <TableLayout
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:stretchColumns="*"
        android:id = "@+id/tblLayoutContent">
    </TableLayout >

</ScrollView >
</RelativeLayout >

【问题讨论】:

  • 不得不更改标题。拼写错误。对不起。
  • android:inputType="number" in xml 文件 editText
  • 我可以看看你的 xml 吗?
  • 或者在代码中:my_edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
  • 我知道我对一个人感到困惑(不难做到),但我想我可能也难倒了其他人。

标签: java android android-edittext


【解决方案1】:

在您的 xml 属性中写入 android:inputType="number"android:inputType="numberDecimal"

android:inputType="number" 只接受整数,android:inputType="numberDecimal" 接受十进制数。

您可以参考this

【讨论】:

  • 查看 OP 代码,他创建的是 EditText from java code 而不是 xml!!
  • @Rami 好吧,他也可以参考我的链接。
  • @Tony - 如果我在我的 xml 文件中创建这些框,它就可以正常工作。但就像 Rami 所说,我需要在 java 代码中创建它们。但我已经看到了该链接并在我的应用程序的另一部分中使用了它。感谢您的尝试。
【解决方案2】:

好的,我想出了一个解决方案。这是我改变的部分。 我决定不将 set 输入包装在 if 语句中。现在它可以工作了,但所有编辑文本都接受十进制。我必须弄清楚如何将数量框限制为仅数字,但现在我必须解决这个问题。

                txtPartMeasurement.setInputType(InputType.TYPE_CLASS_NUMBER |
                        InputType.TYPE_NUMBER_FLAG_DECIMAL);


                // Set all the input attributes for the text boxes.
               /** if (line == "QTY") {
                    txtPartMeasurement.setRawInputType(InputType.TYPE_CLASS_NUMBER |
                            InputType.TYPE_NUMBER_FLAG_DECIMAL);
                }
                    else {
                    txtPartMeasurement.setRawInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
                } */
                txtPartMeasurement.setTextSize(14);

【讨论】:

    猜你喜欢
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 2016-07-01
    • 1970-01-01
    相关资源
    最近更新 更多