MainActivity.java

package com.example.aimee.buttontest;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.KeyEvent; public class MainActivity extends AppCompatActivity {
TextView textview;//声明全局变量 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1=(Button) findViewById(R.id.myButton1);
final Button button2=(Button) findViewById(R.id.myButton2);
final Button button3=(Button) findViewById(R.id.myButton3); textview=(TextView) findViewById(R.id.myTestView);
final Drawable red_Drawable= ContextCompat.getDrawable(getBaseContext(),R.drawable.RED);
final Drawable blue_Drawable= ContextCompat.getDrawable(getBaseContext(),R.drawable.BLUE);
final Drawable yellow_Drawable= ContextCompat.getDrawable(getBaseContext(),R.drawable.YELLOW); button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str="You have clicked "+button1.getText().toString();
textview.setText(str);
if(textview.getBackground()!=red_Drawable){
textview.setBackground(red_Drawable);
}
}
}); button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str="You have clicked "+button2.getText().toString();
textview.setText(str);
if(textview.getBackground()!=blue_Drawable){
textview.setBackground(blue_Drawable);
}
}
}); button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str="You have clicked "+button3.getText().toString();
textview.setText(str);
if(textview.getBackground()!=yellow_Drawable){
textview.setBackground(yellow_Drawable);
}
}
}); } @SuppressLint("ResourceAsColor")
public boolean onKeyUp(int keyCode, KeyEvent event){
CharSequence Charseq="Change your color here!";
textview.setText(Charseq);
final Drawable white_Drawable= ContextCompat.getDrawable(getBaseContext(),R.drawable.WHITE);
textview.setBackground(white_Drawable);
// textview.setBackgroundColor(0xFFFFFFFF);
return super.onKeyUp(keyCode,event);
} public boolean onKeyDown(int keyCode,KeyEvent event){
CharSequence Charseq="You have pressed ";
Charseq=Charseq+"a key!";
textview.setText(Charseq);
return super.onKeyDown(keyCode,event);
} }

layout/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<drawable name="BLUE">#FF0000FF</drawable>
<drawable name="BLACK">#FF000000</drawable>
<drawable name="RED">#FFFF0000</drawable>
<drawable name="YELLOW">#FFFFFF00</drawable>
<drawable name="WHITE">#FFFFFFFF</drawable>
</resources>

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/myTestView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/WHITE"
android:text="@string/hello"
android:textSize="36sp"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> <Button
android:id="@+id/myButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/myButtonText1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" /> <Button
android:id="@+id/myButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="@string/myButtonText2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/myButton3"
app:layout_constraintStart_toEndOf="@+id/myButton1" /> <Button
android:id="@+id/myButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="@string/myButtonText3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" /> </android.support.constraint.ConstraintLayout>

这是一个简单的通过Button控制文本背景颜色的APP,值得注意的是其中的onKeyDown触发针对back,上下箭头键,或者还有其他一些键,没事,但是按字母键会很快被onKeyUp覆盖。

至于.java文件中注释了一句setBackGroundColor(int),可以自行试试与setBackGround(drawable对象)有何不同。

第十三篇-通过Button设置文本背景颜色的更多相关文章

  1. 【C#/WPF】Button按钮动态设置Background背景颜色

    学习笔记: 在XAML中给Button设置颜色大家都懂的,本篇只是记录用C#代码动态生成的按钮设置Background背景颜色. new一个Button,设置Background时可看到该属性类型是S ...

  2. android中在java代码中设置Button按钮的背景颜色

    android中在java代码中设置Button按钮的背景颜色 1.设置背景图片,图片来源于drawable: flightInfoPanel.setBackgroundDrawable(getRes ...

  3. div 背景色设置_DIV背景颜色设置

    DIV 背景色设置篇-div背景颜色设置篇 一.div标签内直接设置背景颜色   -   TOP <div style="background:#000; color:#FFF&quo ...

  4. 雷林鹏分享:jQuery EasyUI 数据网格 - 条件设置行背景颜色

    jQuery EasyUI 数据网格 - 条件设置行背景颜色 本教程将向您展示如何根据一些条件改变数据网格(datagrid)组件的行样式.当 listprice 值大于 50 时,我们将为该行设置不 ...

  5. Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())

    在Qt中设置widget背景颜色或者图片方法很多种:重写paintEvent() , 调色板QPalette , 样式表setStyleSheet等等. 但是各种方法都有其注意事项,如果不注意则很容易 ...

  6. 设置datagridview中button按钮的背景颜色

    问题:DataGridViewButtonColumn()在datagridview中创建按钮列,如何设置按钮的背景颜色(不是单元格的背景颜色). 回答:可以在dataGridView1_CellPa ...

  7. 设置windows10 背景颜色

    [Win + R ] regedit 打开注册表 HKEY_CURRENT_USER\Control Panel\Colors 1.[InfoWindow] 默认为(白色):255 255 255, ...

  8. 设置vim默认参数 例如设置默认背景颜色

    因个人喜好问题,本人使用vim的时候喜欢将背景颜色设为dark. 但是每次打开一个vim的时候都要重新设置一次,感觉非常麻烦. 总要输入[Esc] :set bg=dark很不方便 粗暴的办法是直接进 ...

  9. JavaGUI——设置框架背景颜色和按钮颜色

    import java.awt.Color; import javax.swing.*; public class MyDraw { public static void main(String[] ...

随机推荐

  1. 安装splash

    参考: https://blog.csdn.net/qq_41020281/article/details/82599075

  2. Saltstack 安装与常用模块

    一.介绍 saltstack是基于C/S服务模式,在该架构中,服务器端叫做Master,客户端叫做Minion.传统的C/S模式我们这样理解,客户端发送请求给服务器端,服务器端接受到来自客户端的请求并 ...

  3. python3 自动识图

    一.安装依赖库 pip install pytesseract pip install pillow 二.安装识图引擎tesseract-ocr https://pan.baidu.com/s/1Qa ...

  4. final关键字的用法

    final关键字的作用 1.被final修饰的类不能被继承 报错信息:cannot inherit from final 'com.dajia.test.Animal' 2.被final修饰的方法不能 ...

  5. vue組件

    組件有局部組件和全局組件,全局組件,其它的元素能夠調用. Prop父組件子組件看不大明白.

  6. jQuery插件开发的基本形式

    (function ($, window, document, undefined) { "use strict"; var defaults = { pageIndex: 0, ...

  7. Cisco IP 电话 将它的voice mail 发送到手机

    功能一.将语音转成文字发送短信(有微软认知.百度认知.云片短信API) 功能二.直接将音频发送到微信 不废话,直接送个包 链接: https://github.com/JaviZhu/KLCN.Spe ...

  8. Express static 托管静态文件 理解

    今天偶尔看了一下服务端渲染,遇到了express.static, 在以前学习webpack配置服务端渲染时,也使用express.static 中间件,两者配置不太一样,由于当时也没有认真学,所以 一 ...

  9. bzoj4152-[AMPPZ2014]The_Captain

    Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. Input 第一行包含一个正整数n(2 ...

  10. Mac下安装MySQL(Mac 10.12)

    系统:Mac OS 10.12 MySQL:5.7.15 前言: 安装mysql有两种方式:1为官方下载dmg安装包.2为使用brew进行安装. 安装步骤: 一.官方下载dmg安装包进行安装 1.登陆 ...