Swap one text for another using a string in the Menu

Asked

Viewed 65 times

0

I’m having a problem and I’m not finding a solution,

I have a string in a class java, that string is: R.string.text

I will make a menu and put in the app preferences, to select only 1 option, and each option, will change the string.

Example

Menu with 3 options

Option 1 R.string.texto_verb

Option 2 R.string.texto_poetry

Option 3 R.string.text_phrase

If I click on option 2, my string in the other class java will change text to text_poetry.

And I need this option to be stored.

2 answers

0

Hi

If I understood correctly you could do something more or less like this

public class MinhaClasse {

    private String minhaString;

    public void alteraString(int opcao, String str) {

        if (opcao == 1) {
            minhaString = str.replace(str.substring(str.lastIndexOf('.')+1), "texto_verbo");
        }

        if (opcao == 2) {
            minhaString = str.replace(str.substring(str.lastIndexOf('.')+1), "texto_poesia");
        }

        if (opcao == 3) {
            minhaString = str.replace(str.substring(str.lastIndexOf('.')+1), "texto_frase");
        }
    }

    public String getMinhaString() {
        return minhaString;
    }
}
public class Main {

    public static void main(String[] args) {

        String str = "R.string.texto";

        Scanner sc = new Scanner(System.in);

        int opcao;

        System.out.println("Informe a opcao do menu");

        opcao = sc.nextInt();

        System.out.println("A opcao selecionada foi "+opcao);

        MinhaClasse c = new MinhaClasse();
        c.alteraString(opcao, str);

        System.out.println("String alterada "+c.getMinhaString());

    }

}

And on the way out we’d have

Informe a opcao do menu
1
A opcao selecionada foi 1
String alterada R.string.texto_verbo

Informe a opcao do menu
2
A opcao selecionada foi 2
String alterada R.string.texto_poesia

Informe a opcao do menu
3
A opcao selecionada foi 3
String alterada R.string.texto_frase
  • I’m new to programming, so I need to ask you a few questions, André. The Minhaclasse class would be the class where I put the menu for choice, and the Main class is the one where I already have my string, right? And the menu I should do then in Minhaclasse? Thank you

  • Hello, the Minhaclasse class has the responsibility of giving an option - which comes from the - menu and a string changes that string to another value according to its rule( can consider as its business rule ). The Main class is just to see if the business rule (which is implemented in the Minhaclasse class) is as expected. The Main class must be replaced by the class where Voce takes the menu options which in this case may be for example the click of a button or the Submit of a form, ai depends on how the user will select the option

0

You use setText for that. example:

string texto = 'texto';
int opcao = 1;

if (opçao == 1) {
  texto.setText("seutextoaqui");
}

use the event Onclick to do instantly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.