2014-05-09 4 views
1

사용자 입력을 포함하는 문자열을 가져 와서 단추를 클릭 할 때 텍스트 뷰에 표시하는 데 어려움이 있습니다. 예를 들어, 사용자가 을 입력하고 단추를 클릭하면 텍스트 뷰에 이 표시됩니다. 대답 인덱스이 사용되지 않는다는 경고 메시지가 나타납니다.구문 분석 된 정수 문자열을 텍스트 뷰에 표시하는 방법

   //Fetches user answer and converts it into an integer 
       EditText answer = (EditText)findViewById(R.id.editanswer); { 
       if (answer.getText().toString().length() > 0){ 
        int answerInt = Integer.parseInt(answer.getText().toString()); 

        //Display answer in textview on click 
        TextView displayanswer = (TextView) findViewById(R.id.tvdisplayanswer); 
        displayanswer.setText(answer); 

       } 

와 나는 어려움을 겪고있어 코드는 그것의 coode 문맥

  findViewById(R.id.btnok).setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       Log.d("onClick", "OK button was clicked"); 

       //Fetches user answer and converts it into an integer 
       EditText answer = (EditText)findViewById(R.id.editanswer); { 
       if (answer.getText().toString().length() > 0){ 
        int answerInt = Integer.parseInt(answer.getText().toString()); 

        //Display answer in textview on click 
        TextView displayanswer = (TextView) findViewById(R.id.tvdisplayanswer); 
        displayanswer.setText(answer); 

       } 
      //Displaying a toast if no answer is provided 
       else 
       { 
        Toast.makeText(getApplicationContext(), "No answer provided!", 
          Toast.LENGTH_SHORT).show(); 
       } 


        } 

답변

2

시도에게있어이

displayanswer.setText(" "+answerInt); 

또는

,
displayanswer.setText(String.valueOf(answerInt)); 
+1

와우, 즉 빨리! 나는 타이머가 그렇게하도록 허락하는대로 당신의 답을 받아 들일 것입니다! 감사! –

0

이 시도 :

displayanswer.setText(""+answerInt); 
+0

해설을 추가하십시오. 코드 만 응답하면 안됩니다. – aliteralmind

0

당신은이를 사용할 수 있습니다

String.valueOf(yourint); 
0

당신이이 방법을 간단하게 할 수 있습니다

if (answer.getText().toString().length() > 0){ 
       //Display answer in textview on click 
       TextView displayanswer = (TextView) findViewById(R.id.tvdisplayanswer); 
       displayanswer.setText(answer.getText().toString()); 

      }