Beruflich Dokumente
Kultur Dokumente
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////
//Switch
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////
//CheckBox: know the hobby of the user, activate/deactivate the specific action
//Custom CheckBox
//#1 create view in activity layout
<RelativeLayout>
<View width="match" height="1dp" layout_marginTop="" background=""
id="viewStub"/> //seperator
<CheckBox button="@drawable/checkbox_selector" text id layout_below="viewStub"
centerHorizontal layout_marginTop/>
<Button/>
<RelativeLayout>
//#2 create a selector in drawable
<selector>
<item android:state_checked="true" android:drawable="@drawable/checked"/>
<item android:state_checked="false" android:drawable="@drawable/unchecked"/>
</selector>
//#3 get reference
//#4 check checkbox in button
public void onClick(View v){
StringBuilder result = new StringBuilder();
if(customCheckBox.isChecked()){String s1 =
customCheckBox.getText().toString(); result.append(s1);}
if(result!=null && !result.toString().equals(""))
{Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show()}
else{Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_SHORT).show();}
}
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////
//RadioButton
//RadioGroup: contains several radio buttons, marking one radio button as checked
makes all other radio buttons as unchecked
//Dynamic RadioButton
//using "android.view.ViewGroup.LayoutParams" for width/height of views and
implements "setOnCheckedChangeListener"
//#1 create activity layout without radio group/button
<RelativeLayout id="@+id/relative"/></RelativeLayout>
//#2 declare radio group and button + layout in activity class level
RadioGroup group; RadioButton radio1, radio2; RelativeLayout relative;
//#3 create instance of views in onCreate()
group = new RadioGroup(this); relative =
(RelativeLayout)findViewById(R.id.relative); radio = new RadioButton(this);
//#4 set views attributes
radio1.setText("male"); group.addView(radio1);
group.setOrientation(RadioGroup.HORIZONTAL);
//#5 set layout attributes
RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams((int)LayoutParams.WRAP_CONTENT,
(int)LayoutParams.WRAP_CONTENT);
params.leftMargin = 150; params.rightMargin = 100;
group.setLayoutParams(params); relative.addView(group);
//#6 implement setOnCheckedChangeListener
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId){
RadioButton radio = (RadioButton)findViewById(checkedId);
Toast.makeText(getApplicationContext(), radio.getText(),
Toast.LENGTH_SHORT).show();
}
});
//Custom RadioButton
//#1 create view in activity layout
<RelativeLayout>
<RadioGroup id="@+id/group">
<RadioButton id="@+/radio1" button="@drawable/custom_radio_button"
checked="false"/>
<Button/>
<RelativeLayout>
//#2 create a selector in drawable(custom_radio_button)
<selector xmlns:android="">
<item android:state_checked="true"
android:drawable="@drawable/checkedradiobutton"/>
<item android:state_checked="false"
android:drawable="@drawable/uncheckedradiobutton"/>
</selector>
//#3 get reference
RadioGroup group = (RadioGroup)findViewById(R.id.group);
int selectedId = group.getCheckedRadioButton();
RadioButton radio1 = (RadioButton)findViewById(selectedId);
//#4 check radio in button click
if(selectedId == -1){Toast.makeText(getApplicationContext(), "nothing",
Toast.LENGTH_SHORT).show();}
else{Toast.makeText(getApplicationContext(), radio1.getText(),
Toast.LENGTH_SHORT).show();}