2016-06-14 3 views
3

나는 최근에 안드로이드 스튜디오를 사용하기 시작했으며 현재 로마 숫자 변환기 (Roman Numeral Translator) 앱을 만들고 있습니다. 앱의 인터페이스는 다음과 같습니다. Application interfaceandrioid에서 메소드 출력으로 textview를 설정하는 방법은 무엇입니까?

사용자는 키패드를 사용하여 위에 표시된 TextView에 표시 될 정수를 입력합니다. 변환 버튼을 누르면 입력 한 정수를 가져와 변환합니다 (문자열이나 문자가 포함 된 경우 프로그램에서 입력을 잡을 수 있습니다). 그런 다음 사용자가 "변환"버튼을 클릭하면 응용 프로그램은 TextView를 결과로 재설정합니다.

현재 내 주요 활동에는 버튼에 대한 onClickListeners와 번역을위한 별도의 번역기 메서드가 포함되어 있습니다. 내 문제는 "변환"버튼을 나는 번역기 메서드에서 입력을 얻는 방법 및 변환이 완료되면 TextView로 설정할지 모르겠습니다.

convert.setOnClickListener(
       new View.OnClickListener() { 
        public void onClick(View v) { 
         TextView numeralInput = (TextView) findViewById(R.id.textView); 
         String intValue = numeralInput.getText().toString(); 
         try{ 
          int integer = Integer.parseInt(intValue); 
          if (integer > 0 && integer <= 4999){ 
           translator(integer); 

          }else{ 
           numeralInput.setText("Please enter an integer between 0 and 4,999."); 
          } 

         }catch(NumberFormatException e){ 
          numeralInput.setText("Invalid input try again."); 
         } 
        } 
       } 
     ); 

`

번역기 방법 -`

public static void translator(int integer) { 
     LinkedList<String> stack = new LinkedList<String>(); 
     // if (integer > 0 && integer <= 4999) { 
     //ArrayList<Integer> placement = new ArrayList<Integer>(); 
     int place = (int) Math.log10(integer); 
     for (int i = 0; i <= place; i++) { 
      //while(integer > 0){ 
      //System.out.println(integer); 
      int placeOfValue = integer % 10; 
      //stack.push(placeOfValue); 
      //System.out.print(stack); 

      //System.out.print(placeOfValue +":" + i); 
      String placement = ""; 
      switch (i) { 
       case 0: 
        placement = ones(placeOfValue); 

        break; 
       case 1: 
        placement = tens(placeOfValue); 

        break; 
       case 2: 
        placement = hundreds(placeOfValue); 

        break; 
       case 3: 
        placement = thousands(placeOfValue); 

        break; 
       default: 
        break; 
      } 

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { 
       stack.push(placement); 
      } 
      integer = integer/10; 

      //System.out.print(placement); 
      // System.out.println(placement.size()); 
      //} 
//    for(int j = 0; j < placement.size(); j++){ 
//         double tenthPower = Math.floor(Math.log10(placement.get(j))); 
//         double place = Math.pow(10, tenthPower); 
//         System.out.println(place); 
// 
//    } 
      // } 
      while (!stack.isEmpty()) { 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { 
        System.out.print(stack.pop()); 
       } 
      } 
//  } else { 
//   System.out.println("Please enter an integer between 0 and 4,999."); 
//  } 

     } 
    } 

`

다른 방법`listener- 버튼을 "변환"여기 내 코드의 샘플입니다 내부 번역기는 로마 숫자를위한 라이브러리와 같습니다. 각 로마 숫자에는 각각 숫자가 들어 있습니다. 다음과 같이 장소 값 중 하나를 선택하십시오.

수천`

public static String thousands(int integer) { 
     String thouValue = ""; 
     switch (integer) { 

      case 1: 
       thouValue = "M"; 
       //System.out.print("M"); 
       break; 
      case 2: 
       thouValue = "MM"; 
       //System.out.print("MM"); 
       break; 
      case 3: 
       thouValue = "MMM"; 
       //System.out.print("MMM"); 
       break; 
      case 4: 
       thouValue = "MMMM"; 
       //System.out.print("MMMM"); 
       break; 
      default: 
       thouValue = ""; 
       break; 
     } 
     return thouValue; 
    } 

`

답변

2

translator() 방법은 최종 출력을 포함하는 문자열을 반환 확인 방법 -.

그래서 그 방법에 while 문 앞에 문자열과 같은 String result = null;을 선언하고 루프에서이 변수 같은 result += stack.pop()에 튀어 값을 추가합니다. 이제

, 당신은 translator(integer) 메소드를 호출 장소, 당신이 다른 곳에서 텍스트 뷰에 액세스 할 수 있도록 당신은, 당신의 텍스트 뷰 클래스 회원을하고 한 OnCreate 번들을 초기화 할 필요가 numeralInput.setText(translator(integer)) 대신

+1

우리는이 질문에 두 가지 다른 테이크를 가지고 있습니다. 어쩌면 함께 대답 할 수도 있습니다. –

+1

그래, 사실이야! –

+0

@PrerakSola 답장을 보내 주셔서 감사합니다! 나는 당신이 지금까지 말한 것을 뒤쫓아 왔고 오류가 발생했습니다. 언급 한대로 텍스트를 설정하는 메서드 호출을 변경하지만 안드로이드 스튜디오 말한다 "setText (void) '메서드를 해결할 수 없다"문자열을 반환하는 내 번역기 메서드를 변경하면 문제가 도움이 될 것이라고? – Rave

2

translator(integer)의 수행 활동.

TextView numeralInput; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.act_main); 
    numeralInput = (TextView) findViewById(R.id.textView); 

convert.setOnClickListener(
      new View.OnClickListener() { 
       public void onClick(View v) { 

        String intValue = numeralInput.getText().toString(); 
        try{ 
         int integer = Integer.parseInt(intValue); 
         if (integer > 0 && integer <= 4999){ 
          translator(integer); 

         }else{ 
          numeralInput.setText("Please enter an integer between 0 and 4,999."); 
         } 

        }catch(NumberFormatException e){ 
         numeralInput.setText("Invalid input try again."); 
        } 
       } 
      } 
    ); 
+1

답장을 보내 주셔서 감사합니다! 수정하고 코드에 몇 가지를 추가하여 작동하는지보고 다시 받아야합니다. – Rave

관련 문제