How can I set my enum with the value the user enter in a EditText?

风格不统一 提交于 2019-12-11 17:44:09

问题


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

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.


回答1:


The else condition shouldn't be in that class.

You want something like:

public Wissenstand Bestätigung(View v) {
    TextView uWissen = (TextView) findViewById(R.id.textView_Wissen_Titel);
    TextView pWarung = (TextView) findViewById(R.id.textView_Wissen);
    TextView eWissen = (TextView) findViewById(R.id.editText_eingabeWissentsstand);

    knowledge = Wissenstand.fromUserInput( Integer.parseInt( eWissen.getText().toString() );

    if(knowledge == null){
         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);
    }
}



回答2:


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

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

When calling fromUserInput you should add the corresponding try-catch



来源:https://stackoverflow.com/questions/45484272/how-can-i-set-my-enum-with-the-value-the-user-enter-in-a-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!