Advertisement
ThreeYulianto

rb_Java

Jan 18th, 2020
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3.     ToggleButton toggle;
  4.     CheckBox cb;
  5.  
  6.     @Override
  7.     protected void onCreate(Bundle savedInstanceState) {
  8.         super.onCreate(savedInstanceState);
  9.         setContentView(R.layout.activity_main);
  10.  
  11.         ToggleButton toggle = (ToggleButton) findViewById(R.id.toggle1);
  12.         toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  13.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  14.                 if (isChecked) {
  15.                     // The toggle is enabled
  16.                     Toast.makeText(getApplicationContext(),"Tombol ON", Toast.LENGTH_SHORT).show();
  17.                 } else {
  18.                     // The toggle is disabled\
  19.                     Toast.makeText(getApplicationContext(),"Tombol OFF", Toast.LENGTH_SHORT).show();
  20.                 }
  21.             }
  22.         });
  23.  
  24.         CheckBox cb = (CheckBox) findViewById(R.id.cb);
  25.         cb.setOnClickListener(new View.OnClickListener() {
  26.             @Override
  27.             public void onClick(View v) {
  28.                 boolean checked = ((CheckBox) v).isChecked();
  29.                 // Check which checkbox was clicked
  30.                 if (checked) {
  31.                     // Do your coding
  32.                     Toast.makeText(getApplicationContext(), "Tombol CHECKED", Toast.LENGTH_SHORT).show();
  33.                 } else {
  34.                     Toast.makeText(getApplicationContext(), "Tombol UNCHECKED", Toast.LENGTH_SHORT).show();
  35.                     // Do your coding
  36.                 }
  37.             }
  38.         });
  39.  
  40.         final RadioButton rblaki, rbperempuan;
  41.         rblaki = (RadioButton)findViewById(R.id.rb_1);
  42.         rbperempuan = (RadioButton)findViewById(R.id.rb_2);
  43.         Button btn = (Button)findViewById(R.id.btn1);
  44.         btn.setOnClickListener(new View.OnClickListener() {
  45.             @Override
  46.             public void onClick(View view) {
  47.                 String result = (rblaki.isChecked())?"laki":(rbperempuan.isChecked())?"perempuan":"";
  48.  
  49.                 Toast.makeText(getApplicationContext(), "Tombol Radio = " + result , Toast.LENGTH_SHORT).show();
  50.             }
  51.         });
  52.     }
  53.  
  54.     public void tampil(View view){
  55.         Toast.makeText(getApplicationContext(), "tombol ditekan", Toast.LENGTH_SHORT).show();
  56.     }
  57.  
  58.     public void onRadioButtonClicked(View view) {
  59.         boolean checked = ((RadioButton) view).isChecked();
  60.         String str="";
  61.         // Check which radio button was clicked
  62.         switch(view.getId()) {
  63.             case R.id.rb_1:
  64.                 if(checked)
  65.                     str = "laki";
  66.                 break;
  67.             case R.id.rb_2:
  68.                 if(checked)
  69.                     str = "perempuan";
  70.                 break;
  71.         }
  72.         Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement