第十三篇-通过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[] ...
随机推荐
- Django的一些操作与视图函数
一 . Django的安装 pip install django==1.14.6 # 后面的数字是django的版本 二 . 通过命令行(cmd)来创建Django项目 1. 切换到保存项目的文件夹 ...
- qtp 自动化测试---点滴 获取属性性/修改窗体标题
1 GetROProperty获取对应属性值 value url (这里出错了) If Window("新增").WinObject("TRzDBEdit_10" ...
- 解析xml文件 selectSingleNode取不到节点
今天在做批量生成XML的时候,碰到一个情况 解析xml文件 selectSingleNode一直返回NULL. XML的格式开头有一句这个<CE401Message xmlns="ht ...
- Civil 3D 二次开发 事务
事务,一般是指要做的或所做的事情.在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元(unit). 对于初学者来说,从字面上难以理解什么是事务.下面我试着通过讲述事务的作用及特性来帮 ...
- excel导入、导出
http://blog.csdn.net/cjh200102/article/details/12557599 NPOI 工作本HSSFWorkbook 构造方法,无参表示创建一个新的工作本,可以接收 ...
- Let's Encrypt免费泛域名证书申请
一. 下载acme.sh,以下四条命令任选一条即可 curl https://get.acme.sh | shwget -O - https://get.acme.sh | sh curl https ...
- BZOJ1208[HNOI2004]宠物收养场——treap
凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领 ...
- Aizu2130-Billion Million Thousand-dp
用dp求出最大的表达,再用dp求出.//然而并没有想出来 #include <cstdio> #include <string> #include <algorithm& ...
- jsp小测试--猜大小
page1.jpg: <%@ page language="java" import="java.util.*" pageEncoding="g ...
- BZOJ 1977 严格次小生成树(算竞进阶习题)
树上倍增+kruskal 要找严格次小生成树,肯定先要找到最小生成树. 我们先把最小生成树的边找出来建树,然后依次枚举非树边,容易想到一种方式: 对于每条非树边(u,v),他会与树上的两个点构成环,我 ...