如何在弹出视图中定义 setOnClickListner(Android Studio)?
How to define setOnClickListner in a popup view(Android Studio)?
所以,我制作了一个按钮,点击它会打开一个弹出窗口 window,里面有两个按钮,但现在我无法像我对所有按钮所做的那样,将 setOnClickListner 分配给 OnCreateMethod 中的这两个按钮主要 window 中的其他按钮(如果我这样做,应用程序将停止响应)直到那个时候按钮实际上并不存在(我想,这就是原因),所以你能帮我分配点击监听器吗那些按钮。
这里是弹出窗口执行的代码:
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
});
使用
Button btn1 = popupView.findViewById(R.id.btn_edit);
btn1.setOnClickListener{}
所以,我制作了一个按钮,点击它会打开一个弹出窗口 window,里面有两个按钮,但现在我无法像我对所有按钮所做的那样,将 setOnClickListner 分配给 OnCreateMethod 中的这两个按钮主要 window 中的其他按钮(如果我这样做,应用程序将停止响应)直到那个时候按钮实际上并不存在(我想,这就是原因),所以你能帮我分配点击监听器吗那些按钮。
这里是弹出窗口执行的代码:
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
});
使用
Button btn1 = popupView.findViewById(R.id.btn_edit);
btn1.setOnClickListener{}