无法从EditText获取整数值

时间:2014-02-21 10:34:45

标签: java android

我无法从EdiText(edText)获取整数值。我不知道为什么会出现这个问题。执行Catch下的部分而不是Try

   public class Ybus_Activity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ybus);
        //Creating Linear Layout
        final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        final LinearLayout main = (LinearLayout)findViewById(R.id.android_main_layout);
        //Creating TextView
        TextView getData=new TextView(this);
        getData.setText("Enter the number of LineData : ");
        getData.setId(5);
        getData.setLayoutParams(params);
        main.addView(getData);
        //Creating EdiText
        final EditText edText = new EditText(this);
        edText.setId(3);
        edText.setLayoutParams(params);
        edText .setWidth(100);
        edText .setImeOptions(EditorInfo.IME_ACTION_NEXT);
        edText .setInputType(InputType.TYPE_CLASS_NUMBER);
        edText .setKeyListener(DigitsKeyListener.getInstance());
        edText .setMaxLines(1);
        main.addView(edText );
        final String ed=edText.getText().toString();
        //Creating Button for displaying integer from edText
        Button bt = new Button(this);
        bt.setText("Click to enter Linedata");
        bt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        main.addView(bt);
        final TextView text = new TextView(this);
        bt.setOnClickListener(new View.OnClickListener() 
        { 
            public void onClick(View v) 
            {
                int i = 0;
                try{
                    i =Integer.parseInt(ed);
                    text.setText(i);
                }catch(NumberFormatException ex){
                    text.setText("Value at TextView is not a valid integer");
                }
            }
        });
        main.addView(text);
    }
}

5 个答案:

答案 0 :(得分:1)

int转换为String,以便将您的内容显示为TextView。这是因为setText(CharacterSequence)设置了CharacterSequence

 text.setText(String.valueof(i));

以这种方式实现Button Click侦听器:

bt.setOnClickListener(new View.OnClickListener() 
{  
public void onClick(View v) 
{
int i = 0;
try{
  String ed=edText.getText().toString(); 
  i =Integer.parseInt(ed);
  text.setText(String.valueOf(i);

}catch(NumberFormatException ex){
  text.setText("Value at TextView is not a valid integer");
  }
 }
});

答案 1 :(得分:1)

移动此String ed=edText.getText().toString();

bt.setOnClickListener(new View.OnClickListener() 
{  
public void onClick(View v) 
{
    int i = 0;
    try{
      String ed=edText.getText().toString(); 
      i =Integer.parseInt(ed);
      text.setText(String.valueOf(i));
    }catch(NumberFormatException ex){
       e.printStacktrace();
    }
}
});

内部按钮单击侦听器

同样setText(int)(i是一个int值i)查找具有提到的id的资源。如果找不到,则会获得ResourceNotFOundException。而是使用您需要setText(CharacterSequence)

text.setText(String.valueOf(i));

答案 2 :(得分:1)

您好,您可以使用字符串在textview上显示,以便您可以执行以下操作

     bt.setOnClickListener(new View.OnClickListener() 
        { 
            public void onClick(View v) 
            {
                String ed=edText.getText().toString(); 
                int i = 0;
                try{
                  i =Integer.parseInt(ed);
                 //setting value here
                 text.setText(String.valueof(i));
                 //or you can do like this
                 // text.setText(i+"");
                }catch(NumberFormatException ex){
                  text.setText("Value at TextView is not a valid integer");
                }
            }
        });

答案 3 :(得分:0)

这是字符串:

text.setText(i+""); 

获取int值

int a = Integer.parseInt(text.getText().toString());

答案 4 :(得分:0)

您更改了一行:

i =Integer.parseInt(ed);
text.setText(i);

到:

i = Integer.parseInt(edText.getText().toString());
text.setText(i+"");