2013-04-06 1 views
0

제 신청서에는 두 개의 텍스트 입력란과 하나의 입력란이 있습니다. 이 응용 프로그램은이 텍스트의 정수 값을 계산합니다. 하지만 텍스트를 넣으면 오류가 있습니다. 이 텍스트 필드에서 int를 확인할 수있는 방법과 정수가없는 경우 메시지를 표시하는 방법은 무엇입니까? 내 시도 :변수 유형은 어떻게됩니까?

EditText num1text=(EditText)findViewById(R.id.num1text); 
EditText num2text=(EditText)findViewById(R.id.num2text);   
Float num1=Float.parseFloat(num1text.getText().toString()); 
Float num2=Float.parseFloat(num2text.getText().toString()); 

if (num1 instanceof Float && num2 instanceof Float) 
{ 
Float answ = (num1 * num2)/100; 
TextView answer=(TextView)findViewById(R.id.ans); 
answer.setText(answ); 
}else 
answer.setText("Message"); 

그러나 내 응용 프로그램은 다른 경우에는 내 메시지를 표시하지 않습니다.

답변

4

일부 다른 주석으로는 정규식을 사용하여 입력 필드의 내용을 확인한 후 변환을 시작할 수 있습니다. 이것은 좋은 권장 사항으로, 사용하기 전에 사용자 입력을 제거해야합니다.

입력을 변환 할 수없는 경우 Float.parseFloat() 조작을 실행하면 NumberFormatException이 표시됩니다. 이를 피하려면 try...catch 구조를 사용해야합니다.

try { 
    Float num1=Float.parseFloat(num1text.getText().toString()); 
    Float num2=Float.parseFloat(num2text.getText().toString()); 
    Float answ = (num1 * num2)/100; 
    TextView answer=(TextView)findViewById(R.id.ans); 
    answer.setText(answ); 
} catch (NumberFormatException e) { 
    TextView answer=(TextView)findViewById(R.id.ans); 
    answer.setText("Message"); 
} 

또한, 당신은 단지 숫자에 대한 입력을 제한하는 레이아웃 XML에 android:inputType 속성을 사용 (그리고 숫자 키보드를 얻을) 할 수 있습니다.

+0

감사합니다. Barend –

0

정수/실수 (숫자) 값이 항상 필요하므로 입력란에 숫자로 속성으로 EditTextinputtype을 xml 파일에 설정할 수 있습니다. 당신은 항상 수와 사용자가 예를 들어 문자 또는 숫자 값

를 입력 여부 유형을 검사 할 필요를 얻기 수

이 화면에서만 숫자 키 쇼를 softkeyboard합니다 like

<EditText android:inputType="number" 
.../> 
1

당신은 정규 표현식을 사용할 수 있습니다

if (YourVariable.matches("^[0-9,;]+$")) { 
    // Contains only numbers 
} else { 
    // Contains NOT only numbers 
} 
+0

플로트에 사용할 수 없습니다. –

0

Float.parseFloat (. num2text.getText의 toString()()); float가 아닌 경우 숫자 형식 예외가 발생합니다. 예외를 잡아라. 필드를 지우십시오. 입력이 유효하지 않음을 나타내는 축배 메시지를 표시합니다.

public class MainActivity extends Activity { 
TextView answer; 
EditText num1text,num2text; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    num1text=(EditText)findViewById(R.id.editText1); 
    num2text=(EditText)findViewById(R.id.editText2); 
    Button b= (Button) findViewById(R.id.button1); 
    answer = (TextView) findViewById(R.id.textView2); 
    b.setOnClickListener(new OnClickListener() 
    { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      try 
      { 
      Float num2=Float.parseFloat(num2text.getText().toString()); 
      Float num1=Float.parseFloat(num1text.getText().toString()); 
          //if the numbers are not float throws a numberformat exception 
      if (num1 instanceof Float && num2 instanceof Float) 
      { 
      Float answ = (num1 * num2)/100; 
      answer.setText(Float.toString(answ)); 
      }else 
      answer.setText("Message"); 
      } 
      catch(NumberFormatException e) 
      { 
           //catch the exception clear the fields and display toast 
       num1text.setText(""); 
       num2text.setText(""); 
       Toast.makeText(MainActivity.this, "Invalid Input Type!Check the input", 1000).show(); 
       e.printStackTrace(); 
      } 

     } 

    }); 

} 
}