我正在为一家餐厅开发Android应用程序,用于食品订购系统。
在我的项目中,我有一个
在我的项目中,我有一个
Gridview
,当单击网格视图中项目上的button
时,我想要获取一个带有一些文本字段、按钮等的弹出窗口。
我从这个文档示例得到了帮助。
上面的文档示例作为单独的应用程序可以正常工作。它只是用于创建基本弹出窗口的示例,但我需要在Gridview
上使用它。
我的工作-->
我使用了我的grid_single.xml
和它的实现文件
Grid_single.java
,而不是上述文档中的main.xml
和PopUpWinndowDemoActivity.java
(它是主活动)。
但对我来说它不起作用。当我点击每个项目的按钮时,没有像我希望的那样显示弹出窗口。所以,请帮助我解决这个问题或给我一个在网格视图项目上使用弹出窗口的工作示例。我已经附上了我的文件。
等待您的帮助, 谢谢!Grid_single.java
Button btnClosePopup;
Button btnCreatePopup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_single);
btnCreatePopup = (Button) findViewById(R.id.addToCart);
btnCreatePopup.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
initiatePopupWindow();
}
});
btnCreatePopup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
initiatePopupWindow();
}
});
}
private PopupWindow pwindo;
private void initiatePopupWindow() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) Grid_Single_Popup.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_popup, (ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, 300, 370, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
private OnClickListener cancel_button_click_listener = new OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
};
grid_single.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:padding="5dp"
android:id="@+id/grid_single_back"
>
<ImageView
android:id="@+id/grid_image"
android:layout_width="150dp"
android:layout_height="150dp">
</ImageView>
<TextView
android:id="@+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:textSize="24dp" >
</TextView>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="Cutomize & Add"
android:id="@+id/addToCart"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:textSize="18dp"
android:padding="5sp"/>
</LinearLayout>
提前致谢