How to set textview as method output in andrioid?

前端 未结 2 412
忘掉有多难
忘掉有多难 2021-01-20 07:19

I have just started to use android studio recently and currently I am working on a Roman Numeral Translator app. The app\'s interface looks like this: Application interface<

相关标签:
2条回答
  • 2021-01-20 07:27

    You need to make your TextView a class member and initialise it in your oncreate bundle, so that you can access the textview elsewhere in the activity.

    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.");
                        }
                    }
                }
        );
    
    0 讨论(0)
  • 2021-01-20 07:43

    Make your translator() method return a string which contains the final output.

    So before the while statement in that method, declare a string such as String result = null; and in the loop append the popped values to this variable like, result += stack.pop().

    Now, the place where you call the translator(integer) method, do numeralInput.setText(translator(integer)) instead of translator(integer)

    0 讨论(0)
提交回复
热议问题