[英]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 有很多解决方案来解决您的问题
there will be many ways to achieve what you want, use one of them as per your choice. 有很多方法可以实现您想要的,根据您的选择使用其中一种。
To store text color : 要存储文本颜色:
Use SharedPreference 使用SharedPreference
Use Room or SQLite Database 使用会议室或SQLite数据库
Save color in Singleton class 在Singleton类中保存颜色
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: 更改文本颜色:
Use Listener to change text color when you press button from 2nd activity 在第二个活动中按按钮时,使用侦听器更改文本颜色
Use EventBus if you already have implemented it in your project 如果您已经在项目中实现了EventBus,请使用它
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.