-
- Package Com.hangsheng.button;
- import android.app.Activity;
- import Android.os.Bundle;
- import Android.view.View;
- import Android.widget.Button;
- Public class ex07_widgetbuttonactivity extends Activity {
- /** called when the activity is first created. * /
- @Override
- Public void onCreate (Bundle savedinstancestate) {
- Super. OnCreate (savedinstancestate);
- Setcontentview (R.layout.main); //Associated layout template file Main.xml
- Find_and_modify_button (); //Call member function Find_and_modity_button
- }
- private void Find_and_modify_button () {
- Button button = (button) Findviewbyid (R.id.button); //Instantiate a button object with a resource with ID as button in the resource
- Button.setonclicklistener (Button_listener); //Set the listener for the object button
- };
- Private button.onclicklistener button_listener =new Button.onclicklistener () { // member button Listener Object
- @Override
- Public void OnClick (View v) { //button event
- //TODO auto-generated method stub
- Settitle ("button was clicked");
- }
- };
- }
Layout template file Main.xml:
- <? XML version="1.0" encoding="Utf-8" ?>
- <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- Android:text="@string/hello" />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- Android:text="This is button" />
- </linearlayout>
How to use the android button