我有问题。所以我没有什么活动,在其中一个活动中,我有6个ImageView,我为每个人设置了一个来自xml的“onClick”。我想知道如何检查图像是否被点击,然后出现在另一个活动中。这是我写的,甚至没有完成,我做得好吗?如果是,您可以帮助我将此图像“导入”到最终活动吗?
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);
}
答案 0 :(得分:0)
您可以将您在意图中点击的图像放到其他活动中。
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 = getIntent();
String clickedImageName = intent.getStringExtra("firstImageKey");
if(clickedImageName.equals("firstImage"){
//set desire image
}
您的案例有很多解决方案,但这是我想到的第一个。
<强>更新强>
这是第二个可以解决问题的解决方案:
首先在你的xml中有六个imageViews,你必须像这样为每一个设置标签。
<强> 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>
然后在您的MainActivity(您的六个图像的活动)中声明所有图像视图,向您的类实现View.OnClickListener
并在ClickClick侦听器上设置所有图像。之后在onClick()
方法中实现逻辑,就像您单击第一个图像使意图成为第二个活动并将图像标记传递给意图。所有行动都可能代表这样。
<强> MainActivity.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);
}
}
然后,当您成功创建intent时,在SecondActivity中获取额外的内容,并根据您从第一个活动设置所需的图像源到第二个活动中的imageView。
<强> SecondActivity.java 强>
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看起来像这样。
<强> activity_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"/>
</RelativeLayout>
但这仅适用于您的测试用例,您必须更改它。希望此更新可以帮助您!
答案 1 :(得分:0)
请上传完整的照片。
请在xml中使用click =“onImageClick”。
public ImageView imageSelected;
//if you use R.drawabel.logo in code or @drawable/logo in xml.
private int resid = 0;
public void onImageClick(View view) {
Intent i = new Intent(this, MemeSecondStep.class);
switch(view.getId()){
case R.id.goitiPunu:
//if you use @drawable/goitiPunu in layout.
resid = R.drawable.goitiPunu;
break
case R.id.goitiPdoi:
break
}
i.putExtra("resid",resid);
startActivity(i);
}
MemeSecondStep.java
private void onCreate(Some params){
int resid = getIntent().getIntegerExtra("resid",0);
imageView.setImageResource(resid).
}