【问题标题】:How to define setOnClickListner in a popup view(Android Studio)?如何在弹出视图(Android Studio)中定义 setOnClickListner?
【发布时间】:2021-09-15 14:37:08
【问题描述】:

所以,我制作了一个按钮,单击该按钮会打开一个弹出窗口,其中包含两个按钮,但现在我无法将 setOnClickListner 分配给 OnCreateMethod 中的这两个按钮,就像我为主中的所有其他按钮所做的那样窗口(如果我这样做,则应用程序停止响应)直到那时按钮实际上并不存在(我认为,这就是原因),所以请你帮我将点击列表分配给这些按钮。

这里是弹出窗口执行的代码:

 public void onButtonShowPopupWindowClick(View view) {

    // inflate the layout of the popup window
    LayoutInflater inflater = (LayoutInflater)
            getSystemService(LAYOUT_INFLATER_SERVICE);
    View popupView = inflater.inflate(R.layout.confirm_number_popup, null);

    hideSoftKeyboard(Register.this);

    // create the popup window
    int width = LinearLayout.LayoutParams.WRAP_CONTENT;
    int height = LinearLayout.LayoutParams.WRAP_CONTENT;
    boolean focusable = true; // lets taps outside the popup also dismiss it
    final PopupWindow popupWindow = new PopupWindow(popupView, width, height, false);

    // show the popup window
    // which view you pass in doesn't matter, it is only used for the window token
    popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

    // dismiss the popup window when touched
    popupView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            popupWindow.dismiss();
            return true;
        }
    });
}

这是弹出视图的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/white">

<Button
    android:id="@+id/btn_edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/green_shade_1"
    android:text="@string/edit"
    android:textSize="16sp"
    android:layout_below="@id/txt_edit"
    android:layout_alignParentStart="true"
    android:layout_marginStart="20dp"
    android:background="@color/white"
    style="?android:attr/borderlessButtonStyle"/>

<Button
    android:id="@+id/btn_Ok"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/green_shade_1"
    android:text="@string/ok"
    android:textSize="16sp"
    android:layout_below="@id/txt_edit"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="20dp"
    android:background="@color/white"
    style="?android:attr/borderlessButtonStyle"/>
    
</RelativeLayout>

如果您无法理解该问题或是否需要任何其他代码,请告诉我。
谢谢!

编辑:我已经尝试在我的 OnCreate 中添加它,但它不起作用:

Button btnEdit = findViewById(R.id.btn_edit);

btnEdit.setOnClickListener(v -> {
        //The function
    });

【问题讨论】:

    标签: android-studio onclicklistener popupwindow


    【解决方案1】:

    使用

    Button btn1 = popupView.findViewById(R.id.btn_edit);
    btn1.setOnClickListener{}
    

    【讨论】:

    • 你好@Alireza,你能告诉我这里的popupview是什么吗?
    • 你好。弹出视图是 View popupView = inflater.inflate(R.layout.confirm_number_popup, null);
    • 是的,成功了,非常感谢!!!?
    • 您能否告诉我如何在此编辑按钮的 setOnClickListner 中关闭此弹出窗口?
    • 有没有popupWindow.dismiss()??查一下
    猜你喜欢
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多