【问题标题】:How to check, in which contentView am I actually? Android Studio如何检查,我实际上在哪个 contentView 中?安卓工作室
【发布时间】:2021-03-07 17:56:27
【问题描述】:

嗨!

我的 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 它在photo.xml 中找到一个ConstraintLayout,但应用程序仍在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 中而不是其他?

【问题讨论】:

  • 为什么要更改onActivityResult中的activity布局?你真的应该把它分开并至少为每个xml-view 自己的类
  • 是的,我知道这很糟糕,我会把它分开,但现在可以在 OnTouchEvent 中做一些 if 语句来检查我在 activity_main.xml 上不做的布局?或者现在将它分离到另一个类并将 onTouchEvent 移动到另一个类会更快?

标签: java android touch-event contentview


【解决方案1】:

真的应该为每项功能设置单独的 Activity(或 Fragment),而不是替换当前视图,而是在获取图像时启动新的 Activity(或 Fragment)。从长远来看,这将是最好的,所以我建议这样做。

也就是说 - 要解决这个问题现在 - 你可以在 onTouchEvent 中进行空检查:

ConstraintLayout constraintLayout = findViewById(R.id.photo);
if (constraintLayout == null) {
    return false;
}

【讨论】:

    猜你喜欢
    • 2015-04-15
    • 2018-03-21
    • 2022-01-03
    • 2017-02-09
    • 2020-05-13
    • 2018-07-10
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    相关资源
    最近更新 更多