【问题标题】:How I can pass color of view between activity and another?我如何在活动和另一个活动之间传递视图颜色?
【发布时间】:2016-02-27 15:34:27
【问题描述】:

有没有办法在活动和另一个活动之间传递视图或按钮的颜色?

"选择颜色的用户"

我尝试了很多,每次运行它,我都会收到消息:“不幸的是应用程序已停止”!当我打开活动2

【问题讨论】:

  • Use LogCat 检查与您的崩溃相关的 Java 堆栈跟踪。如果您需要有关现有方法的帮助,请编辑您的问题以提供minimal reproducible example,并更详细地解释“视图颜色”的含义。

标签: java android view colors


【解决方案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】:

    这样做....

    1. 获取所选颜色的ID
    2. 将该颜色传递给activity2
    3. 从资源中加载该颜色

    活动 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);
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-08-14
      • 2012-07-16
      • 2014-04-03
      • 1970-01-01
      • 1970-01-01
      • 2012-01-24
      • 2015-12-31
      • 2012-03-05
      • 1970-01-01
      相关资源
      最近更新 更多