Android通过点击按钮改变Activity的背景颜色_个人笔记

今天学习了通过两个按钮的点击监听事件。

    private Button button1;     private Button button2;    private LinearLayout layout;

先声明按钮、布局LinearLayout

 protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button1= (Button) this.findViewById(R.id.button1);//获取        button2= (Button) this.findViewById(R.id.button2);//获取        layout= (LinearLayout) this.findViewById(R.id.layout);//获取        button1.setOnClickListener(new View.OnClickListener() {  //事件            @Override            public void onClick(View v) {                layout.setBackgroundColor(Color.RED);                ((Button)v).setText("背景红了");            }        });        button2.setOnClickListener(new View.OnClickListener() { //事件            @Override            public void onClick(View v) {                layout.setBackgroundColor(Color.BLUE);                ((Button)v).setText("背景蓝了");            }        });    }

以下是xml的代码

  <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="红"        android:id="@+id/button1" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="蓝"        android:id="@+id/button2" />

在这里需要注意的是要明确LinearLayout的ID

    android:id="@+id/layout"

实现出来就是点哪个按钮,背景就变什么色。

更多文章:http://blog.csdn.net/qq_26849491

更多相关文章

  1. 无标题文章
  2. android 上层wifi模块调用分析
  3. android 设置桌面背景
  4. [android]在上下文菜单的选中事件中获取列表选中的元素
  5. Android_GPS
  6. android 设置桌面背景
  7. Android获取屏幕宽高、屏幕密度、手动计算view大小
  8. AlertDialog背景的修改
  9. Android中改变Dialog背景透明度

随机推荐

  1. Android(安卓)通过 地名 获得 经纬度 并
  2. Android 带小圆圈的倒计时圆形进度条
  3. Android 如何更换屏幕上锁界面背景图片
  4. 从菜鸟到Android资深工程师的进阶之路
  5. android使用Navigation实现Fragment之间
  6. Android开发学习笔记——四大组件之Conte
  7. android模拟器安装及优化(集锦)
  8. Android的前景
  9. 利用android studio LinearLayout线性布
  10. Android中进行单元测试