我正在申请一个学校项目并且遇到了一些问题。我目前有一个图像按钮,当点击时打开用户选择图像的图库,然后它被放入图像按钮。我试图创建另一个图像按钮时单击它打开图库并将其放入图像按钮。我创建了两个按钮,但每次单击每个按钮并选择一个图像时,它只会将图像放在左侧图像按钮上。我试图在你点击第一个图像按钮的地方创建它,你选择了一个图像并将它放入该按钮,单击下一个按钮并将其放入该按钮。这很难解释。 这是我的MainActivity:
package com.example.triptych;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity {
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void loadImagefromGallery(View view) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageButton imageButton = (ImageButton) findViewById(R.id.buttonLoadPicture);
// Set the Image in ImageView after decoding the String
imageButton.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
}
这是我的MainActivity2
package com.example.triptych;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity2 extends Activity {
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void loadImagefromGallery(View view) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageButton imageButton = (ImageButton) findViewById(R.id.button2);
// Set the Image in ImageView after decoding the String
imageButton.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
}
这是我的布局(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="@+id/buttonLoadPicture"
android:layout_width="160dp"
android:layout_height="300dp"
android:layout_marginLeft="10dp"
android:layout_weight="0.51"
android:contentDescription="TODO"
android:onClick="loadImagefromGallery"
android:src="@drawable/ic_launcher"
android:text="@string/load_picture" />
<ImageButton
android:id="@+id/button2"
android:layout_width="160dp"
android:layout_height="300dp"
android:layout_marginLeft="180dp"
android:layout_weight="0.51"
android:contentDescription="TODO"
android:onClick="loadImagefromGallery"
android:src="@drawable/ic_launcher"
android:text="@string/load_picture" />
</RelativeLayout>
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.triptych"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的字符串:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Triptych</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="load_picture">Load Picture</string>
</resources>
如果你仍然不明白我的问题是什么以及我为什么要这样做。我有两个图像按钮,我想要它点击它们,选择一个图像,它被放置在图像按钮上。我想要它所以我点击左图像按钮,选择一个图像,按钮现在是我选择的图像。我想点击右边的BUTTON并选择一个图像,而BUTTON现在是我选择的图像。我点击了第一个按钮,选择了一个图像然后按下那个按钮,这是正确的,问题就是当我点击右键并选择一个图像时,它会转到其他图像按钮,而不是我点击的那个按钮。如果你不明白我的意思,我会放弃。谢谢,抱歉听起来很粗鲁,但我的每一个问题都被评为糟糕,所以我试图把它告诉你我需要什么。如果您了解如何执行此操作,请提供帮助,谢谢。
答案 0 :(得分:0)
我想我明白发生了什么......
您发布了2 activities
,但找不到转到MainActivity2
的方法,因此如果您仍在同一个Activity中,则会调用方法loadImagefromGallery
(您在{ {1}})始终从第一个活动开始,因此有一个代码只能更改一个按钮。在这种情况下是左键。
我希望这个猜测很有帮助。
无论如何,如果您想在每个onClick
中执行此操作,您应该在方法Activity
中执行loadImagefromGallery
,这样您就会知道点击了哪个按钮。然后将其设置为该按钮。
答案 1 :(得分:-1)
创建一个全局变量Button clicked_button并在每次单击按钮时初始化它,这将是您更改图像的按钮。