首先在你的helloworld程序对应的layout配置文件(res/layout/下的XXX.xml文件)中添加一个按钮,具体代码如下
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
然后在你的继承Activity类的java类中添加按钮的事件监听以及事件处理,代码如下:
public class 你的helloworld类名 extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.你的layout文件名);
//下面的代码用于为按钮注册一个监听
findViewById(R.id.frame_layout).setOnClickListener(new OnClickListener() {
//下面的代码用于处理按钮点击后的事件
public void onClick(View v) {
//下面的代码用于使背景变色
findViewById(R.id.layout).setBackgroundColor(Color.BLUE);
}
});
}
}
android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/qq" > 上面的代码是把drawable下的名字为qq的图片作为背景 如果是某个控件比如图片imageview可以在layout中添加 android:id="@+id/imageview0" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/qq"//添加图片 /> 也可以在代码中添加