在具有可点击背景项的透明布局上点击侦听器

Click listeners on a transparent layout with background items click-able

我创建了一个透明的 activity。背景是可点击的,打开 activity 可以在手机上执行任何操作。现在我想在透明 activity 内的 textview 上设置点击侦听器。我已经搜索过,但没有找到任何东西。

这是我的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

这是我正在使用的 Java 代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.tv);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
            }
        });


    }

我希望 hello world 和其他应用程序一样可以点击,例如 Chrome、消息、菜单等

activity 无法实现您想要实现的目标。当您使用 activity 时,它会覆盖整个屏幕,并且在屏幕上进行的任何触摸都只能由 activity 访问(无论透明与否,这都没有关系)。

我的建议是,尝试使用小部件而不是 activity。它可能会满足您的要求。我建议您使用任何浮动小部件或将该小部件永久放置在屏幕中央。

以下link可能对您有所帮助。

Chat Widget

Floating widget

您不必为此目的使用 activity。您只需将其称为表单服务 class。

 windowManager2 = (WindowManager)getSystemService(WINDOW_SERVICE);
        LayoutInflater layoutInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view=layoutInflater.inflate(R.layout.popup_incoming, null);
        params=new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        params.gravity=Gravity.CENTER|Gravity.CENTER;
        params.x=0;
        params.y=0;
        windowManager2.addView(view, params);

        view.setOnTouchListener(new View.OnTouchListener() {
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        initialX = params.x;
                        initialY = params.y;
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();
                        return true;
                    case MotionEvent.ACTION_UP:
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        params.x = initialX + (int) (event.getRawX() - initialTouchX);
                        params.y = initialY + (int) (event.getRawY() - initialTouchY);
                        windowManager2.updateViewLayout(view, params);
                        return true;
                }
                return false;
            }
        });



        txtNameIncoming = (TextView)view.findViewById(R.id.NameIncoming);

        txtIncomingnumber = (TextView)view.findViewById(R.id.txtIncomingnumber);
        txtIncomingnumber.setText("You have Incoming Call from " + PhoneStateReceiver.savedNumber);
        btnClose = (Button) view.findViewById(R.id.buttonClose);

        btnClose.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
         clearView(v.getContext());

        }
    });

xml 将是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:background="#0090FF"
android:padding="10dp"
android:keepScreenOn="true" >

<TextView
    android:id="@+id/NameIncoming"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:text="Incoming Caller"
    android:textColor="#fff" />

<TextView
    android:id="@+id/txtIncomingnumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/NameIncoming"
    android:text="Incoming Call Number"
    android:textColor="#fff" />

<Button
    android:id="@+id/buttonClose"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Close" />

现在自己修改一下。