• 第十三篇-通过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);
        }
    
    }
    View Code

     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>
    View Code

    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>
    View Code

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

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

  • 相关阅读:
    MAZE(2019年牛客多校第二场E题+线段树+矩阵乘法)
    Kth Minimum Clique(2019年牛客多校第二场D题+k小团+bitset)
    Removing Stones(2019年牛客多校第三场G+启发式分治)
    Make Rounddog Happy(2019年杭电多校第十场1011+HDU6701+启发式分治)
    Rikka with Travels(2019年杭电多校第九场07题+HDU6686+树形dp)
    二维平面数点
    Acesrc and Travel(2019年杭电多校第八场06+HDU6662+换根dp)
    Cutting Bamboos(2019年牛客多校第九场H题+二分+主席树)
    Find the median(2019年牛客多校第七场E题+左闭右开线段树)
    Explorer(2019年牛客多校第八场E题+线段树+可撤销并查集)
  • 原文地址:https://www.cnblogs.com/smart-zihan/p/9837098.html
Copyright © 2020-2023  润新知