Bu derste view öğelerinden checkbox ve toggle button’un ne olduğundan, ilgili methodlarından ve listenerlarından bahsedip, bunlarla ilgili örnekler yaptım.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener{ RelativeLayout rl; CheckBox c; ToggleButton t; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); c= (CheckBox) findViewById(R.id.checkBox2); c.setOnClickListener(this); t= (ToggleButton) findViewById(R.id.toggleButton); t.setOnCheckedChangeListener(this); rl=(RelativeLayout) findViewById(R.id.activity_main); } @Override public void onClick(View v) { CheckBox gecici= (CheckBox) v; if(gecici.isChecked()){ Toast.makeText(this, "Seçildi", Toast.LENGTH_LONG).show(); }else { Toast.makeText(this, "Seçim kaldırıldı", Toast.LENGTH_LONG).show(); } } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ c.setChecked(true); rl.setBackgroundColor(Color.BLUE); }else{ c.setChecked(false); rl.setBackgroundColor(Color.RED); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="emrealtunbilek.com.checkboxvetooglebutton.MainActivity"> <CheckBox android:text="Seçildi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:id="@+id/checkBox2" /> <ToggleButton android:text="ToggleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/checkBox2" android:layout_alignParentStart="true" android:layout_marginTop="34dp" android:textOn="AÇIK" android:textOff="KAPALI" android:id="@+id/toggleButton" /> </RelativeLayout> |