你好!
我的 Android 应用程序有一些问题,我只在一个班级工作(我知道这很糟糕,但现在它对我来说是最快的选择),我有基本活动布局(activity_main.xml
),我在这个布局上有 2 个按钮,其中一个按钮具有 setOnClickListener 以打开文件管理器以选择照片并将其添加到photo.xml
.
imageBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 1234:
if(resultCode == RESULT_OK){
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 filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap SelectedImage = BitmapFactory.decodeFile(filePath);
setContentView(R.layout.photo);
ImageView img = findViewById(R.id.image);
int screenWidth = DeviceDimensionsHelper.getDisplayWidth(this);
int screenHeight = DeviceDimensionsHelper.getDisplayHeight(this);
Bitmap SelectedImageScaled = Bitmap.createScaledBitmap(SelectedImage, screenWidth, screenHeight, true);
img.setImageBitmap(SelectedImageScaled);
}
}
}
然后,我有 OnTouchEvent,我正在搜索具有 id=photo 的 ConstraintLayout,它在 中photo.xml
,但是如果我在其中main_activity.xml
,然后如果我单击按钮以外的其他位置,应用程序将崩溃,bcs 它正在寻找 ConstraintLayout inphoto.xml
但应用仍在main_acitivity.xml
.
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
int x = (int)event.getX();
int y = (int)event.getY();
TextView tv;
if(i < 2) {
tv = new TextView(MainActivity.this);
tv.setText("\u29bf");
tv.setId(tv.generateViewId());
ConstraintLayout constraintLayout = (ConstraintLayout)findViewById(R.id.photo);
constraintLayout.addView(tv);
tv.setX(x);
tv.setY(y-360);
if(i == 0){
pos[0] = constraintLayout.findViewById(tv.getId()).getX();
pos[1] = constraintLayout.findViewById(tv.getId()).getY();
} else {
pos[2] = constraintLayout.findViewById(tv.getId()).getX();
pos[3] = constraintLayout.findViewById(tv.getId()).getY();
Log.i("222", "x1= " + pos[0] + " y1= " + pos[1] + " x2= " + pos[2] + " y2= " + pos[3]);
if(pos[2] < pos[0] && pos[1] < pos[3]){
Toast.makeText(getBaseContext(), "Różnica X= " + String.valueOf((int)(pos[0] - pos[2])) + " , różnica Y= " + String.valueOf((int)(pos[3] - pos[1])), Toast.LENGTH_SHORT).show();
}
else if(pos[2] < pos[0] && pos[1] > pos[3]){
Toast.makeText(getBaseContext(), "Różnica X= " + String.valueOf((int)(pos[0] - pos[2])) + " , różnica Y= " + String.valueOf((int)(pos[1] - pos[3])), Toast.LENGTH_SHORT).show();
}
else if(pos[2] > pos[0] && pos[1] < pos[3]){
Toast.makeText(getBaseContext(), "Różnica X= " + String.valueOf((int)(pos[2] - pos[0])) + " , różnica Y= " + String.valueOf((int)(pos[3] - pos[1])), Toast.LENGTH_SHORT).show();
}
else if(pos[2] > pos[0] && pos[1] > pos[3]){
Toast.makeText(getBaseContext(), "Różnica X= " + String.valueOf((int)(pos[2] - pos[0])) + " , różnica Y= " + String.valueOf((int)(pos[1] - pos[3])), Toast.LENGTH_SHORT).show();
}
}
i++;
}
}
return false;
}
我如何在 onTouchEvent 中创建 if 来检查我是否在photo.xml
其他人中?