How can I set my enum with the value the user ente

2019-09-16 19: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