如何将textview设置为andrioid中的方法输出? [英] How to set textview as method output in andrioid?

查看:165
本文介绍了如何将textview设置为andrioid中的方法输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用android studio,目前我正在开发一个罗马数字翻译应用程序。该应用程序的界面如下所示:应用程序界面

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

用户将使用键盘输入一个整数,该整数将显示在上面显示的TextView上。当他们点击转换按钮时,它将获取他们输入的整数并将其转换(如果包含字符串或字符,程序将能够捕获输入)。然后,在用户点击转换按钮后,应用程序会将TextView重置为结果。

The user will use the keypad to input a integer which will show up on the TextView shown above. When they hit the convert button it will take the integer they have entered and convert it (the program will be able to catch the input if contains a string or character). Then the app will reset the TextView to the result after the user hits the "convert" button.

目前,我的主要活动包含按钮的onClickListeners和翻译的单独翻译方法。我的问题是转换按钮我不知道如何从转换器方法获取输入并在完成转换时将其设置为TextView。以下是我的代码示例:

Currently, my main activity contains the onClickListeners for the buttons and a separate translator method for the translations. My problem is with the "convert" button I'm not sure how to get the input from the translator method and set it as TextView when it finishes converting. Here is the sample of my code:

转换按钮监听器 -

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.");
//        }

        }
    }

`

翻译器中的其他方法就像一个罗马数字库,每个方法都包含每个地方值的数字,如下所示。

The other methods inside translator is like a library for the roman numerals each containing a numeral for each of the place values as shown below.

千种方法 -

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;
    }

`

推荐答案

让你的翻译器()方法返回一个包含最终输出的字符串。

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

所以在该方法中的 while 语句之前,声明一个字符串,例如
String result = null; 并在循环中将弹出值附加到此变量,如 result + = stack.pop()

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().

现在,您调用转换器(整数)方法的地方,执行 numeralInput.setText(translator(integer))而不是翻译器(整数)

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

这篇关于如何将textview设置为andrioid中的方法输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆