第十三篇-通过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[] ...
随机推荐
- centos6.8 安装wget
1.执行yum -y install wget 提示:没有相应的包.原因:默认最小化安装没有装wget,而且默认源中没有wget,所以只能先切换源配置. (1)下载.阿里的源配置,并放到/etc/yu ...
- Zend Framework2从入门到精通
1. 下载安装zf2的web程序 步骤: 第一步,保证得到一个基本的zf2框架 直接从官网下载并解压即可:http://www.zendframework.org.cn/downloads/lates ...
- PLSQL 错误问题:Datebase character set (AL32UTF-8) and Client character set (ZHS16GBK) are different.
(解决不了,网上用的是Orecal,我用的只是客户端.) 网上找到解决方法 打开注册表(ctr+R,输入regedit),根据报错提示找到注册表位置,但本机是win10 64位系统,根据以上路径找不到 ...
- pip 指定版本
要用 pip 安装特定版本的 Python 包,只需通过 == 操作符 指定,例如: pip install -v pycrypto==2.3 将安装 pycrypto 2.3 版本.
- Using MongoDB with Web API and ASP.NET Core
MongoDB is a NoSQL document-oriented database that allows you to define JSON based documents which a ...
- AgilePoint数据库模式中当前所有表的列表
表名 描述 WF_ACTIVITY_INSTS 包含有关活动实例的信息. WF_ASSIGNED_OBJECTS 包含有关用户角色的分配对象的信息. WF_AUDIT_TRAILS 包含有关流程模板的 ...
- 51nod2383
2383 高维部分和 1 秒 131,072 KB 80 分 5 级题 输入一个长度为n的数组a[i],下标从0开始(0到n-1)保证n是2的整数次幂,对于每个i (0 <= i < ...
- python绘制图形
python能快速解决日常工作中的小任务,比如数据展示. python做数据展示,主要用到matplotlib库,使用简单的代码,就可以很方便的绘制折线图.柱状图等.使用Java等,可能还需要配合 ...
- python变量与基础数据类型
一.什么是变量 变量是什么? 变量:把程序运行的中间结果临时的存在内存里,以便后续的代码调用.在python中一切都是变量. 1.python变量命名的要求 1,必须有数字,字母,下划线任意组合. ...
- 微服务配合docker使用
1.docker 安装 rabbitmq 启动脚本: docker run -d --name rabbitmq --publish : \ --publish : --publish : --pub ...