【发布时间】:2016-02-27 15:34:27
【问题描述】:
有没有办法在活动和另一个活动之间传递视图或按钮的颜色?
"选择颜色的用户"
我尝试了很多,每次运行它,我都会收到消息:“不幸的是应用程序已停止”!当我打开活动2
【问题讨论】:
-
Use LogCat 检查与您的崩溃相关的 Java 堆栈跟踪。如果您需要有关现有方法的帮助,请编辑您的问题以提供minimal reproducible example,并更详细地解释“视图颜色”的含义。
有没有办法在活动和另一个活动之间传递视图或按钮的颜色?
"选择颜色的用户"
我尝试了很多,每次运行它,我都会收到消息:“不幸的是应用程序已停止”!当我打开活动2
【问题讨论】:
根据 Xoce 的响应,如果您没有将颜色定义为资源或者只是知道它是十六进制代码,您也可以这样做:
活动 1
Intent pass = new Intent( );
Bundle extras = new Bundle();
extras.putInt("colorHexCode", colorHexCode); //Example of color code: "#FFFFFF"
pass.putExtras(extras);
startActivity(pass);
活动 2
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle data = getIntent().getExtras();
String colorHexCode = data.getStringExtra("colorHexCode");
TextView textView = (TextView) findViewById(R.id.my_text_view);
textView.setTextColor(Color.parseColor(colorHexCode));
}
【讨论】:
这样做....
活动 1
Intent pass = new Intent( );
Bundle extras = new Bundle();
extras.putInt("colorResourceName", colorResourceName);
pass.putExtras(extras);
startActivity(pass);
活动 2
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle data = getIntent().getExtras();
int colorResourceName = data.getIntExtra("colorResourceName", -1);
}
【讨论】: