Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在活动和其他活动之间传递视图的颜色?_Java_Android_View_Colors - Fatal编程技术网

Java 如何在活动和其他活动之间传递视图的颜色?

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

有没有办法在活动和其他活动之间传递视图或按钮的颜色

“将选择颜色的用户”

我试了很多次,每次运行它,我都会收到一条信息:“不幸的是,应用程序已经停止”!当我打开activity2时,请执行此操作

  • 获取所选颜色的id
  • 将该颜色传递给activity2
  • 从资源中加载该颜色
  • 活动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));
    }
    

    根据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堆栈跟踪。如果您需要现有方法的帮助,请编辑您的问题,使其有一个完整的答案,并更详细地解释“视图颜色”的含义。