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对象)有何不同。

最新文章

  1. javascript平时小例子⑧(导航置顶效果)
  2. 总结的js性能优化方面的小知识
  3. 使用微信JS-SDK调用微信浏览器的接口
  4. 2.建立exception包,建立Bank类,类中有变量double balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,
  5. 如何使用和了解ALTERA的IP核
  6. C#文件后缀名详解
  7. iBatis 的删除一条记录
  8. 笔记之Cyclone IV第一卷第三章器件中的存储器模块
  9. [.NET] 一步步打造一个简单的 MVC 电商网站 - BooksStore(三)
  10. js+画曲线和圆 并限制圆的渲染范围
  11. [乐意黎原创] cuteftp 9 显示中文乱码
  12. 9.11 test
  13. 网络基础tcp/ip协议一
  14. 小技巧-C#文本快速删除空行
  15. go [第一篇]初识
  16. hbase_存储模型
  17. CentOS7 完整安装后创建私有的yum仓库
  18. DACLs and ACEs
  19. RateLimiter 限流
  20. 简单mysql类

热门文章

  1. 【apache2】AH00543: httpd: bad user name apache
  2. RestTemplate proxy 设置方式
  3. oracle11g安装教程完整版
  4. Js--String、Date、Array对象
  5. socket跟TCP/IP 的关系,单台服务器上的并发TCP连接数可以有多少
  6. GLSL 变量属性
  7. sql server2012学习笔记
  8. kubernetes ceph-rbd挂载步骤 类型PersistentVolume
  9. jsp大学课程hi实验:分页在线测评(session的使用)
  10. Django+Xadmin打造在线教育系统(九)