gpt4 book ai didi

java - 设置 X 和 Y 坐标时按钮单击事件不起作用

转载 作者:行者123 更新时间:2023-12-02 03:06:40 26 4
gpt4 key购买 nike

我创建了简单的 ImageView 并设置了一个框架,并设置了按钮的 x 和 y 坐标,然后设置了 x 和 y 坐标,这次按钮单击事件起作用,但设置了 x 和 y 坐标不起作用按钮单击事件。

我的类(class)

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private ImageView viewImage;
private Button btnCapture;
private static final int RESULT_LOAD_IMAGE = 1;
private static final int REQUEST_IMAGE_CAPTURE = 2;
private Uri mCapturedImageURI;

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCapture = (Button) findViewById(R.id.btnCapture);
btnCapture.setX(850);
btnCapture.setY(300);
btnCapture.setOnClickListener(this);
viewImage = (ImageView) findViewById(R.id.viewImage);
viewImage.setOnTouchListener(new Touch(getApplicationContext()));
}


@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void activeTakePhoto() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
String fileName = "temp.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver()
.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
takePictureIntent
.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}

private void activeGallery() {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE &&
resultCode == RESULT_OK && null != data) {

Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver()
.query(selectedImage, filePathColumn, null, null,
null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();

Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
viewImage.setImageBitmap(bitmap);

}
case REQUEST_IMAGE_CAPTURE:
if (requestCode == REQUEST_IMAGE_CAPTURE &&
resultCode == RESULT_OK) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor =
getContentResolver().query(mCapturedImageURI, projection, null,
null, null);
int column_index_data = cursor.getColumnIndexOrThrow(
MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String picturePath = cursor.getString(column_index_data);
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
viewImage.setImageBitmap(bitmap);
}
}
}

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnCapture:
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.custom_dialog_box);
dialog.setTitle("Select Image");
Button dialogButton = (Button) dialog.findViewById(R.id.btnExit);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button btnChoosePath = (Button) dialog.findViewById(R.id.btnChoosePath);
btnChoosePath.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View v) {
activeGallery();
dialog.dismiss();
}
});
Button btnTakePhoto = (Button) dialog.findViewById(R.id.btnTakePhoto);
btnTakePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
activeTakePhoto();
dialog.dismiss();
}
});
dialog.show();
break;
}

}

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">


<Button
android:id="@+id/btnCapture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/camera_icon" />

<RelativeLayout
android:id="@+id/FrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
android:id="@+id/viewImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix" />
</FrameLayout>

<ImageView
android:id="@+id/abc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/aa" />
</RelativeLayout>

最佳答案

您能否将 onClick(View view) 方法替换为以下内容。

 @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnCapture:

final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.custom_dialog_box);
dialog.setTitle("Select Image");
Button dialogButton = (Button) dialog.findViewById(R.id.btnExit);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button btnChoosePath = (Button) dialog.findViewById(R.id.btnChoosePath);
btnChoosePath.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View v) {
activeGallery();
dialog.dismiss();
}
});
Button btnTakePhoto = (Button) dialog.findViewById(R.id.btnTakePhoto);
btnTakePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
activeTakePhoto();
dialog.dismiss();
}
});
dialog.show();

break;
}

}

已编辑答案。更改 MainActivity.class 中的以下内容

btnCapture.setX(300);
btnCapture.setY(850);

并将您的 xml 文件替换为以下内容。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">


<RelativeLayout
android:id="@+id/FrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
android:id="@+id/viewImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix" />
</FrameLayout>

<ImageView
android:id="@+id/abc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blue_squre" />
</RelativeLayout>

<ImageButton
android:id="@+id/btnCapture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

在您的 xml 中,有一件事您使用 btnCapture id 作为 Button。然后在 .class 文件中创建 ImageButton 对象。我不知道它对你有什么作用。但我改变它尝试一下。

关于java - 设置 X 和 Y 坐标时按钮单击事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41628478/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com