Android's implementation of individual component click event Handling

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.