Android 其他类单击ImageView
我有个问题。所以我有几个活动,在其中一个活动中,我有6个ImageView,我为每个人设置了一个来自xml的“onClick”。我想知道如何检查图像是否被点击,然后显示在另一个活动中。这是我写的,它还没有完成,我做得好吗?如果是,您能帮助我将这些图像“导入”到最终活动中吗Android 其他类单击ImageView,android,Android,我有个问题。所以我有几个活动,在其中一个活动中,我有6个ImageView,我为每个人设置了一个来自xml的“onClick”。我想知道如何检查图像是否被点击,然后显示在另一个活动中。这是我写的,它还没有完成,我做得好吗?如果是,您能帮助我将这些图像“导入”到最终活动中吗 public ImageView imageSelected; private Intent i = new Intent(this, MemeSecondStep.class); public void onFirstPi
public ImageView imageSelected;
private Intent i = new Intent(this, MemeSecondStep.class);
public void onFirstPicClick(View view) {
startActivity(i);
imageSelected = (ImageView) findViewById(R.id.goitiPunu);
}
public void onSecondPicClick(View view) {
startActivity(i);
imageSelected = (ImageView) findViewById(R.id.goitiPdoi);
}
public void onThirdPicClick(View view) {
startActivity(i);
imageSelected = (ImageView) findViewById(R.id.goitiPtrei);
}
public void onFourthPicClick(View view) {
startActivity(i);
imageSelected = (ImageView) findViewById(R.id.goitiPpatru);
}
public void onFifthPicClick(View view) {
startActivity(i);
imageSelected = (ImageView) findViewById(R.id.goitiPcinci);
}
public void onSixthPicClick(View view) {
startActivity(i);
imageSelected = (ImageView) findViewById(R.id.goitiPsase);
}
您可以将单击的图像放入意图中,并将其发送到其他类似的活动中
public ImageView imageSelected;
private Intent i = new Intent(this, MemeSecondStep.class);
public void onFirstPicClick(View view) {
i.putExtra("firstImageKey","firstImage");
startActivity(i);
imageSelected = (ImageView) findViewById(R.id.goitiPunu);
}
在您的第二个活动中,必须有一个ImageView,当您从intent中获取哪个图像时,单击“设置图像源”以获得所需的图像
有了这句话,你可以从意图中获得额外的信息,并设置渴望的形象
Intent intent = getIntent();
String clickedImageName = intent.getStringExtra("firstImageKey");
if(clickedImageName.equals("firstImage"){
//set desire image
}
你的案子有很多解决办法,但这是我想到的第一个
更新
下面是可以解决您问题的第二个解决方案:
首先,在包含六个ImageView的xml中,必须像这样为每个ImageView设置标记
public ImageView imageSelected;
private Intent i = new Intent(this, MemeSecondStep.class);
public void onFirstPicClick(View view) {
i.putExtra("firstImageKey","firstImage");
startActivity(i);
imageSelected = (ImageView) findViewById(R.id.goitiPunu);
}
first_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:tag="image1"/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:src="@drawable/ic_launcher"
android:tag="image2"/>
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView2"
android:src="@drawable/ic_launcher"
android:tag="image3"/>
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView3"
android:src="@drawable/ic_launcher"
android:tag="image4"/>
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView4"
android:src="@drawable/ic_launcher"
android:tag="image5"/>
<ImageView
android:id="@+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView5"
android:src="@drawable/ic_launcher"
android:tag="image6"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
</RelativeLayout>
然后,当您成功创建意图时,请在SecondActivity中从中获取额外信息,并根据您从第一个活动中单击的图像将desire image source设置为SecondActivity中的imageView,如下所示
public ImageView imageSelected;
private Intent i = new Intent(this, MemeSecondStep.class);
public void onFirstPicClick(View view) {
i.putExtra("firstImageKey","firstImage");
startActivity(i);
imageSelected = (ImageView) findViewById(R.id.goitiPunu);
}
SecondActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView image1;
private ImageView image2;
private ImageView image3;
private ImageView image4;
private ImageView image5;
private ImageView image6;
private String yourImageTag="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeLayoutElements();
image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
image4.setOnClickListener(this);
image5.setOnClickListener(this);
image6.setOnClickListener(this);
}
private void initializeLayoutElements(){
image1= (ImageView)findViewById(R.id.imageView1);
image2= (ImageView)findViewById(R.id.imageView2);
image3= (ImageView)findViewById(R.id.imageView3);
image4= (ImageView)findViewById(R.id.imageView4);
image5= (ImageView)findViewById(R.id.imageView5);
image6= (ImageView)findViewById(R.id.imageView6);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
yourImageTag = v.getTag().toString();
intent.putExtra("imageTag",yourImageTag);
startActivity(intent);
}
}
public class SecondActivity extends AppCompatActivity {
private ImageView yourNewImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initializeLayoutElements();
getDataFromIntent();
}
private void initializeLayoutElements(){
yourNewImage= (ImageView)findViewById(R.id.imageView1);
}
private void getDataFromIntent(){
Intent intent = getIntent();
String yourImageTag = intent.getStringExtra("imageTag");
switch (yourImageTag){
case "image1":
Toast.makeText(SecondActivity.this, "image1", Toast.LENGTH_SHORT).show();
//Here you can set your desire recourse for yourNewImage
break;
case "image2":
Toast.makeText(SecondActivity.this, "image2", Toast.LENGTH_SHORT).show();
//Here you can set your desire recourse for yourNewImage
break;
case "image3":
Toast.makeText(SecondActivity.this, "image3", Toast.LENGTH_SHORT).show();
//Here you can set your desire recourse for yourNewImage
break;
case "image4":
Toast.makeText(SecondActivity.this, "image4", Toast.LENGTH_SHORT).show();
//Here you can set your desire recourse for yourNewImage
break;
case "image5":
Toast.makeText(SecondActivity.this, "image5", Toast.LENGTH_SHORT).show();
//Here you can set your desire recourse for yourNewImage
break;
case "image6":
Toast.makeText(SecondActivity.this, "image6", Toast.LENGTH_SHORT).show();
//Here you can set your desire recourse for yourNewImage
break;
}
}
}
第二个活动的xml如下所示
public ImageView imageSelected;
private Intent i = new Intent(this, MemeSecondStep.class);
public void onFirstPicClick(View view) {
i.putExtra("firstImageKey","firstImage");
startActivity(i);
imageSelected = (ImageView) findViewById(R.id.goitiPunu);
}
活动\u second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:tag="image1"/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:src="@drawable/ic_launcher"
android:tag="image2"/>
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView2"
android:src="@drawable/ic_launcher"
android:tag="image3"/>
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView3"
android:src="@drawable/ic_launcher"
android:tag="image4"/>
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView4"
android:src="@drawable/ic_launcher"
android:tag="image5"/>
<ImageView
android:id="@+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView5"
android:src="@drawable/ic_launcher"
android:tag="image6"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
</RelativeLayout>
但这只是针对您的测试用例,您必须更改它。希望此更新对您有所帮助 您可以将单击的图像放入intent中,并将其发送到其他类似的活动中
public ImageView imageSelected;
private Intent i = new Intent(this, MemeSecondStep.class);
public void onFirstPicClick(View view) {
i.putExtra("firstImageKey","firstImage");
startActivity(i);
imageSelected = (ImageView) findViewById(R.id.goitiPunu);
}
在您的第二个活动中,必须有一个ImageView,当您从intent中获取哪个图像时,单击“设置图像源”以获得所需的图像
有了这句话,你可以从意图中获得额外的信息,并设置渴望的形象
Intent intent = getIntent();
String clickedImageName = intent.getStringExtra("firstImageKey");
if(clickedImageName.equals("firstImage"){
//set desire image
}
你的案子有很多解决办法,但这是我想到的第一个
更新
下面是可以解决您问题的第二个解决方案:
首先,在包含六个ImageView的xml中,必须像这样为每个ImageView设置标记
public ImageView imageSelected;
private Intent i = new Intent(this, MemeSecondStep.class);
public void onFirstPicClick(View view) {
i.putExtra("firstImageKey","firstImage");
startActivity(i);
imageSelected = (ImageView) findViewById(R.id.goitiPunu);
}
first_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:tag="image1"/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:src="@drawable/ic_launcher"
android:tag="image2"/>
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView2"
android:src="@drawable/ic_launcher"
android:tag="image3"/>
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView3"
android:src="@drawable/ic_launcher"
android:tag="image4"/>
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView4"
android:src="@drawable/ic_launcher"
android:tag="image5"/>
<ImageView
android:id="@+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView5"
android:src="@drawable/ic_launcher"
android:tag="image6"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
</RelativeLayout>
然后,当您成功创建意图时,请在SecondActivity中从中获取额外信息,并根据您从第一个活动中单击的图像将desire image source设置为SecondActivity中的imageView,如下所示
public ImageView imageSelected;
private Intent i = new Intent(this, MemeSecondStep.class);
public void onFirstPicClick(View view) {
i.putExtra("firstImageKey","firstImage");
startActivity(i);
imageSelected = (ImageView) findViewById(R.id.goitiPunu);
}
SecondActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView image1;
private ImageView image2;
private ImageView image3;
private ImageView image4;
private ImageView image5;
private ImageView image6;
private String yourImageTag="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeLayoutElements();
image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
image4.setOnClickListener(this);
image5.setOnClickListener(this);
image6.setOnClickListener(this);
}
private void initializeLayoutElements(){
image1= (ImageView)findViewById(R.id.imageView1);
image2= (ImageView)findViewById(R.id.imageView2);
image3= (ImageView)findViewById(R.id.imageView3);
image4= (ImageView)findViewById(R.id.imageView4);
image5= (ImageView)findViewById(R.id.imageView5);
image6= (ImageView)findViewById(R.id.imageView6);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
yourImageTag = v.getTag().toString();
intent.putExtra("imageTag",yourImageTag);
startActivity(intent);
}
}
public class SecondActivity extends AppCompatActivity {
private ImageView yourNewImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initializeLayoutElements();
getDataFromIntent();
}
private void initializeLayoutElements(){
yourNewImage= (ImageView)findViewById(R.id.imageView1);
}
private void getDataFromIntent(){
Intent intent = getIntent();
String yourImageTag = intent.getStringExtra("imageTag");
switch (yourImageTag){
case "image1":
Toast.makeText(SecondActivity.this, "image1", Toast.LENGTH_SHORT).show();
//Here you can set your desire recourse for yourNewImage
break;
case "image2":
Toast.makeText(SecondActivity.this, "image2", Toast.LENGTH_SHORT).show();
//Here you can set your desire recourse for yourNewImage
break;
case "image3":
Toast.makeText(SecondActivity.this, "image3", Toast.LENGTH_SHORT).show();
//Here you can set your desire recourse for yourNewImage
break;
case "image4":
Toast.makeText(SecondActivity.this, "image4", Toast.LENGTH_SHORT).show();
//Here you can set your desire recourse for yourNewImage
break;
case "image5":
Toast.makeText(SecondActivity.this, "image5", Toast.LENGTH_SHORT).show();
//Here you can set your desire recourse for yourNewImage
break;
case "image6":
Toast.makeText(SecondActivity.this, "image6", Toast.LENGTH_SHORT).show();
//Here you can set your desire recourse for yourNewImage
break;
}
}
}
第二个活动的xml如下所示
public ImageView imageSelected;
private Intent i = new Intent(this, MemeSecondStep.class);
public void onFirstPicClick(View view) {
i.putExtra("firstImageKey","firstImage");
startActivity(i);
imageSelected = (ImageView) findViewById(R.id.goitiPunu);
}
活动\u second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:tag="image1"/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:src="@drawable/ic_launcher"
android:tag="image2"/>
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView2"
android:src="@drawable/ic_launcher"
android:tag="image3"/>
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView3"
android:src="@drawable/ic_launcher"
android:tag="image4"/>
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView4"
android:src="@drawable/ic_launcher"
android:tag="image5"/>
<ImageView
android:id="@+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView5"
android:src="@drawable/ic_launcher"
android:tag="image6"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
</RelativeLayout>
但这只是针对您的测试用例,您必须更改它。希望此更新对您有所帮助
请上传完整的照片
请在xml中使用click=“onImageClick”
MemeSecondStep.java
private void onCreate(Some params){
int resid = getIntent().getIntegerExtra("resid",0);
imageView.setImageResource(resid).
}
请上传完整的照片
请在xml中使用click=“onImageClick”
MemeSecondStep.java
private void onCreate(Some params){
int resid = getIntent().getIntegerExtra("resid",0);
imageView.setImageResource(resid).
}
在下一个活动的布局中需要一个ImageView,并将源设置为相同。您不能简单地“移动”ImageView—您需要在下一个活动的布局中使用ImageView,并将源设置为相同。你不能简单地“移动”图像视图嘿,我有一个问题,如果我用R.id.goitiPunu更改“firstImage”是否可以?不,我认为这不是问题,但如果你这样做,你必须在第二个活动中检查R.id.gotiPunu是否相等尝试一下,并让我知道发生了什么好吗?你确定这是第二个活动的意图。getExtra?如果我把它放在红色的话,我会得到getExtra。是的,如果你把字符串放在第一个活动中,你必须得到getStringExtra()。。如果你用getInt()输入int必须得到嘿,我有一个问题,如果我用R.id.goitiPunu更改“firstImage”可以吗?不,我认为这不是问题,但如果你这样做,你必须在第二个活动中检查R.id.gotiPunu是否相等尝试一下,让我知道发生了什么好吗?你确定这是第二个活动的intent.getExtra吗?如果我把它放在红色的话,我会得到getExtra。是的,如果你把字符串放在第一个活动中,你必须得到getStringExtra()。。如果你用getInt()Dude输入int,我的应用程序在我执行你的解决方案时崩溃了。这就是我得到的错误:我已经发布了一个图片1评论。图片没有完成,裁剪的区域很重要。如果可能的话,请提供错误日志。伙计,我的应用程序在我执行你的解决方案时崩溃了。这是我得到的错误:我已经发布了一个图片1评论。图片没有完成,裁剪的区域很重要。如果可能,请提供错误日志。