简体   繁体   English

如何通过在另一个活动中单击按钮来更改TextView的颜色?

[英]How to change the color of a TextView in one activity by clicking a button in another?

I have two activities, in activity(1) I have a TextView, in activity(2) I have a button. 我有两个活动,在活动(1)中有一个TextView,在活动(2)中有一个按钮。 I want to change the TextView color in activity(1) by clicking the button in activity(2) and save this color. 我想通过单击活动(2)中的按钮来更改活动(1)中的TextView颜色并保存该颜色。 How can I do this? 我怎样才能做到这一点?

You can save your color with Shared Preferences when your button is clicked and later get it : 您可以在单击按钮时使用“共享首选项”保存颜色,以后再获取它:

In activity 2 when your button is pressed : 在活动2中,按下按钮时

 PreferenceManager.getDefaultSharedPreferences(MainActivity.this)
        .edit()
        .putString(key, value).apply();

And in activity 1 get the value that you saved: 在活动1中,获取您保存的值:

PreferenceManager.getDefaultSharedPreferences(DriverScreen.this).getString(key, "default value")

You must use a database for keep the textview color and get the color from database. 您必须使用数据库来保持textview颜色并从数据库中获取颜色。 when click the button you can change it in the database. 单击按钮时,可以在数据库中进行更改。

There are many solutions to your problem 有很多解决方案来解决您的问题

  1. You can use sharepreference. 您可以使用sharepreference。
  2. You can use a singleton class. 您可以使用单例类。
  3. You can use a eventlistener. 您可以使用事件监听器。
  4. You can use intent.putExtra() for this purpose 您可以为此使用intent.putExtra()

there will be many ways to achieve what you want, use one of them as per your choice. 有很多方法可以实现您想要的,根据您的选择使用其中一种。

To store text color : 要存储文本颜色:

  1. Use SharedPreference 使用SharedPreference

  2. Use Room or SQLite Database 使用会议室或SQLite数据库

  3. Save color in Singleton class 在Singleton类中保存颜色

  4. or open second activity using startActivityForResult() if you're coming back to 1st activity after completion of 2nd activity work. 或在完成第二项活动后返回到第一项活动时,使用startActivityForResult()打开第二项活动。

To Change Text Color: 更改文本颜色:

  1. Use Listener to change text color when you press button from 2nd activity 在第二个活动中按按钮时,使用侦听器更改文本颜色

  2. Use EventBus if you already have implemented it in your project 如果您已经在项目中实现了EventBus,请使用它

  3. Using Local Broadcast Receiver (Send broadcast to 1st activity after click on button of 2nd activity) 使用本地广播接收器(单击第二活动的按钮后将广播发送到第一活动)

there is many ways to change text color from another activity. 有很多方法可以更改其他活动的文本颜色。

First 第一

pass color value using intent 使用意图传递颜色值

Code

Main2Activity.java

 button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int color=getResources().getColor(R.color.colorAccent);
            Intent intent=new Intent(Main2Activity.this,MainActivity.class);
            intent.putExtra("color",color);
            startActivity(intent);
        }
    });

MainActivity.java

  android_text=findViewById(R.id.android_text);
    color=getIntent().getIntExtra("color",0);
    android_text.setTextColor(color);

Second way 第二种方式

use static variable 使用静态变量

code

MainActivity.java

  static int color;
    android_text=findViewById(R.id.android_text);
    android_text.setTextColor(color);

Main2Activity.java

  button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           //set color
            MainActivity.color=getResources().getColor(R.color.colorAccent);
            Intent intent=new Intent(Main2Activity.this,MainActivity.class);
            startActivity(intent);
        }
    });

在此处输入图片说明

after update 更新后

在此处输入图片说明

I Hope its work for you 希望它对你有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过另一个活动更改一个活动的 TextView 字体大小? - How to change TextView fontSize of one activity through another activity? Android如何通过单击按钮更改Textview的数据 - Android How to change data of Textview by clicking on button 如何通过单击按钮更改textView中的文本大小 - how to change textsize in textView by clicking on button 在第二个活动中单击一个按钮时,如何使用微调器更改主要活动的背景颜色? - How can I change the background color of a Main Activity by using spinner while clicking a button in the Second Activity? 通过单击另一个按钮更改一个按钮的可见性 - Change visibility of one button by clicking another button 如何传递按钮意图在另一个活动中制作可绘制的变色? - how to pass button intent to make drawable change color in another activity? 如何通过 RecyclerView 中的删除按钮更改 Activity 中的 TextView? - How to change TextView in Activity by delete button in RecyclerView? 通过在另一个活动中单击按钮将项目添加到ListView中的一个片段中 - Add items to ListView in a fragment in one Activity by clicking a button in another activity 如何通过单击按钮在同一活动中更改布局? - How to change a layout in same activity by clicking a Button? 如何在android studio中点击按钮时从另一个活动更改textview文本? - How to change textview text from another activity on button click in android studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM