Java 使EditText输入仅接受不起作用的数字

Java 使EditText输入仅接受不起作用的数字,java,android,android-edittext,Java,Android,Android Edittext,我的问题是我无法阻止我的EditText输入接受字母数字。这是安卓系统上的,我正在开发一个应用程序。我只想让它接受数字 我动态创建EditText。下面是创建它们的代码。这是整个Java文件的代码 public class PartDetail extends Activity implements View.OnClickListener{ private final int NUM_COL = 2; @Override protected void onCreat

我的问题是我无法阻止我的
EditText
输入接受字母数字。这是安卓系统上的,我正在开发一个应用程序。我只想让它接受数字

我动态创建
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) {

    }
}

在xml属性中写入
android:inputType=“number”
,或
android:inputType=“numberDecimal”

android:inputType=“number”
仅接受整数,
android:inputType=“numberDecimal”
接受十进制数


你可以参考。

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

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

我不得不改变标题。拼写错误。抱歉。android:inputType=“number”在xml文件edittext中我可以看到您的xml吗?或者在代码中:
my\u edittext.setInputType(inputType.TYPE\u CLASS\u number)我知道我很困惑(不难做到),但我想我可能也难倒了其他人。看看操作代码,他用java代码而不是xml创建EditText@Rami好吧,他可能也会引用我的链接。@Tony-如果我在xml文件中创建框,它可以像这样工作。但正如Rami所说,我需要在java代码中创建它们。但我已经看到了这个链接,并在我的应用程序的另一部分使用了它。谢谢你的努力。
                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);