所以,我做了一個按鈕,點擊后會打開一個彈出視窗,里面有兩個按鈕,但現在我無法像對待主視窗中的所有其他按鈕那樣,在OnCreateMethod中為這兩個按鈕指定setOnClickListner(如果我這樣做,應用程式就會停止回應),因為直到那個時候按鈕還沒有實際存在(我想,這就是原因),所以請你幫助我為這些按鈕指定on click listner。
以下是popupWindow的代碼:
public void onButtonShowPopupWindowClick(View view) {
// 膨脹彈出視窗的布局
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE)。
View popupView = inflater.inflate(R.layout.confirm_number_popup, null)。
hideSoftKeyboard(Register.this)。
//創建彈出視窗
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // 讓在彈出視窗外的點擊也能將其駁回。
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, false);
// 顯示彈出視窗
// 你傳入哪個視圖并不重要,它只被用于視窗標記。
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// 當被觸摸時解除彈出視窗
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss()。
回傳true。
}
});
}
下面是彈出式視圖的布局檔案:
如果你不能理解這個問題或需要其他代碼,請告訴我。
謝謝你! 編輯:我已經嘗試在我的OnCreate中添加這個,但它不作業: uj5u.com熱心網友回復: 使用
標籤: 上一篇:如何在安卓串列視圖中添加大量資料
<?
<?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">
<按鈕
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"/>
<按鈕
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>
Button btnEdit = findViewById(R.id.btn_edit);
btnEdit.setOnClickListener(v -> {
//該函式
});
Button btn1 = popupView.findViewById(R.id.btn_edit)。
btn1.setOnClickListener{}。
