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

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

问题描述

我最近刚刚开始使用 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;
    }

`

推荐答案

让你的 translator() 方法返回一个包含最终输出的字符串.

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

现在,在调用 translator(integer) 方法的地方,执行 numeralInput.setText(translator(integer)) 而不是 translator(integer)

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

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

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