Android Studio||TextView点击改变背景颜色/点击出现图像imageView/延时恢复(内

step by step.

目录

参考:

1. xml中原本颜色设置

xml: 

 xml:

2.颜色修改

java:

java: 

 3. 整个代码:

xml:

java:

4.延时恢复(Handler)


参考:

TextView.setTextColor颜色值的理解_weixin_30249203的博客-CSDN博客TextView.setTextColor(int value),括号里是int型的值,可以填充的值有三种情况。第一种:系统自带的颜色类,TextView.setTextColor(android.graphics.Color.RED)第二种:十六进制的颜色值,TextView.setTextColor(0xffff3030);说明:0x是代表颜色整数的标记,ff是表示透明度,ff30...https://blog.csdn.net/weixin_30249203/article/details/95703666?ops_request_misc=&request_id=&biz_id=102&utm_term=textView%20set%E9%A2%9C%E8%89%B2&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-3-95703666.142%5Ev76%5Einsert_down38,201%5Ev4%5Eadd_ask,239%5Ev2%5Einsert_chatgpt&spm=1018.2226.3001.4187

Android中实现延时执行操作的几种方法_android studio 延时函数_dandelionsnow的博客-CSDN博客转载自:https://blog.csdn.net/mq2856992713/article/details/52005253目前开发涉及到一个延迟操作场景:点击一个按钮,实现一个旋转动画,2秒钟后,然后需要跳转到一个新页面的操作,(比如微信收红包的操作)在这里记录一下几种实现延时执行操作的方法:1.使用线程的休眠实现延时操作 new Thread() { ...https://blog.csdn.net/hanxiongwei/article/details/88849487?ops_request_misc=&request_id=&biz_id=102&utm_term=android%20studio%20%E5%BB%B6%E6%97%B6&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-6-88849487.142%5Ev76%5Einsert_down38,201%5Ev4%5Eadd_ask,239%5Ev2%5Einsert_chatgpt&spm=1018.2226.3001.4187

1. xml中原本颜色设置

xml: 

        <TextViewandroid:id="@+id/choice_4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="12dp"android:background="#ffe4c7"android:gravity="center"android:padding="10dp"android:text="猫"android:textColor="@color/black"android:textSize="150px"android:textStyle="bold" />

效果:


 xml:

<TextViewandroid:id="@+id/choice_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="12dp"android:background="#ffe4c7"android:gravity="center"android:padding="10dp"android:text="毛"android:textColor="@color/black"android:textSize="150px"android:textStyle="bold" />

 效果: 

2.颜色修改

java:

ImageView tips = (ImageView) findViewById(R.id.right_);TextView answer = (TextView) findViewById(R.id.choice_4); //正确答案answer.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){//点击正确答案answer.setBackgroundColor(0xff98e6b6);answer.setTextColor(android.graphics.Color.RED);tips.setVisibility(View.VISIBLE);}});

 效果:

java: 

//错误答案TextView answer_w = (TextView) findViewById(R.id.choice_1);ImageView tip = (ImageView) findViewById(R.id.wrong_1);wro(answer_w,tip);answer_w = (TextView) findViewById(R.id.choice_2);tip = (ImageView) findViewById(R.id.wrong_2);wro(answer_w,tip);answer_w = (TextView) findViewById(R.id.choice_3);tip = (ImageView) findViewById(R.id.wrong_3);wro(answer_w,tip);answer_w = (TextView) findViewById(R.id.choice_5);tip = (ImageView) findViewById(R.id.wrong_5);wro(answer_w,tip);answer_w = (TextView) findViewById(R.id.choice_6);tip = (ImageView) findViewById(R.id.wrong_6);wro(answer_w,tip);

 效果: 

 

 3. 整个代码:

xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"android:background="@drawable/pic_bg_self1"tools:context=".TestActivity_self"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="-8dp"android:gravity="center_vertical"android:orientation="horizontal"><TextViewandroid:id="@+id/choice_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="12dp"android:background="#ffe4c7"android:gravity="center"android:padding="10dp"android:text="毛"android:textColor="@color/black"android:textSize="150px"android:textStyle="bold" /><TextViewandroid:id="@+id/choice_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="12dp"android:background="#ffe4c7"android:gravity="center"android:padding="10dp"android:text="茂"android:textColor="@color/black"android:textSize="150px"android:textStyle="bold" /><TextViewandroid:id="@+id/choice_3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="12dp"android:background="#ffe4c7"android:gravity="center"android:padding="10dp"android:text="狗"android:textColor="@color/black"android:textSize="150px"android:textStyle="bold" /><TextViewandroid:id="@+id/choice_4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="12dp"android:background="#ffe4c7"android:gravity="center"android:padding="10dp"android:text="猫"android:textColor="@color/black"android:textSize="150px"android:textStyle="bold" /><TextViewandroid:id="@+id/choice_5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="12dp"android:background="#ffe4c7"android:gravity="center"android:padding="10dp"android:text="苗"android:textColor="@color/black"android:textSize="150px"android:textStyle="bold" /><TextViewandroid:id="@+id/choice_6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="12dp"android:background="#ffe4c7"android:gravity="center"android:padding="10dp"android:text="喵"android:textColor="@color/black"android:textSize="150px"android:textStyle="bold" /></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_alignParentBottom="true"android:layout_marginBottom="-25dp"android:orientation="horizontal"><ImageViewandroid:id="@+id/wrong_1"android:layout_width="50dp"android:layout_height="50dp"android:layout_alignParentBottom="true"android:layout_marginBottom="0dp"android:layout_margin="25dp"android:visibility="invisible"android:background="@drawable/pic_icon_wrong" /><ImageViewandroid:id="@+id/wrong_2"android:layout_width="50dp"android:layout_height="50dp"android:layout_alignParentBottom="true"android:layout_margin="25dp"android:layout_marginBottom="0dp"android:visibility="invisible"android:background="@drawable/pic_icon_wrong" /><ImageViewandroid:id="@+id/wrong_3"android:layout_width="50dp"android:layout_height="50dp"android:layout_alignParentBottom="true"android:layout_marginBottom="0dp"android:layout_margin="25dp"android:visibility="invisible"android:background="@drawable/pic_icon_wrong" /><ImageViewandroid:id="@+id/right_"android:layout_width="50dp"android:layout_height="50dp"android:layout_alignParentBottom="true"android:layout_marginBottom="0dp"android:layout_margin="25dp"android:visibility="invisible"android:background="@drawable/pic_icon_right" /><ImageViewandroid:id="@+id/wrong_5"android:layout_width="50dp"android:layout_height="50dp"android:layout_alignParentBottom="true"android:layout_margin="25dp"android:layout_marginBottom="0dp"android:visibility="invisible"android:background="@drawable/pic_icon_wrong" /><ImageViewandroid:id="@+id/wrong_6"android:layout_width="50dp"android:layout_height="50dp"android:layout_alignParentBottom="true"android:layout_margin="25dp"android:layout_marginBottom="0dp"android:visibility="invisible"android:background="@drawable/pic_icon_wrong" /></LinearLayout>
</RelativeLayout>

java:

package com.example.childlearningsys;import androidx.appcompat.app.AppCompatActivity;import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;public class TestActivity_self extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_test_self);ImageView tips = (ImageView) findViewById(R.id.right_);TextView answer = (TextView) findViewById(R.id.choice_4); //正确答案answer.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){//点击正确答案answer.setBackgroundColor(0xff98e6b6);answer.setTextColor(android.graphics.Color.RED);tips.setVisibility(View.VISIBLE);}});//错误答案TextView answer_w = (TextView) findViewById(R.id.choice_1);ImageView tip = (ImageView) findViewById(R.id.wrong_1);wro(answer_w,tip);answer_w = (TextView) findViewById(R.id.choice_2);tip = (ImageView) findViewById(R.id.wrong_2);wro(answer_w,tip);answer_w = (TextView) findViewById(R.id.choice_3);tip = (ImageView) findViewById(R.id.wrong_3);wro(answer_w,tip);answer_w = (TextView) findViewById(R.id.choice_5);tip = (ImageView) findViewById(R.id.wrong_5);wro(answer_w,tip);answer_w = (TextView) findViewById(R.id.choice_6);tip = (ImageView) findViewById(R.id.wrong_6);wro(answer_w,tip);}public void wro(TextView an_w,ImageView tip){an_w.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){an_w.setBackgroundColor(0xffef6c54);an_w.setTextColor(0xfffcc362);tip.setVisibility(View.VISIBLE);}});}
}

效果:

4.延时恢复(Handler)

java:(自定义sleepT)

public void sleepT(TextView an,ImageView tip){//页面静止Handler handler = new Handler();handler.postDelayed(new Runnable() {@Overridepublic void run() {tip.setVisibility(View.INVISIBLE);an.setBackgroundColor(0xffffe4c7);an.setTextColor(0xff000000);}}, 1000);//1秒后执行Runnable中的run方法}

 更改整体代码:

java:

package com.example.childlearningsys;import androidx.appcompat.app.AppCompatActivity;import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;public class TestActivity_self extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_test_self);ImageView tips = (ImageView) findViewById(R.id.right_);TextView answer = (TextView) findViewById(R.id.choice_4); //正确答案answer.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){//点击正确答案answer.setBackgroundColor(0xff98e6b6);answer.setTextColor(android.graphics.Color.RED);tips.setVisibility(View.VISIBLE);sleepT(answer,tips);}});//错误答案TextView answer_w = (TextView) findViewById(R.id.choice_1);ImageView tip = (ImageView) findViewById(R.id.wrong_1);wro(answer_w,tip);answer_w = (TextView) findViewById(R.id.choice_2);tip = (ImageView) findViewById(R.id.wrong_2);wro(answer_w,tip);answer_w = (TextView) findViewById(R.id.choice_3);tip = (ImageView) findViewById(R.id.wrong_3);wro(answer_w,tip);answer_w = (TextView) findViewById(R.id.choice_5);tip = (ImageView) findViewById(R.id.wrong_5);wro(answer_w,tip);answer_w = (TextView) findViewById(R.id.choice_6);tip = (ImageView) findViewById(R.id.wrong_6);wro(answer_w,tip);}public void wro(TextView an_w,ImageView tip){an_w.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){an_w.setBackgroundColor(0xffef6c54);an_w.setTextColor(0xfffcc362);tip.setVisibility(View.VISIBLE);sleepT(an_w,tip);}});}public void sleepT(TextView an,ImageView tip){//页面静止Handler handler = new Handler();handler.postDelayed(new Runnable() {@Overridepublic void run() {tip.setVisibility(View.INVISIBLE);an.setBackgroundColor(0xffffe4c7);an.setTextColor(0xff000000);}}, 1000);//1秒后执行Runnable中的run方法}
}

本文链接:https://my.lmcjl.com/post/4234.html

展开阅读全文

4 评论

留下您的评论.