Sie sind auf Seite 1von 4

//Button

//handle Button click


//#1 in java by 2 method
//#A implements an interface called View.OnClickListner
public MainActivity extends Activity implements View.OnClickListener{
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(this);
@Override
public void onClick(View v) {
int id = v.getId();
switch(id) {
case R.id.button1:
// execeute our code
}
}
}
//#B use an anonym interface implementation
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Implements our code do handle user click
}
});

//#2 in XML: makes the activity class code less messy


//in xml
<Button android:onClick="executeHello"/>
//now in java
public void executeHello(View v){}

///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////

//ToggleButton: on/off state, sound, wifi, bluetooth


//check toggle button state
Boolean state = toggle_btn.isChecked();

//#1 create view in activity layout


<RelativeLayout>
<ToggleButton id="@+id/toggle1"
abdroid:textOff="Off" android:textOn="On" //set text when button is in on/off state
OR setTextOn("") setTextOff("")
android:checked="true" //current state
android:drawableTop="@drawable/icon"/> //icon on top of text
//#2 get reference of ToggleButton in onCreate
ToggleButton toggle = (ToggleButton)findViewById(R.id.toggle1);
//#3 add toggle on/off to button click listener
but.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
StringBuilder result = new StringBuilder();
result.append("ToggleButton1: ").append(toggle.getText());
// OR
String result = "ToggleButton1: " + toggle.getText() + "\n";
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT);
}
});

///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////
//Switch

///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////
//CheckBox: know the hobby of the user, activate/deactivate the specific action

//#1 create view in activity layout


<RelativeLayout width="match" height="wrap">
<CheckBox id="@+id/check1" layout_alignParentLeft="true"
layout_alignParentTop="true" text="pizza"/>
<CheckBox id="@+id/check2" layout_alignParentTop="true"
layout_toRightOf="@+id/check1" text="coffe"/>
<CheckBox id="@+id/check3" layout_alignParentTop="true"
layout_toRightOf="@+id/check2" text="burger"/>
<Button id="@+id/btn" layout_below="@+id/check2" layout_toLeftOf="@+id/check3"
text="Order"/>
</<RelativeLayout>
//#2 get reference of views in onCreate()
//#3 check status in button listener
public onClick(View v){
int total = 0; StringBuilder result = new StringBuilder();
if(piza.isChecked()){result.append("\nPizza"); total+=100;}
if(coffe.isChecked()){result.append("\nCoffe"); total+=50;}
if(burger.isChecked()){result.append("\nBurger"); total+=120;}
result.append("\nTotal: " + total + " gram");
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT);
}

//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

//#1 create view in activity layout


<RadioGroup width="wrap" height="wrap" id="@+id/radio_group">
<RadioButton width="match" heigh="wrap" text checked="false"/>
<RadioButton/>
</RadioGroup>
//#2 get reference in onCreate
radioGroup = (RadioGroup)findViewById(R.id.radio_group);
int selectedId = radioGroup.getCheckedRadioButton();
RadioButton selectedRadio = (RadioButton)findViewById(selectedId);
//#3 check radio status in button onclick
if(selectedId == -1){Toast.makeText(Activity.this, "Nothing",
Toast.LENGTH_SHORT).show();}
else{Toast.makeText(Activity.this, selectedRadio.getText(),
Toast.LENGTH_SHORT).show();}

//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();}

Das könnte Ihnen auch gefallen