将活动一的int值发送到活动二

时间:2017-06-19 19:24:22

标签: android android-intent bundler

您好我尝试使用此代码将int值从活动1发送到活动二

@Override
        public void onClick(View v) {
            new ChromaDialog.Builder()
                    .initialColor(getResources().getColor(R.color.colorAccent))
                    .colorMode(ColorMode.ARGB)
                    .indicatorMode(IndicatorMode.HEX)
                    .onColorSelected(new OnColorSelectedListener() {
                        @Override
                        public void onColorSelected(@ColorInt int color) {
                            Intent intent = new Intent(MainActivity.this, Hackpage.class);
                            intent.putExtra("intVariableName", color);
                            Toast.makeText(MainActivity.this,"color :"+Integer.toHexString(color),Toast.LENGTH_LONG).show();
                        }
                    })
                    .create()
                    .show(getSupportFragmentManager(), "ChromaDialog");


        }
    });
}

但我的价值问题我得到0

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_hackpage);
    Texthack = (TextView)findViewById(R.id.hacktext);
    Intent mIntent = getIntent();
    int intValue = mIntent.getIntExtra("intVariableName", 0);
    Texthack.setBackgroundColor(Color.parseColor("#"+Integer.toHexString(intValue )));

2 个答案:

答案 0 :(得分:0)

您的密钥似乎正在获取默认值 检查你的颜色变量是否为int,可能很长。 如果它很长,你可以通过getIntent().getLongExtra(your_key, default_value)

获得它

答案 1 :(得分:0)

//global:
static final String SOME_ACTION = "com.example.test.myapplicationtest.SOME_ACTION";
IntentFilter intentFilter = new IntentFilter(SOME_ACTION); //youre project package name. example "com.example.yourprojectpackagename.SOME_ACTION".



       @Override
    public void onClick(View v) {
        new ChromaDialog.Builder()
                .initialColor(getResources().getColor(R.color.colorAccent))
                .colorMode(ColorMode.ARGB)
                .indicatorMode(IndicatorMode.HEX)
                .onColorSelected(new OnColorSelectedListener() {
                    @Override
                    public void onColorSelected(@ColorInt int color) {
            registerReceiver(mReceiver, intentFilter);
            Intent intent = new Intent(SOME_ACTION);
            intent.putExtra("intVariableName", color);
            sendBroadcast(intent);
            Toast.makeText(MainActivity.this,"color :"+Integer.toHexString(color),Toast.LENGTH_LONG).show();
                    }
                })
                .create()
                .show(getSupportFragmentManager(), "ChromaDialog");
    }
});

}

     ///
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
       int value =  intent.getIntExtra("intVariableName",0);
        Toast.makeText(getApplicationContext(),""+value,Toast.LENGTH_LONG).show();
    }
};
它的效果很好。 将值发送给现有的Activiy。 (例如.MainActivity.java)。 所以你不需要使用Hackpage.class。