号码范围上一个EditText - 只需要1到10之间的数字 [英] Number range on an edittext - only want numbers between 1 and 10

查看:553
本文介绍了号码范围上一个EditText - 只需要1到10之间的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个应用程序,只允许用户在1到10之间选择,我试着写我自己的方法,其中,如果数字是出的范围,它改变了文本的意见,以错误并清零什么是在编辑文本,但它崩溃的应用程序。有谁知道有什么错呢?新到Android。

code:

 公共无效buttonClick(视图V)
{    TextView的tvNum =(的TextView)findViewById(R.id.tvNumEnt);
    TextView的tvName =(的TextView)findViewById(R.id.tvNumEnt);
    TextView的tvNameEnt =(的TextView)findViewById(R.id.NameEnt);
    TextView的tvNumEnt =(的TextView)findViewById(R.id.NumEnt);    NUM的EditText =(EditText上)findViewById(R.id.ETnumber);
    的EditText名=(的EditText)findViewById(R.id.ETname);    。字符串nameContent = name.getText()的toString();
    。字符串numContent = num.getText()的toString();    tvName.setText(nameContent);        int值=的Integer.parseInt(num.getText()的toString());
        如果(值大于10)
        {
            tvNum.setText(错误);
            num.getText()清()。
            name.getText()清()。        }
        否则,如果(值。1)
        {
            tvNum.setText(错误);
            num.getText()清()。
            name.getText()清()。
        }
        其他
        {            tvNum.setText(numContent);            tvNameEnt.setVisibility(View.VISIBLE);
            tvNumEnt.setVisibility(View.VISIBLE);
            tvName.setVisibility(View.VISIBLE);
            tvNum.setVisibility(View.VISIBLE);
        }}


解决方案

您在这条线有问题。

  int值=的Integer.parseInt(num.toString());

更改为:

  int值=的Integer.parseInt(num.getText()的toString());

现在您从对象调用的toString()的EditText 对象。你必须调用它的getText()方法,然后再调用后的toString()方法的CharSequence

更新:
你会发现两次用相同的ID同样的观点。看看下面的code:

  TextView的tvNum =(的TextView)findViewById(R.id.tvNumEnt);
TextView的tvName =(的TextView)findViewById(R.id.tvNumEnt);

应该有 R.id.tvNumEnt 中的第二次,我想是这样...

  TextView的tvNum =(的TextView)findViewById(R.id.tvNumEnt);
TextView的tvName =(的TextView)findViewById(R.id.tvNumEnt);

I'm trying to make an app that only allows a user to enter between 1 and 10, I've tried writing my own method where if the number is out of that range, it changes the text views to ERROR and clears what's in the edit text, however it crashes the app. Does anyone know what's wrong with it? New to android.

Code:

    public void buttonClick (View v)
{



    TextView tvNum = (TextView) findViewById(R.id.tvNumEnt);
    TextView tvName = (TextView) findViewById(R.id.tvNumEnt);
    TextView tvNameEnt = (TextView) findViewById(R.id.NameEnt);
    TextView tvNumEnt = (TextView) findViewById(R.id.NumEnt);

    EditText num = (EditText) findViewById(R.id.ETnumber);
    EditText name = (EditText) findViewById(R.id.ETname);

    String nameContent = name.getText().toString();
    String numContent = num.getText().toString();

    tvName.setText(nameContent);

        int value = Integer.parseInt(num.getText().toString());
        if (value > 10)
        {
            tvNum.setText("ERROR");
            num.getText().clear();
            name.getText().clear();

        }
        else if (value < 1)
        {
            tvNum.setText("ERROR");
            num.getText().clear();
            name.getText().clear();
        }
        else
        {



            tvNum.setText(numContent);

            tvNameEnt.setVisibility(View.VISIBLE);
            tvNumEnt.setVisibility(View.VISIBLE);
            tvName.setVisibility(View.VISIBLE);
            tvNum.setVisibility(View.VISIBLE);
        }

}

解决方案

You have issue on this line

int value = Integer.parseInt(num.toString());

change to:

int value = Integer.parseInt(num.getText().toString());

Now you call toString() method from Object for EditText object. You have to call getText() method for it as first and after then call toString() method for CharSequence

UPDATE: You find two times the same view with the same ids. Look at the code below:

TextView tvNum = (TextView) findViewById(R.id.tvNumEnt);
TextView tvName = (TextView) findViewById(R.id.tvNumEnt);

there should be R.id.tvNumEnt in the second time, I think so...

TextView tvNum = (TextView) findViewById(R.id.tvNumEnt);
TextView tvName = (TextView) findViewById(R.id.tvNumEnt);

这篇关于号码范围上一个EditText - 只需要1到10之间的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆