java如何在活动和其他活动之间传递视图的颜色? 1 周,2 日 Questions & Answers 1345 有没有办法在活动和其他活动之间传递视图或按钮的颜色 “将选择颜色的用户” 我试了很多次,每次运行它,我都会收到一条信息:“不幸的是,应用程序已经停止”!当我打开activity2
# 1 楼答案 根据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)); }
# 2 楼答案 这样做 获取所选颜色的id 将该颜色传递给activity2 从资源中加载该颜色 活动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); }
# 1 楼答案
根据Xoce的响应,如果没有将颜色定义为资源,或者只是知道它是十六进制代码,也可以这样做:
活动1
活动2
# 2 楼答案
这样做
活动1
活动2