onClick在MainActivity中更改imageButton

时间:2015-03-17 09:53:21

标签: java android

当我点击Click按钮更改MainActivity.class中的imageButton

这是具有onClick按钮的活动:

public void onClick8 (View view) {
    //here please code for change imagebutton from onClick
    Intent myIntent = new Intent(view.getContext(), MainActivity.class);
    startActivity(myIntent);
    finish();
    MediaPlayer mediaPlayer = MediaPlayer.create(zem13.this, R.raw.melody);
    mediaPlayer.start();

}

这是带有imageButton的MainActivity中的代码:

ImageButton imageButton = (ImageButton)findViewById(R.id.imageButton2);
imageButton.setImageResource(R.drawable.psik);

2 个答案:

答案 0 :(得分:0)

您需要在视图中添加onClickListener,以触发onClick。然后,您需要覆盖完成操作的onClick以更改imageButton

看看这个:

        Button btn=new Button(this);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //to change you imageButton here.
            imageButton.setBackgroundResource(0);
        }
    });

答案 1 :(得分:0)

如果我理解得很好,你应该传递一些指标来将图像资源更改为MainActivity并将其设置在onCreate()中。尝试更改您的代码:

    public void onClick8 (View view) {
        //here please code for change imagebutton from onClick
        Intent myIntent = new Intent(view.getContext(), MainActivity.class);

        myIntent.putExtra("shouldChangeButton", true);

        startActivity(myIntent);
        finish();
        MediaPlayer mediaPlayer = MediaPlayer.create(zem13.this, R.raw.melody);
        mediaPlayer.start();
}

然后在MainActivity中:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        boolean defaultValue = false; //or true as you need
        if(getIntent().getBooleanExtra("shouldChangeButton", defaultValue)) {
            ImageButton ib = (ImageButton) findViewById(R.id.imagebutton);
            ib.setImageResource(R.drawable.img);
        }
    }

或者如果您想要永久性更改,您应该将其保留在SharedPreferences中,而不是通过意图传递它:

public void onClick8 (View view) {
        //here please code for change imagebutton from onClick
        Intent myIntent = new Intent(view.getContext(), MainActivity.class);

        SharedPreferences prefs = getSharedPreferences("YourAppNamePrefs", MODE_PRIVATE);
        prefs.edit().putBoolean("shouldChangeButton", true).apply();

        startActivity(myIntent);
        finish();
        MediaPlayer mediaPlayer = MediaPlayer.create(zem13.this, R.raw.melody);
        mediaPlayer.start();
}

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        boolean defaultValue = false; //or true as you need

        SharedPreferences prefs = getSharedPreferences("YourAppNamePrefs", MODE_PRIVATE);
        boolean shouldChangeButton = prefs.getBoolean("shouldChangeButton", defaultValue);

        if(shouldChangeButton) {
            ImageButton ib = (ImageButton) findViewById(R.id.imagebutton);
            ib.setImageResource(R.drawable.img);
        }
    }