Android添加 按钮事件响应 - [Android]

Android添加 按钮事件响应 - [Android]

版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://shexinwei.blogbus.com/logs/106831915.html

在程序中添加一个按钮,并添加按钮相应事件的步骤:

1.在相应的Activity(例如:hello)所对应的xml布局文件中添加按钮控件。

<Button android:id="@+id/continue_button" android:layout_gravity="center" android:layout_width="wrap_content" android:text="@string/main_title" android:layout_height="wrap_content"></Button>

2.在class hello的OnCreate函数中添加按钮的单击事件监听器

 View continue_button = this.findViewById(R.id.continue_button);

        continue_button.setOnClickListener((OnClickListener) this);

3.实现单击后所要显示的Activity类的布局文件:about.xml

<?xml version="1.0" encoding="UTF-8"?>

<LinearLayout android:background="@color/background" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical">

<TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/aboutInfo"/>

</LinearLayout>


 

4.实现单击后所要显示的Activity类:

public class About extends Activity {

         @Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

this.setContentView(R.layout.about);

         }

}

5.实现监听器接口OnClickListener中的方法OnClick()

public void onClick(View v) {

if(R.id.continue_button == v.getId())

{

/* TextView text = new TextView(this);

text.setText("this is continue button");

this.setContentView(text);*/

Intent i = new Intent(this,About.class);

this.startActivity(i);

}

 

if(R.id.newgame_button == v.getId())

{

Intent i = new Intent(this,image.class);

this.startActivity(i);

}

}

}

6.在AndroidManifest.xml文件中添加新建的Activity:

<activity android:name=".About" android:label="@string/about_title">

posted on 2011-10-16 08:08  antyi  阅读(20772)  评论(1)    收藏  举报

导航