Advertisement
Guest User

java android

a guest
Oct 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. package com.example.bau.counter;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.RadioButton;
  8. import android.widget.RadioGroup;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.  
  14. private Button countButton, resetButton;
  15. private TextView mTextViewCounter;
  16. private RadioGroup mRadioGroup;
  17. private RadioButton upRadio, downRadio;
  18. private int counter = 0;
  19.  
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24.  
  25. countButton = (Button) findViewById(R.id.countButton);
  26. resetButton = (Button) findViewById(R.id.resetButton);
  27. mTextViewCounter = (TextView) findViewById(R.id.counterTab);
  28. mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
  29. upRadio = (RadioButton) findViewById(R.id.countUpRadio);
  30. downRadio = (RadioButton) findViewById(R.id.countDownRadio);
  31.  
  32. upRadio.toggle();
  33.  
  34. countButton.setOnClickListener(new View.OnClickListener() {
  35. @Override
  36. public void onClick(View v) {
  37. int checked = mRadioGroup.getCheckedRadioButtonId();
  38.  
  39. if (checked == upRadio.getId()) {
  40. counter++;
  41. Toast.makeText(MainActivity.this, "Incrementing the counter", Toast.LENGTH_SHORT).show();
  42. }
  43. else if (checked == downRadio.getId()) {
  44. counter--;
  45. Toast.makeText(MainActivity.this, "Decrementing the counter", Toast.LENGTH_SHORT).show();
  46. }
  47.  
  48. mTextViewCounter.setText(counter + "");
  49. }
  50. });
  51.  
  52. resetButton.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View v) {
  55. counter = 0;
  56. mTextViewCounter.setText(counter + "");
  57. }
  58. });
  59.  
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement