Android Studio onClick 按钮在另一个中显示 ImageView activity
Android Studio onClick button to display ImageView in another activity
我设置了 2 个活动 - 一和二。
对于activity 1,我有一个EditText
和一个按钮。当用户打开应用程序时,它会显示 Activity 一个(就像屏幕截图一样)提示用户输入一个数字。例如,1 或 2。
我想做的是:我想在 activity 2 中显示一个 ImageView
当用户在 EditText
中输入一个数字然后点击按钮在 activity 1.
- 如果用户输入1,则在activity中显示
1.png
2
- 如果用户输入2,会在activity中显示
2.png
2
- 如果用户输入3,则在activity2中显示
3.png
等...
图像将从 drawable
文件夹中获取。
请参考这个截图
[![在此处输入图片描述][1]][1]
我可以通过 Intent
从 activity 1 到 2 传递整数值,但我不能为 ImageView 这样做。所以这意味着我已经完成了 if else 循环,只是 ImageView
无法显示。
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.
或者我不应该使用 get_image.setBackgroundResource??任何人都可以帮忙吗?我在这里呆了 1 天...
提前致谢!
请查看截图 -> http://i.stack.imgur.com/53vjy.jpg
您说您可以将整数值从 activity 1 传递到 2,所以只需使用它来查找您的图像。
ImageView imageView = (ImageView)findViewById(R.id.yourImageViewId);
if(1 == yourValue) {
//set 01.png
} else {...}
我可能遗漏了一些东西,因为我听不懂你说的 "but i cant do it for imageview"。
编辑:在您的附加代码之后。
因此,您必须将整数值映射到资源文件名。您不能将整数值设为 get_image.setBackgroundResource(R.drawable.id)。
我认为在你的情况下你应该使用一个数组来存储你需要的资源 id int[] drawableIds = {R.drawable.01, R.drawable.02}
在你的 Activity2
然后像这样使用 get_image.setBackgroundResource(drawableIds[yourIntegerValue-1])
(当然,当你使用这种方法时,你应该注意索引中的数组)。
尝试使用以下代码来解决问题,
对于 Activity 一个写下面的代码重定向到 activity 2
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("SelectedNumber", editText.getEditableText.toString());
startActivity(intent);
Now In Activity 2 在onCreate方法中写下代码
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
}
试试这个方法可能对你有帮助。
String mDrawableName = "1"; //editText.getText().toString();
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
然后你的 Activity2 使用,
setImageResource(resID);
(or)
setImageDrawable(getResources().getDrawable(resID));
最后,
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 扩展 Activity
{
私人 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);
}
}
}
您可以在 extras 中传递资源值。试试这个方法。
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();
}
}
});
在你的ActivityTwo.java
ImageView imageView = (ImageView)findViewById(R.id.yourImageViewId);
imageView.setImageResource(getIntent().getIntExtra("image",0));
编码愉快。
在 activity 2's on create
get_image= (ImageView)findViewById(R.id.imageView);
然后对 intent 中的值使用 switch case 然后
switch(intentValues){
case 1:
get_images.setImageDrawable(getResources().getDrawable(R.drawable.1));
break;
case 2:
get_images.setImageDrawable(getResources().getDrawable(R.drawable.2));
break;
}
我设置了 2 个活动 - 一和二。
对于activity 1,我有一个EditText
和一个按钮。当用户打开应用程序时,它会显示 Activity 一个(就像屏幕截图一样)提示用户输入一个数字。例如,1 或 2。
我想做的是:我想在 activity 2 中显示一个 ImageView
当用户在 EditText
中输入一个数字然后点击按钮在 activity 1.
- 如果用户输入1,则在activity中显示
1.png
2 - 如果用户输入2,会在activity中显示
2.png
2 - 如果用户输入3,则在activity2中显示
3.png
等...
图像将从 drawable
文件夹中获取。
请参考这个截图
[![在此处输入图片描述][1]][1]
我可以通过 Intent
从 activity 1 到 2 传递整数值,但我不能为 ImageView 这样做。所以这意味着我已经完成了 if else 循环,只是 ImageView
无法显示。
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.
或者我不应该使用 get_image.setBackgroundResource??任何人都可以帮忙吗?我在这里呆了 1 天...
提前致谢!
请查看截图 -> http://i.stack.imgur.com/53vjy.jpg
您说您可以将整数值从 activity 1 传递到 2,所以只需使用它来查找您的图像。
ImageView imageView = (ImageView)findViewById(R.id.yourImageViewId);
if(1 == yourValue) {
//set 01.png
} else {...}
我可能遗漏了一些东西,因为我听不懂你说的 "but i cant do it for imageview"。
编辑:在您的附加代码之后。
因此,您必须将整数值映射到资源文件名。您不能将整数值设为 get_image.setBackgroundResource(R.drawable.id)。
我认为在你的情况下你应该使用一个数组来存储你需要的资源 id int[] drawableIds = {R.drawable.01, R.drawable.02}
在你的 Activity2
然后像这样使用 get_image.setBackgroundResource(drawableIds[yourIntegerValue-1])
(当然,当你使用这种方法时,你应该注意索引中的数组)。
尝试使用以下代码来解决问题,
对于 Activity 一个写下面的代码重定向到 activity 2
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("SelectedNumber", editText.getEditableText.toString());
startActivity(intent);
Now In Activity 2 在onCreate方法中写下代码
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
}
试试这个方法可能对你有帮助。
String mDrawableName = "1"; //editText.getText().toString();
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
然后你的 Activity2 使用,
setImageResource(resID);
(or)
setImageDrawable(getResources().getDrawable(resID));
最后,
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 扩展 Activity { 私人 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);
}
}
}
您可以在 extras 中传递资源值。试试这个方法。
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();
}
}
});
在你的ActivityTwo.java
ImageView imageView = (ImageView)findViewById(R.id.yourImageViewId);
imageView.setImageResource(getIntent().getIntExtra("image",0));
编码愉快。
在 activity 2's on create
get_image= (ImageView)findViewById(R.id.imageView);
然后对 intent 中的值使用 switch case 然后
switch(intentValues){
case 1:
get_images.setImageDrawable(getResources().getDrawable(R.drawable.1));
break;
case 2:
get_images.setImageDrawable(getResources().getDrawable(R.drawable.2));
break;
}