Открыть активность в зависимости от значения TextView - PullRequest
0 голосов
/ 08 июня 2018

У меня есть кнопка и TextView в MainActivity, и у меня есть еще один пример трех действий (один, два, три, четыре), поэтому я хочу, чтобы в TextView текст был «привет», тогда, когда я нажимаю кнопку, одно действие будет открыто ис текстом, равным «привет», он открывает два действия, с «хорошим» текстом, занятием три, и с текстом «спасибо», открывается четвертое действие.

1 Ответ

0 голосов
/ 08 июня 2018
btn = (Button) findViewById(R.id.btn);
tv = (TextView) findViewById(R.id.tv);

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String s = tv.getText().toString();

        if (s.equals("hi")) {
            Intent intent = new Intent(this, One.class);
            startActivity(intent);
        }
        if (s.equals("hello")) {
            Intent intent = new Intent(this, Two.class);
            startActivity(intent);
        }
        if (s.equals("nice")) {
            Intent intent = new Intent(this, Three.class);
            startActivity(intent);
        }
        if (s.equals("thanks")) {
            Intent intent = new Intent(this, Four.class);
            startActivity(intent);
        }
    }
});
...