当用户在测验中对我的问题给出正确答案时,创建带有下一步按钮的弹出窗口 window
Create a popup window with a next button when a user gives the right answer to my question in the quiz
我正在创建一个测验应用程序,我想在其中创建一个弹出窗口 window,当用户给出正确答案时,应该会出现一个弹出窗口,显示他已给出正确答案的文本和一个按钮.该按钮应将用户移至应用中的下一个问题。
我尝试创建一个 popup.xml 当用户给出正确答案时它会响应。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="500dp"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center"
android:background="#242424">
<TextView
android:id="@+id/correct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="30dp"
android:textSize="22sp"
android:textColor="#FFFFFF"
android:text="Correct! Go to Next."/>
<Button
android:id="@+id/button3"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
并调用此方法
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.popup_window, null);
// 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, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
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;
}
});
}
我从 How to create a popup window (PopupWindow) in Android 的一些答案中得到了帮助,但没有得到正确的答案。帮帮我。
这里是在onButtonShowPopupWindowClick()方法中添加clicklistener后的代码
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.popup_window, null);
Button btn = findViewById(R.id.button3);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(level_one.this,level_two.class);
startActivity(i);
}
});
// 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, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
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;
}
});
}
以下跟踪显示我的应用崩溃
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.geeksmind, PID: 18894
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.geeksmind.level_one.onButtonShowPopupWindowClick(level_one.java:180)
at com.example.geeksmind.level_one.onClick(level_one.java:96)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22298)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
问题在下一行
Button btn = findViewById(R.id.button3);
当你膨胀一个弹出窗口时,R.id.button3
就在那里。所以 R.id.button3
的父级是 popupwindow。视图初始化应如下所示
View popupView = inflater.inflate(R.layout.popup_window, null);
Button btn = popupView.findViewById(R.id.button3);
我正在创建一个测验应用程序,我想在其中创建一个弹出窗口 window,当用户给出正确答案时,应该会出现一个弹出窗口,显示他已给出正确答案的文本和一个按钮.该按钮应将用户移至应用中的下一个问题。
我尝试创建一个 popup.xml 当用户给出正确答案时它会响应。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="500dp"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center"
android:background="#242424">
<TextView
android:id="@+id/correct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="30dp"
android:textSize="22sp"
android:textColor="#FFFFFF"
android:text="Correct! Go to Next."/>
<Button
android:id="@+id/button3"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
并调用此方法
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.popup_window, null);
// 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, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
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;
}
});
}
我从 How to create a popup window (PopupWindow) in Android 的一些答案中得到了帮助,但没有得到正确的答案。帮帮我。
这里是在onButtonShowPopupWindowClick()方法中添加clicklistener后的代码
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.popup_window, null);
Button btn = findViewById(R.id.button3);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(level_one.this,level_two.class);
startActivity(i);
}
});
// 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, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
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;
}
});
}
以下跟踪显示我的应用崩溃
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.geeksmind, PID: 18894
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.geeksmind.level_one.onButtonShowPopupWindowClick(level_one.java:180)
at com.example.geeksmind.level_one.onClick(level_one.java:96)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22298)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
问题在下一行
Button btn = findViewById(R.id.button3);
当你膨胀一个弹出窗口时,R.id.button3
就在那里。所以 R.id.button3
的父级是 popupwindow。视图初始化应如下所示
View popupView = inflater.inflate(R.layout.popup_window, null);
Button btn = popupView.findViewById(R.id.button3);