Java 如何在活动和其他活动之间传递视图的颜色?
有没有办法在活动和其他活动之间传递视图或按钮的颜色 “将选择颜色的用户” 我试了很多次,每次运行它,我都会收到一条信息:“不幸的是,应用程序已经停止”!当我打开activity2时,请执行此操作Java 如何在活动和其他活动之间传递视图的颜色?,java,android,view,colors,Java,Android,View,Colors,有没有办法在活动和其他活动之间传递视图或按钮的颜色 “将选择颜色的用户” 我试了很多次,每次运行它,我都会收到一条信息:“不幸的是,应用程序已经停止”!当我打开activity2时,请执行此操作 获取所选颜色的id 将该颜色传递给activity2 从资源中加载该颜色 活动1 Intent pass = new Intent( ); Bundle extras = new Bundle(); extras.putInt("colorResourceName", colorResourceN
Intent pass = new Intent( );
Bundle extras = new Bundle();
extras.putInt("colorResourceName", colorResourceName);
pass.putExtras(extras);
startActivity(pass);
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();
int colorResourceName = data.getIntExtra("colorResourceName", -1);
}
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));
}
根据Xoce的响应,如果没有将颜色定义为资源,或者只是知道它是十六进制代码,也可以这样做: 活动1
Intent pass = new Intent( );
Bundle extras = new Bundle();
extras.putInt("colorResourceName", colorResourceName);
pass.putExtras(extras);
startActivity(pass);
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();
int colorResourceName = data.getIntExtra("colorResourceName", -1);
}
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));
}
检查与崩溃相关的Java堆栈跟踪。如果您需要现有方法的帮助,请编辑您的问题,使其有一个完整的答案,并更详细地解释“视图颜色”的含义。