问题
Hi i'm very new to android and tried to ask this question recently, but nobody understood what the hell i was talking about and looking at it myself i'm not surprised, (kinda made more sense while i was writing it) so I've changed a lot to hopefully make this much clearer, here goes. What i want to achieve is an image, view/button that changes its view and its function when clicked, For example; when the app starts you get 2 containers top and bottom, the top is empty and the bottom has 3 images, when you click on one of these 3 images in the bottom container the images all change and the clicked image goes into the top container.
it will do this two or three times populating different sets of images into the bottom container each time one is selected, whilst adding each selected image into the top container next to the last.
my code at the moment accomplishes the first part of this
3 images displayed at the bottom when my app starts the top is empty

when the first image is clicked it is put in the top and there are new images in the bottom

if the second image was clicked from the app starting that image would be in the top and the images below would be different again like this

and if the third image was clicked from the app starting that image would be in the top and the images below would be different again like this

Now my problem is
"it will do this two or three times populating different sets of images into the bottom container each time one is selected, whilst adding each selected image into the top container next to the last."
ive tried a few different approaches with regards to this without much joy but cant see a way that i can change the ids of the the images or there function (most probably due to a lack of experience) but i am now thinking that i may rewrite it again using a gridView and image arrays using getId and setId can anybody lend me there two cents on this maybe there is a better way, here is my code
package com.martinsapp.socialstories;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.os.Bundle;
import android.widget.ImageButton;
public class speakActivity extends Activity{
int speak = 1;
ImageButton btn1;
ImageButton btn2;
ImageButton btn3;
ImageButton btn4;
ImageButton btn5;
ImageButton btn6;
ImageButton btnmt1;
ImageButton btnmt2;
ImageButton btnmt3;
ImageButton btnmt4;
ImageButton btnmt5;
ImageButton btnmt6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speak);
btn1 = (ImageButton)findViewById(R.id.imageButton);
btn1.setOnClickListener(ButtonClickListener);
btn1.setBackgroundColor(View.INVISIBLE);
btn2 = (ImageButton)findViewById(R.id.imageButton2);
btn2.setOnClickListener(ButtonClickListener);
btn2.setBackgroundColor(View.INVISIBLE);
btn3 = (ImageButton)findViewById(R.id.imageButton3);
btn3.setOnClickListener(ButtonClickListener);
btn3.setBackgroundColor(View.INVISIBLE);
btn4 = (ImageButton)findViewById(R.id.imageButton4);
btn4.setOnClickListener(ButtonClickListener);
btn4.setBackgroundColor(View.INVISIBLE);
btn5 = (ImageButton)findViewById(R.id.imageButton5);
btn5.setOnClickListener(ButtonClickListener);
btn5.setBackgroundColor(View.INVISIBLE);
btn6 = (ImageButton)findViewById(R.id.imageButton6);
btn6.setOnClickListener(ButtonClickListener);
btn6.setBackgroundColor(View.INVISIBLE);
btnmt1 = (ImageButton)findViewById(R.id.imageButtonempty1);
btnmt1.setOnClickListener(ButtonClickListener);
btnmt1.setVisibility(View.INVISIBLE);
btnmt2 = (ImageButton)findViewById(R.id.imageButtonempty2);
btnmt2.setOnClickListener(ButtonClickListener);
btnmt2.setVisibility(View.INVISIBLE);
btnmt3 = (ImageButton)findViewById(R.id.imageButtonempty3);
btnmt3.setOnClickListener(ButtonClickListener);
btnmt3.setVisibility(View.INVISIBLE);
btnmt4 = (ImageButton)findViewById(R.id.imageButtonempty4);
btnmt4.setOnClickListener(ButtonClickListener);
btnmt4.setVisibility(View.INVISIBLE);
btnmt5 = (ImageButton)findViewById(R.id.imageButtonempty5);
btnmt5.setOnClickListener(ButtonClickListener);
btnmt5.setVisibility(View.INVISIBLE);
btnmt6 = (ImageButton)findViewById(R.id.imageButtonempty6);
btnmt6.setOnClickListener(ButtonClickListener);
btnmt6.setVisibility(View.INVISIBLE);
if (speak == 1){
btn1.setImageResource(R.drawable.i_want);
btn2.setImageResource(R.drawable.i_like);
btn3.setImageResource(R.drawable.i_feel);
btn4.setVisibility(View.INVISIBLE);
btn5.setVisibility(View.INVISIBLE);
btn6.setVisibility(View.INVISIBLE);
}
}
private View.OnClickListener ButtonClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.imageButton:
speak = 2;
if (speak == 2){
btn1.setImageResource(R.drawable.to_play);
btn2.setImageResource(R.drawable.to_eat);
btn3.setImageResource(R.drawable.a_drink);
btn4.setImageResource(R.drawable.a_hug);
btn4.setVisibility(View.VISIBLE);
btn5.setVisibility(View.INVISIBLE);
btn6.setVisibility(View.INVISIBLE);
btnmt1.setBackgroundResource(R.drawable.i_want);
btnmt1.setVisibility(View.VISIBLE);
}
break;
case R.id.imageButton2:
speak = 3;
if (speak == 3){
btn1.setImageResource(R.drawable.friends);
btn2.setImageResource(R.drawable.music);
btn3.setImageResource(R.drawable.sports);
btn4.setImageResource(R.drawable.emptypuzzlepiece);
btn5.setImageResource(R.drawable.emptypuzzlepiece);
btn3.setVisibility(View.VISIBLE);
btn4.setVisibility(View.INVISIBLE);
btn5.setVisibility(View.INVISIBLE);
btn6.setVisibility(View.INVISIBLE);
btnmt1.setBackgroundResource(R.drawable.i_like);
btnmt1.setVisibility(View.VISIBLE);
}
break;
case R.id.imageButton3:
speak = 4;
if (speak == 4){
btn1.setImageResource(R.drawable.sad);
btn2.setImageResource(R.drawable.happy);
btn3.setImageResource(R.drawable.sick);
btn4.setVisibility(View.INVISIBLE);
btn5.setVisibility(View.INVISIBLE);
btn6.setVisibility(View.INVISIBLE);
btnmt1.setBackgroundResource(R.drawable.i_feel);
btnmt1.setVisibility(View.VISIBLE);
}
}
}
};
}
回答1:
So you want to change the drawable Resource R.drawable.i_feel
to be a different image?
I'm afraid, that's not possible. Android gives every image in the drawables
folder a unique Id.
If there should be specific sets of images been shown in the bottom container, you'll need to create an array for every set.
I'd recommend you to change the OnClickListener a bit. You can change it into this:
private View.OnClickListener ButtonClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageButton clickedButton = (ImageButton) v;
Drawable latestDrawable = clickedButton.getDrawable();
switch (v.getId()) {
case R.id.imageButton:
speak = 2;
if (speak == 2) {
btn1.setImageResource(R.drawable.to_play);
btn2.setImageResource(R.drawable.to_eat);
btn3.setImageResource(R.drawable.a_drink);
btn4.setImageResource(R.drawable.a_hug);
setImageButtonVisibilities();
btnmt1.setImageDrawable(latestDrawable);
btnmt1.setVisibility(View.VISIBLE);
}
break;
case R.id.imageButton2:
speak = 3;
if (speak == 3) {
btn1.setImageResource(R.drawable.friends);
btn2.setImageResource(R.drawable.music);
btn3.setImageResource(R.drawable.sports);
btn4.setImageResource(R.drawable.emptypuzzlepiece);
btn5.setImageResource(R.drawable.emptypuzzlepiece);
setImageButtonVisibilities();
btnmt1.setImageDrawable(latestDrawable);
btnmt1.setVisibility(View.VISIBLE);
}
break;
case R.id.imageButton3:
speak = 4;
if (speak == 4) {
btn1.setImageResource(R.drawable.sad);
btn2.setImageResource(R.drawable.happy);
btn3.setImageResource(R.drawable.sick);
setImageButtonVisibilities();
btnmt1.setImageDrawable(latestDrawable);
btnmt1.setVisibility(View.VISIBLE);
}
}
}
private setImageButtonVisibilities() {
if(btn1.getDrawable == null)
btn1.setVisibility(View.INVISIBLE);
else
btn1.setVisibility(View.VISIBLE);
if(btn2.getDrawable == null)
btn2.setVisibility(View.INVISIBLE);
else
btn2.setVisibility(View.VISIBLE);
if(btn3.getDrawable == null)
btn3.setVisibility(View.INVISIBLE);
else
btn3.setVisibility(View.VISIBLE);
if(btn4.getDrawable == null)
btn4.setVisibility(View.INVISIBLE);
else
btn4.setVisibility(View.VISIBLE);
if(btn5.getDrawable == null)
btn5.setVisibility(View.INVISIBLE);
else
btn5.setVisibility(View.VISIBLE);
if(btn6.getDrawable == null)
btn6.setVisibility(View.INVISIBLE);
else
btn6.setVisibility(View.VISIBLE);
}
}
This will always put the Drawable
of the pressed ImageButton
as Drawable for the ImageButton in the top container.
The new method is just helping with the Visibility changes you do in any case
statement.
You'll need to call it everytime you changed the Drawables. you cn copy and paste it in in the class as well...
I hope this helps you a bit.
来源:https://stackoverflow.com/questions/21555311/how-can-i-handle-images-and-change-there-function