如何使用用户在EditText中输入的值来设置我的枚举? [英] How can I set my enum with the value the user enter in a EditText?

查看:196
本文介绍了如何使用用户在EditText中输入的值来设置我的枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个editText,用户输入0到10之间的数字。根据输入的数字,枚举(知识)将被设置。

这是我的代码到目前为止:

I have a editText where the users enter a number between 0 and 10. Depending on their entered number the enum (knowledge) will set.
Here's my code so far:

public int fromUserInput() {
    TextView eWissen = (TextView) findViewById(R.id.editText_eingabeWissentsstand);
    eWissen.getText().toString() == Zahl;
    return Zahl;
}

private enum Wissenstand
{
    BEGINNER, FORTGESCHRITTENER, PRO, GRANDMASTER;

    static Wissenstand fromUserInput(final int input)
    {
        if (input >= 10) {
            return GRANDMASTER;
        } else if (input >= 7) {
            return PRO;
        } else if (input >= 4) {
            return FORTGESCHRITTENER;
        } else if (input >= 0) {
            return BEGINNER;
        } else {
            TextView uWissen = (TextView) findViewById(R.id.textView_Wissen_Titel);
            TextView pWarung = (TextView) findViewById(R.id.textView_Wissen);


        uWissen.setText("Fehler gefunden!");
        uWissen.getResources().getColor(android.R.color.holo_red_dark);
        pWarung.setText("Gib eine Zahl von 0 bis 10 ein!\n0,5-er Schritte sind nicht erlaubt!\nWeitere Informationen kannst du der Legende entnehmen!");
        pWarung.getResources().getColor(android.R.color.holo_red_light);
        }
        return null;
    }
}

此外,我无法在我的...中使用findViewById 由于某些原因,UserInput(Final int input){...}的静态Wissenstand。

如果您需要的东西告诉我,我可以帮助我。

Additionally I can't use "findViewById" in my "static Wissenstand from UserInput(Final int input) {...}" method for some reason.
If you need something tell me, I help wherever I can.

推荐答案

findViewById 不是一个寻找你的意见的魔法。这实际上是一种 查看 活动

findViewById is not a "magical method" that looks for your views. It's actually a method of View or Activity

在你的枚举类里面,你不应该使用那种东西。只需检查int值,如果超出范围就抛出一个 IllegalArgumentException

Inside your enum class you shouldn't use that kind of stuff. Just check the int value and if it's out of range throw an IllegalArgumentException:

static Wissenstand fromUserInput(final int input)
    {
        if (input >= 10) {
            return GRANDMASTER;
        } else if (input >= 7) {
            return PRO;
        } else if (input >= 4) {
            return FORTGESCHRITTENER;
        } else if (input >= 0) {
            return BEGINNER;
        } else {
            throw new IllegalArgumentException("Invalid value");    
        }
    }

从UserInput调用 code>你应该添加相应的try-catch

When calling fromUserInput you should add the corresponding try-catch

这篇关于如何使用用户在EditText中输入的值来设置我的枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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