zoukankan      html  css  js  c++  java
  • 2_1.8_点击按钮__改变背景颜色

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
    android:id="@+id/tvTtitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.172"
    android:textSize="17dp"
    android:textColor="@color/myRed"
    />

    <Button
    android:id="@+id/btnSayHello"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="200dp"
    android:text="打招呼"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    <Button
    android:id="@+id/btnRed"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="260dp"
    android:text="红色"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    <Button
    android:id="@+id/btnGreen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="320dp"
    android:text="绿色"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    <Button
    android:id="@+id/btnBlue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="380dp"
    android:text="蓝色"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

    ------------------------------------------------------------------

    ColorActivity.java

    package com.example.dfby.myapplication;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    public class ColorActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btnRed,btnGreen,btnBlue;
    private TextView tvTtitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.i("我的日志","这是我的自定义Activity");

    //设置有颜色
    tvTtitle=findViewById(R.id.tvTtitle);
    btnRed=findViewById(R.id.btnRed);//获取按钮对象
    btnGreen=findViewById(R.id.btnGreen);//获取按钮对象
    btnBlue=findViewById(R.id.btnBlue);//获取按钮对象
    btnRed.setOnClickListener(this);
    btnGreen.setOnClickListener(this);
    btnBlue.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
    Toast.makeText(this,"干嘛点击我",Toast.LENGTH_SHORT).show();
    if(v.getId()==R.id.btnRed){
    getWindow().setBackgroundDrawableResource(R.color.myRed);
    }else if(v.getId()==R.id.btnGreen){
    getWindow().setBackgroundDrawableResource(R.color.myGreen);
    }else if(v.getId()==R.id.btnBlue){
    getWindow().setBackgroundDrawableResource(R.color.myBlue);
    }
    }
    }

  • 相关阅读:
    常见的单链表题目
    一个string类的几个函数
    strcpy和memcpy的区别
    字符串及 strcpy几种写法
    什么函数不能声明为虚函数
    STL中Vector和List的底层数据结构
    C/C++堆、栈及静态数据区详解
    tcp四次握手
    几个知识点
    内存对齐的规则与作用
  • 原文地址:https://www.cnblogs.com/bj-tony/p/10243501.html
Copyright © 2011-2022 走看看