Android's implementation of each component click event Handling:
Note: (TextView This component to click to produce the effect, to set, android:clickable= "true" this property )
Layout: Layout/activity_main.xml
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >
<textview
Android:id= "@+id/tv"
android:text= "Click to change Font Color"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:onclick= "OnClick"
Android:clickable= "true"
/>
<imageview
Android:id= "@+id/iv"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:src= "@drawable/ic_launcher"
android:onclick= "OnClick"
/>
<radiogroup
Android:id= "@+id/rg"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:orientation= "Horizontal" >
<radiobutton
Android:id= "@+id/rbmale"
android:text= "Male"
Android:checked= "true"
android:onclick= "OnClick"
/>
<radiobutton
Android:id= "@+id/rbfamale"
android:text= "Female"
android:onclick= "OnClick"/>
<checkbox
Android:id= "@+id/cbmale"
android:text= "Change Male"
android:onclick= "OnClick"/>
<checkbox
Android:id= "@+id/cbfamale"
android:text= "Changing Woman"
android:onclick= "OnClick"/>
</RadioGroup>
</LinearLayout>
Java class:
Package com.example.day03_01;
Import android.support.v7.app.ActionBarActivity;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.widget.CheckBox;
Import Android.widget.ImageView;
Import Android.widget.RadioButton;
Import Android.widget.TextView;
public class Mainactivity extends Actionbaractivity {
Private RadioButton Rbmale;//Initialize-----male
Private RadioButton Rbfamale;//Initialize------female
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Init ();
}
private void Init () {//Initialize----male, female
Rbmale = (RadioButton) Findviewbyid (R.id.rbmale);
Rbfamale = (RadioButton) Findviewbyid (R.id.rbfamale);
}
public void OnClick (View v) {
LOG.I ("Main", V.getid () + "");
Switch (V.getid ()) {
Case R.id.tv:
TextView TV = (TextView) v;
LOG.I ("Main", Tv.gettext (). toString ());
Tv.settextcolor (color.red);
Break
Case R.ID.IV:
ImageView IV =(ImageView) v;
Iv.setimageresource (R.DRAWABLE.P01);
Break
Case R.id.cbmale:
CheckBox cb = (checkbox) v;
Rbmale.setchecked (cb.ischecked ());
Rbfamale.setchecked (!cb.ischecked ());
Break
Case R.id.cbfamale:
CheckBox CBFA = (checkbox) v;
Rbmale.setchecked (!cbfa.ischecked ());
Rbfamale.setchecked (cbfa.ischecked ());
Break
}
}
}
Effect:
650) this.width=650; "src=" http://img.blog.csdn.net/20141122133009109 "/>
This article from "Follow the heart of the other side" blog, declined reprint!
Android's implementation of individual component click event Handling