gpt4 book ai didi

Android popupWindow,无法在弹出窗口中获取元素

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:38:15 24 4
gpt4 key购买 nike

我在使用 调试器 从我的 PopupWindow 中获取我的 Button 和其他元素时遇到问题,它只是报告为

private void initiatePopupWindow() 
{
try
{
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) LoginActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.activity_register,(ViewGroup)

findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, 600, 900, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

btnReg = (Button) findViewById(R.id.btnReg);
inputName = (EditText) findViewById(R.id.name);
inputDOB = (DatePicker) findViewById(R.id.dob);
String name = inputName.getText().toString();

btnReg.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{


if ( ( !inputName.getText().toString().equals("")) &&
( getAge(inputDOB.getDayOfMonth(), inputDOB.getMonth(), inputDOB.getYear()) > 15) )
{
//register user
}
else if ( ( inputName.getText().toString().equals("")) )
{
Toast.makeText(getApplicationContext(),
"Please enter your name", Toast.LENGTH_SHORT).show();
}
else if (( getAge(inputDOB.getDayOfMonth(), inputDOB.getMonth(), inputDOB.getYear()) < 16) )
{
Toast.makeText(getApplicationContext(),
"You must be at least 16 to use this app", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), email, Toast.LENGTH_SHORT).show();



}

}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}

谁能指出我正确的方向或解释为什么我无法使用 R.id 找到它们?

干杯

更新:Activity_Register.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/rounded"
android:orientation="vertical"
android:padding="10sp" >

<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="@string/name"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">

<requestFocus />
</EditText>

<DatePicker
android:id="@+id/dobPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:layout_below="@+id/dob"
android:layout_centerHorizontal="true"/>

<TextView
android:id="@+id/dob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dob"
android:layout_below="@+id/name"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp"/>

<Button
android:id="@+id/btnReg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="38dp"
android:text="@string/register"
android:layout_below="@+id/dobPicker"
android:layout_centerHorizontal="true"/>

</RelativeLayout>

最佳答案

你可以点赞

View layout = inflater.inflate(R.layout.activity_register,
(ViewGroup) youractivity.this.findViewById(R.id.popup_element));

btnReg = (Button) layout.findViewById(R.id.btnReg);
inputName = (EditText) layout.findViewById(R.id.name);
inputDOB = (DatePicker) layout.findViewById(R.id.dob);

这是因为您的所有元素都来自 inflated layout View ,您需要传递 context 以将 popup_element 布局作为 查看组

关于Android popupWindow,无法在弹出窗口中获取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22396969/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com