Add one vowel at a time to a Textview

Asked

Viewed 30 times

0

I have 5 buttons that emulate vowels, as each button adds one vowel at a time to a display, defined as TextView ?

  • 1

    Each time you press the key, for example "A" button, you concatenate the contents of the textview + "A", and you will be the textview again. Remembering that if the textview is empty, it can crash, so check before with the length.

  • "will be" = "arrow"

1 answer

2

  1. Place the 5 buttons and a Textview on the screen.
  2. Implement the View.Onclicklistener class.
  3. Set the event for this class for each button.
  4. When shooting the event, use a switch command to manipulate the buttons.
    • With an auxiliary "Text" string, the vowels are concatenated. At the end of the event Textview is defined.

private Button btnA, btnE, btnI, btnO, btnU;
private TextView textView;
private String Texto="";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.texto);

    btnA = (Button) findViewById(R.id.btnA);
    btnE = (Button) findViewById(R.id.btnE);
    btnI = (Button) findViewById(R.id.btnI);
    btnO = (Button) findViewById(R.id.btnO);
    btnU = (Button) findViewById(R.id.btnU);

    btnA.setOnClickListener(this);
    btnE.setOnClickListener(this);
    btnI.setOnClickListener(this);
    btnO.setOnClickListener(this);
    btnU.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.btnA:
            Texto += "A";
        break;
        case R.id.btnE:
            Texto += "E";
            break;
        case R.id.btnI:
            Texto += "I";
            break;
        case R.id.btnO:
            Texto += "O";
            break;
        case R.id.btnU:
            Texto += "U";
            break;
    }
    textView.setText(Texto);
}

That’s the way I would do it now. There are other ways to do it.

Browser other questions tagged

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