第十三篇-通过Button设置文本背景颜色
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设置文本背景颜色的更多相关文章
- 【C#/WPF】Button按钮动态设置Background背景颜色
学习笔记: 在XAML中给Button设置颜色大家都懂的,本篇只是记录用C#代码动态生成的按钮设置Background背景颜色. new一个Button,设置Background时可看到该属性类型是S ...
- android中在java代码中设置Button按钮的背景颜色
android中在java代码中设置Button按钮的背景颜色 1.设置背景图片,图片来源于drawable: flightInfoPanel.setBackgroundDrawable(getRes ...
- div 背景色设置_DIV背景颜色设置
DIV 背景色设置篇-div背景颜色设置篇 一.div标签内直接设置背景颜色 - TOP <div style="background:#000; color:#FFF&quo ...
- 雷林鹏分享:jQuery EasyUI 数据网格 - 条件设置行背景颜色
jQuery EasyUI 数据网格 - 条件设置行背景颜色 本教程将向您展示如何根据一些条件改变数据网格(datagrid)组件的行样式.当 listprice 值大于 50 时,我们将为该行设置不 ...
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
在Qt中设置widget背景颜色或者图片方法很多种:重写paintEvent() , 调色板QPalette , 样式表setStyleSheet等等. 但是各种方法都有其注意事项,如果不注意则很容易 ...
- 设置datagridview中button按钮的背景颜色
问题:DataGridViewButtonColumn()在datagridview中创建按钮列,如何设置按钮的背景颜色(不是单元格的背景颜色). 回答:可以在dataGridView1_CellPa ...
- 设置windows10 背景颜色
[Win + R ] regedit 打开注册表 HKEY_CURRENT_USER\Control Panel\Colors 1.[InfoWindow] 默认为(白色):255 255 255, ...
- 设置vim默认参数 例如设置默认背景颜色
因个人喜好问题,本人使用vim的时候喜欢将背景颜色设为dark. 但是每次打开一个vim的时候都要重新设置一次,感觉非常麻烦. 总要输入[Esc] :set bg=dark很不方便 粗暴的办法是直接进 ...
- JavaGUI——设置框架背景颜色和按钮颜色
import java.awt.Color; import javax.swing.*; public class MyDraw { public static void main(String[] ...
随机推荐
- Linux基础学习笔记2-文件管理和重定向
本节内容 1)文件系统结构元素 2)创建和查看文件 3)复制.转移和删除文件 4)软和硬链接 5)三种I/O设备 6)把I/O重定向至文件 7)使用管道 文件系统和结构 文件系统 文件和目录被组织成一 ...
- delphi中如何实现DBGrid中的两列数据想减并存入另一列
可参考下面的例子: 数据自动计算的实现:“金额”是由“单价”和“工程量”相乘直接得来的,勿需人工输入. 这可在“数据源构件”的onupdatedata例程添加如下代码实现: procedure T ...
- java 中的打印流
package cn.zhou; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.F ...
- ES 6 系列 - Proxy
Proxy 用于修改某些操作的默认行为,等同于在语言层面做出修改,所以是一种“元编程”,即对编程语言进行编程. 简单地理解,就是在目标对象之前假设一层“拦截”,外界对改对象的访问,都必须先通过这层拦截 ...
- UVA 1602 Lattice Animals
题目 输入n.w.h($1\leqslant n \leqslant 10, 1\leqslant w,h \leqslant n$),求能放在w*h网格里的不同的n连块的个数(注意,平移.旋转.翻转 ...
- linux 安装Brew
点击查看原文 Linuxbrew:Linux下的Homebrew amendgit 关注 2017.02.16 17:20* 字数 455 阅读 4745评论 0喜欢 2 前不久还在跟同事抱怨ubun ...
- BZOJ1150[CTSC2007]数据备份Backup——模拟费用流+堆+链表
题目描述 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味 的,因此你想设计一个系统让不同的办公楼彼此之间互相备份,而你则坐在家中尽享计算机游 ...
- [IOI2018]组合动作——构造
题目连接: [IOI2018]combo 题目大意:有一个未知的长度为n的字符串$T$,只包含$A,B,X,Y$四个字符且首字母只出现一次,每一次你可以询问一个长度不超过$4n$的字符串$S$,交互库 ...
- 李昊大佬的CV模板
#include<cstdio> #include<iostream> #include<cstdlib> #include<iomanip> #inc ...
- POJ3468/splay树/成段更新
板子题,正在努力看懂板子.. http://blog.csdn.net/acm_cxlove/article/details/7815019 http://www.cnblogs.com/kuangb ...