Android Studio onClick button to display ImageView in another activity

三世轮回 提交于 2019-12-11 05:56:21

问题


I have setup 2 activities - one and two .

For activity 1, I have a EditText and a button. When user open the app, it will show Activity One(just like the screenshot) to prompt user to key in a number. For example, 1 or 2.

What I am trying to do is that: I want to display a ImageView in activity 2 when user key in a number in EditTextfollow by a click on the button in activity 1.

  • If user key in 1, it will display 1.png in activity 2
  • If user key in 2, it will display 2.png in activity 2
  • If user key in 3, it will display 3.png in activity 2 etc...

the image will get from drawable folder.

Please refer to this screenshot

[![enter image description here][1]][1]

I can pass the integer value through Intent from activity 1 to 2 but I can't do it for ImageView. so that means the if else loop i have already done just that the ImageView cant display.

 get_image.setBackgroundResource(R.drawable.1); //  here i can only key in 01 ( it will get image from Drawable folder 1.png). i cant put int value into it. 

Or i shouldn't use get_image.setBackgroundResource?? Anyone can help? I stuck here for 1 day...

thanks in advance!

please check screenshot -> http://i.stack.imgur.com/53vjy.jpg


回答1:


You said that you can pass integer value from activity 1 to 2 so just use that to find your image.

ImageView imageView = (ImageView)findViewById(R.id.yourImageViewId);
if(1 == yourValue) { 
//set 01.png 
} else {...}

I may missing somethings because i can't understand when you said that "but i cant do it for imageview".

EDIT: after your addtional code. So you must map your integer value with your resource file name. You could not put your integer value to get_image.setBackgroundResource(R.drawable.id). I think in your situation you should use an array just store id of resource you need int[] drawableIds = {R.drawable.01, R.drawable.02} in your Activity2 and then use like this get_image.setBackgroundResource(drawableIds[yourIntegerValue-1]) (ofcourse you should take care array out of index when you use this method).




回答2:


Try below code for your solution,

For Activity One write below code to redirect on activity 2

     Intent intent = new Intent(Activity1.this, Activity2.class);
      intent.putExtra("SelectedNumber", editText.getEditableText.toString());
      startActivity(intent);

Now In Activity 2 write below code in onCreate method

     int selectedNumber = 1;
      if(getIntent().getExtras() != null)
      {
          selectedNumber = getIntent().getExtras().getInt("SelectedNumber");
      }

      switch(selectedNumber)
       {
            case 1:  // set your 01.png Image
                     break;
            case 2:  // set your 02.png Image
                     break;

            // And so
       }



回答3:


Try this way might helps you.

String mDrawableName = "1"; //editText.getText().toString();
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());

then your Activity2 use,

 setImageResource(resID); 
(or)
 setImageDrawable(getResources().getDrawable(resID)); 

Finally,

button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {

    String mDrawableName = editText.getText().toString();
    int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());

    Intent ii=new Intent(Activity.this, Activity1.class);
    ii.putExtra("resId", resID);
    startActivity(ii);
}
});

Activity2

public class Activity2 extends Activity { private ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.intent);
    img = (ImageView)findViewById(R.id.img);
    Intent iin= getIntent();
    Bundle b = iin.getExtras();

    if(b!=null)
    {
        int drawableId =(int) b.get("redId");
        img.setImageResource(drawableId); 
    }
  } 
}



回答4:


you can pass resource value in extras. try this way.

EditText edit = (EditText) findViewById(R.id.yourEdittext);
Button btn = (Button) findViewById(R.id.yourButton);

btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent i = new Intent(ActivityOne.this, ActivityTwo.class);

                if(edit.getText().toString().trim().equals("1")){
                    i.putExtra("image", R.drawable.01);
                    startActivity(i);
                }else if(edit.getText().toString().trim().equals("2")){
                    i.putExtra("image", R.drawable.02);
                    startActivity(i);
                }else{
                    Toast.makeText(getApplicationContext(), "Please Enter Valid Value.",
                    Toast.LENGTH_SHORT).show();
                }
            }
        });

and in your ActivityTwo.java

ImageView imageView = (ImageView)findViewById(R.id.yourImageViewId);
imageView.setImageResource(getIntent().getIntExtra("image",0));

Happy Coding.




回答5:


In activity 2's on create

get_image= (ImageView)findViewById(R.id.imageView);

then use switch case for values from intent then

  switch(intentValues){
    case 1:
     get_images.setImageDrawable(getResources().getDrawable(R.drawable.1));
    break;
    case 2:
    get_images.setImageDrawable(getResources().getDrawable(R.drawable.2));
    break;
    }


来源:https://stackoverflow.com/questions/38342718/android-studio-onclick-button-to-display-imageview-in-another-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!