Advertisement
tpbolon

Radio Button

Jun 18th, 2020
1,760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. package com.example.radiogenre;
  2.  
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.RadioButton;
  10. import android.widget.RadioGroup;
  11. import android.widget.Toast;
  12.  
  13. public class MainActivity extends Activity {
  14.     private RadioGroup rgjeniskelamin;
  15.     private Button btPilih;
  16.    
  17.  
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_main);
  22.         rgjeniskelamin=(RadioGroup)findViewById(R.id.rgkelamin);
  23.         btPilih=(Button)findViewById(R.id.btpilih);
  24.         btPilih.setOnClickListener(new OnClickListener() {
  25.            
  26.             @Override
  27.             public void onClick(View arg0) {
  28.                 // TODO Auto-generated method stub
  29.                 int id=rgjeniskelamin.getCheckedRadioButtonId();
  30.                 if (id==-1) {
  31.                     Toast.makeText(MainActivity.this, "Jenis Kelamin Belum dipilih!!!", Toast.LENGTH_SHORT).show();
  32.                    
  33.                 } else {
  34.                     switch (id) {
  35.                     case R.id.rdlaki:
  36.                         Toast.makeText(MainActivity.this,
  37.                                 "Saya Seorang "+((RadioButton)findViewById(id)).getText(),
  38.                                 Toast.LENGTH_SHORT).show();
  39.                         break;
  40.                     case R.id.rdperempuan:
  41.                         Toast.makeText(MainActivity.this,
  42.                                 "Saya Seorang "+((RadioButton)findViewById(id)).getText(),
  43.                                 Toast.LENGTH_SHORT).show();
  44.                         break;
  45.                     default:
  46.                         break;
  47.                     }
  48.                 }
  49.             }
  50.         });
  51.     }
  52.  
  53.  
  54.     @Override
  55.     public boolean onCreateOptionsMenu(Menu menu) {
  56.         // Inflate the menu; this adds items to the action bar if it is present.
  57.         getMenuInflater().inflate(R.menu.main, menu);
  58.         return true;
  59.     }
  60.    
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement