所以基本上,我有一個editText在我的活動中,用戶輸入他/她的電話號碼,然后點擊一個Next按鈕。現在,在點擊該按鈕時,我膨脹了一個彈出式視圖,向用戶顯示他/她輸入的號碼,并問他是否可以。
這里的問題是,我無法從EditText中訪問數字以在彈出式視圖中顯示它。誰能告訴我如何做到這一點?
這里是編輯文本:
<EditText
android:id="@ id/Phone_Number"/span>
android:layout_width="300dp"/span>
android:layout_height="wrap_content"/span>
android:importantForAutofill="no"。
android:inputType="phone"/>
確認號碼彈出TextView :
<TextView
android:id="@ id/txt_phone_number_confirm"/span>
android:layout_width="wrap_content"/span>
android:layout_height="wrap_content"/span>
android:text="" />
Layout Inflator:
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater =(LayoutInflater
getSystemService(LAYOUT_INFLATER_SERVICE)。
@SuppressLint("InflateParams") View popupView = inflater. inflate(R.layout.confirm_number_popup, null)。
ViewGroup root = (ViewGroup) getWindow().getDecorView().getRootView()。
Button btn1 = popupView.findViewById(R.id.btn_edit)。
hideSoftKeyboard(Register.this)。
//創建彈出視窗。
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
final PopupWindow popupWindow = new PopupWindow(popupView, 寬度。
height, false)。)
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0) 。
編輯:當我像這樣改變inflator來設定文本時,我得到一個空指標例外:
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater =(LayoutInflater
getSystemService(LAYOUT_INFLATER_SERVICE)。
@SuppressLint("InflateParams") View popupView = inflater。 inflate(R.layout.confirm_number_popup, null)。
ViewGroup root = (ViewGroup) getWindow().getDecorView().getRootView()。
Button btn1 = popupView.findViewById(R.id.btn_edit)。
hideSoftKeyboard(Register.this)。
//創建彈出視窗。
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
final PopupWindow popupWindow = new PopupWindow(popupView, 寬度。
height, false)。)
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0) 。
//Edit
TextView txt_phone_confirm =
findById(R.id.txt_phone_number_confirm)。
EditText txt_Phone = findViewById(R.id.Phone_Number)。
txt_phone_confirm.setText(txt_Phone.getText().toString())。
btn1.setOnClickListener(v -> {
clearDim(root);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)。
popupWindow.dismiss()。
});
}
在這樣做的時候,我得到了:
java.lang.NullPointerException: 嘗試呼叫虛擬方法'void
android.widget.TextView.setText(java.lang.CharSequence)'對一個null的
物件的參考
uj5u.com熱心網友回復:
在popupView上呼叫find view by id方法,因為這個視圖有你的TextView:
TextView txt_phone_confirm = popupView.findViewById(R.id.txt_phone_number_confirm) 。
這將得到你剛剛膨脹的文本視圖。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/327618.html
標籤:
上一篇:TextEdit的值被傳遞到另一個活動中,但當重新訪問下一個活動時,在AndroidStudio中使用JAVA的值會丟失。
