Android 当从单独的类调用PopupWindow方法时,如何从xml文件定义PopupWindow中的布局
当从单独的类调用PopupWindow方法时,我希望能够从xml文件定义PopupWindow中的布局。下面的代码可以根据需要工作,只是布局是从java文件而不是xml文件中提取的。在这种情况下,我不知道如何正确访问xml布局并在PopupWindow中实现,或者这是否可行。感谢您的建议和具体建议。谢谢 showPopup.javaAndroid 当从单独的类调用PopupWindow方法时,如何从xml文件定义PopupWindow中的布局,android,xml,popupwindow,Android,Xml,Popupwindow,当从单独的类调用PopupWindow方法时,我希望能够从xml文件定义PopupWindow中的布局。下面的代码可以根据需要工作,只是布局是从java文件而不是xml文件中提取的。在这种情况下,我不知道如何正确访问xml布局并在PopupWindow中实现,或者这是否可行。感谢您的建议和具体建议。谢谢 showPopup.java public class showPopup { Context ctx; Button btnDismiss; public showPopup(Context
public class showPopup {
Context ctx;
Button btnDismiss;
public showPopup(Context ctx){
this.ctx = ctx;
}
public void onCreateView(LayoutInflater layoutInflater, ViewGroup container) {
View layout = layoutInflater.inflate(R.layout.popup_layout, null);
btnDismiss = (Button) layout.findViewById(R.id.btndismissxml);
}
public void goJoe(View parent){
final PopupWindow popup = new PopupWindow(ctx);
btnDismiss = new Button (ctx);
btnDismiss.setText("Text is from showPopup.java");
popup.setContentView(btnDismiss);
popup.setWidth(400);
popup.setHeight(580);
popup.showAtLocation(parent, Gravity.CENTER_HORIZONTAL, 10, 10);
btnDismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popup.dismiss();
}
});
}
}
public class Tab3Fragment extends Fragment implements OnClickListener{
Button btnPopup;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup containerGroup, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab3_fragment, containerGroup, false);
btnPopup = (Button)v.findViewById(R.id.btnPopupxml);
btnPopup.setOnClickListener(this);
return v;
}
//@Override
public void onViewCreated(View v) {
btnPopup = (Button)v.findViewById(R.id.btnPopupxml);
btnPopup.setOnClickListener(this);
}
@Override
public void onClick(View parent) {
new showPopup(getActivity().getApplicationContext()).goJoe(parent);
}
}
Tab3Fragment.java
public class showPopup {
Context ctx;
Button btnDismiss;
public showPopup(Context ctx){
this.ctx = ctx;
}
public void onCreateView(LayoutInflater layoutInflater, ViewGroup container) {
View layout = layoutInflater.inflate(R.layout.popup_layout, null);
btnDismiss = (Button) layout.findViewById(R.id.btndismissxml);
}
public void goJoe(View parent){
final PopupWindow popup = new PopupWindow(ctx);
btnDismiss = new Button (ctx);
btnDismiss.setText("Text is from showPopup.java");
popup.setContentView(btnDismiss);
popup.setWidth(400);
popup.setHeight(580);
popup.showAtLocation(parent, Gravity.CENTER_HORIZONTAL, 10, 10);
btnDismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popup.dismiss();
}
});
}
}
public class Tab3Fragment extends Fragment implements OnClickListener{
Button btnPopup;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup containerGroup, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab3_fragment, containerGroup, false);
btnPopup = (Button)v.findViewById(R.id.btnPopupxml);
btnPopup.setOnClickListener(this);
return v;
}
//@Override
public void onViewCreated(View v) {
btnPopup = (Button)v.findViewById(R.id.btnPopupxml);
btnPopup.setOnClickListener(this);
}
@Override
public void onClick(View parent) {
new showPopup(getActivity().getApplicationContext()).goJoe(parent);
}
}
popup_layout.xml
<?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="fill_parent"
android:id="@+id/layout">
<Button android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:id="@+id/btndismissxml"
android:text="Text is from popup_layout.xml"></Button>
</RelativeLayout>
更新(1835111年12月):
这是一个草稿弹出窗口。在应用程序中,将有几个服务用户可编辑的下拉字段
您可以尝试使用
从API文档中,显示对话框窗口的片段,浮动在其活动窗口的顶部。该片段包含一个对话框对象,它根据片段的状态适当地显示该对话框对象。对话框的控制(决定何时显示、隐藏、关闭)应该通过此处的API完成,而不是直接调用对话框
实现应该重写这个类并实现onCreateView(LayoutFlater、ViewGroup、Bundle)来提供对话框的内容。或者,他们可以覆盖onCreateDialog(Bundle)以创建一个完全自定义的对话框,例如一个AlertDialog,它有自己的内容
1.创建自定义弹出式布局
2.创建自定义对话框片段
3.创建按钮点击界面
4.根据你的片段调用它
1.创建自定义弹出式布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/app_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="TITLE"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/popupMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/app_title"
android:layout_below="@+id/app_title"
android:layout_marginTop="30dp"
android:text="Content" />
<Button
android:id="@+id/navBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/popupMsg"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="OK" />
</RelativeLayout>
import android.app.Dialog;
import android.app.DialogFragment;
.....
public class CustomAlert extends DialogFragment {
public OnSubmitListener mListener;
Context context;
String Title = "";
String content = "";
public CustomAlert (Context context, String title, String message) {
this.context = context;
this.Title = title;
this.content = message;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = new Dialog(getActivity());
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialog.setContentView(R.layout.popup_layout);
Window window = dialog.getWindow();
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.TRANSPARENT));
dialog.show();
TextView title = (TextView) dialog.findViewById(R.id.app_title);
TextView msg = (TextView) dialog.findViewById(R.id.popupMsg);
Button btn = (Button) dialog.findViewById(R.id.navBtn);
title.setText(Title);
msg.setText(content);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.setOnAlert(content);
dismiss();
}
});
return dialog;
}
}
public interface OnSubmitListener{
void setOnAlert(String arg);
}
public class Tab3Fragment extends Fragment implements OnClickListener, OnSubmitListener{
Button btnPopup;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup containerGroup, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab3_fragment, containerGroup, false);
btnPopup = (Button)v.findViewById(R.id.btnPopupxml);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btnPopup.setOnClickListener(getActivity());
}
@Override
public void onClick(View v) {
ShowDialog("Title", "Sample content");
}
private void ShowDialog(String title, String msg) {
CustomAlert fragment1 = new CustomAlert (
getActivity(), title, msg);
fragment1.mListener = this;
fragment1.show(getFragmentManager(), "");
}
@Override
public void setOnAlert(String arg) {
Toast.makeText(getActivity(),
"Button Clicked:"+arg,
Toast.LENGTH_LONG).show();
}
}
3.为按钮单击创建界面。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/app_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="TITLE"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/popupMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/app_title"
android:layout_below="@+id/app_title"
android:layout_marginTop="30dp"
android:text="Content" />
<Button
android:id="@+id/navBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/popupMsg"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="OK" />
</RelativeLayout>
import android.app.Dialog;
import android.app.DialogFragment;
.....
public class CustomAlert extends DialogFragment {
public OnSubmitListener mListener;
Context context;
String Title = "";
String content = "";
public CustomAlert (Context context, String title, String message) {
this.context = context;
this.Title = title;
this.content = message;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = new Dialog(getActivity());
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialog.setContentView(R.layout.popup_layout);
Window window = dialog.getWindow();
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.TRANSPARENT));
dialog.show();
TextView title = (TextView) dialog.findViewById(R.id.app_title);
TextView msg = (TextView) dialog.findViewById(R.id.popupMsg);
Button btn = (Button) dialog.findViewById(R.id.navBtn);
title.setText(Title);
msg.setText(content);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.setOnAlert(content);
dismiss();
}
});
return dialog;
}
}
public interface OnSubmitListener{
void setOnAlert(String arg);
}
public class Tab3Fragment extends Fragment implements OnClickListener, OnSubmitListener{
Button btnPopup;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup containerGroup, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab3_fragment, containerGroup, false);
btnPopup = (Button)v.findViewById(R.id.btnPopupxml);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btnPopup.setOnClickListener(getActivity());
}
@Override
public void onClick(View v) {
ShowDialog("Title", "Sample content");
}
private void ShowDialog(String title, String msg) {
CustomAlert fragment1 = new CustomAlert (
getActivity(), title, msg);
fragment1.mListener = this;
fragment1.show(getFragmentManager(), "");
}
@Override
public void setOnAlert(String arg) {
Toast.makeText(getActivity(),
"Button Clicked:"+arg,
Toast.LENGTH_LONG).show();
}
}
4.在片段上实现它。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/app_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="TITLE"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/popupMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/app_title"
android:layout_below="@+id/app_title"
android:layout_marginTop="30dp"
android:text="Content" />
<Button
android:id="@+id/navBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/popupMsg"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="OK" />
</RelativeLayout>
import android.app.Dialog;
import android.app.DialogFragment;
.....
public class CustomAlert extends DialogFragment {
public OnSubmitListener mListener;
Context context;
String Title = "";
String content = "";
public CustomAlert (Context context, String title, String message) {
this.context = context;
this.Title = title;
this.content = message;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = new Dialog(getActivity());
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialog.setContentView(R.layout.popup_layout);
Window window = dialog.getWindow();
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.TRANSPARENT));
dialog.show();
TextView title = (TextView) dialog.findViewById(R.id.app_title);
TextView msg = (TextView) dialog.findViewById(R.id.popupMsg);
Button btn = (Button) dialog.findViewById(R.id.navBtn);
title.setText(Title);
msg.setText(content);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.setOnAlert(content);
dismiss();
}
});
return dialog;
}
}
public interface OnSubmitListener{
void setOnAlert(String arg);
}
public class Tab3Fragment extends Fragment implements OnClickListener, OnSubmitListener{
Button btnPopup;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup containerGroup, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab3_fragment, containerGroup, false);
btnPopup = (Button)v.findViewById(R.id.btnPopupxml);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btnPopup.setOnClickListener(getActivity());
}
@Override
public void onClick(View v) {
ShowDialog("Title", "Sample content");
}
private void ShowDialog(String title, String msg) {
CustomAlert fragment1 = new CustomAlert (
getActivity(), title, msg);
fragment1.mListener = this;
fragment1.show(getFragmentManager(), "");
}
@Override
public void setOnAlert(String arg) {
Toast.makeText(getActivity(),
"Button Clicked:"+arg,
Toast.LENGTH_LONG).show();
}
}
5.快乐编码….更改
showPopup
如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/app_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="TITLE"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/popupMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/app_title"
android:layout_below="@+id/app_title"
android:layout_marginTop="30dp"
android:text="Content" />
<Button
android:id="@+id/navBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/popupMsg"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="OK" />
</RelativeLayout>
import android.app.Dialog;
import android.app.DialogFragment;
.....
public class CustomAlert extends DialogFragment {
public OnSubmitListener mListener;
Context context;
String Title = "";
String content = "";
public CustomAlert (Context context, String title, String message) {
this.context = context;
this.Title = title;
this.content = message;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = new Dialog(getActivity());
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialog.setContentView(R.layout.popup_layout);
Window window = dialog.getWindow();
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.TRANSPARENT));
dialog.show();
TextView title = (TextView) dialog.findViewById(R.id.app_title);
TextView msg = (TextView) dialog.findViewById(R.id.popupMsg);
Button btn = (Button) dialog.findViewById(R.id.navBtn);
title.setText(Title);
msg.setText(content);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.setOnAlert(content);
dismiss();
}
});
return dialog;
}
}
public interface OnSubmitListener{
void setOnAlert(String arg);
}
public class Tab3Fragment extends Fragment implements OnClickListener, OnSubmitListener{
Button btnPopup;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup containerGroup, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab3_fragment, containerGroup, false);
btnPopup = (Button)v.findViewById(R.id.btnPopupxml);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btnPopup.setOnClickListener(getActivity());
}
@Override
public void onClick(View v) {
ShowDialog("Title", "Sample content");
}
private void ShowDialog(String title, String msg) {
CustomAlert fragment1 = new CustomAlert (
getActivity(), title, msg);
fragment1.mListener = this;
fragment1.show(getFragmentManager(), "");
}
@Override
public void setOnAlert(String arg) {
Toast.makeText(getActivity(),
"Button Clicked:"+arg,
Toast.LENGTH_LONG).show();
}
}
public class showPopup {
Context ctx;
Button btnDismiss, btnSaveRecord, btnLastRecord;
EditText edSpeciesLookup,edSpeciesLookupRowid;
DBAdapter msdb;
SQLiteDatabase db;
public showPopup(Context ctx){
this.ctx = ctx;
msdb = new DBAdapter(ctx,"gfda", null);
db = msdb.getWritableDatabase();
}
public void goJoe(View parent){
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popUpView = inflater.inflate(R.layout.popup_layout, null, false);
final PopupWindow popup = new PopupWindow(popUpView, 400,
580, true);
popup.setContentView(popUpView);
btnDismiss = (Button) popUpView.
findViewById(R.id.btndismissxml);
edSpeciesLookup = (EditText) popUpView.
findViewById(R.id.editspecieslookupxml);
edSpeciesLookupRowid = (EditText) popUpView.
findViewById(R.id.editspecieslookuprowidxml);
popup.showAtLocation(parent, Gravity.CENTER_HORIZONTAL, 10, 10);
btnDismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popup.dismiss();
}
});
btnSaveRecord = (Button) popUpView.findViewById(R.id.btnSaveRecordxml);
btnSaveRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {//SAVE
String szSpecies = edSpeciesLookup.getText().toString();
if(szSpecies.matches("")){//checks to see if species field is empty...
///nothing happens...
}else{db.execSQL("INSERT INTO speciesLookupDb (species) VALUES ('"+szSpecies+"')");
resetForm();
}
}
});
btnLastRecord=(Button)popUpView.findViewById(R.id.btnLastRecordxml);
btnLastRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor c = db.rawQuery("SELECT * FROM speciesLookupDb WHERE _id = (SELECT MAX(_id) FROM speciesLookupDb)",null);
if (c.moveToFirst()){////a record exists (table is not blank)
edSpeciesLookupRowid.setText(c.getString(0));
edSpeciesLookup.setText(c.getString(1));
}else{//no record here...table is blank.
}
}
});
}
}
如果您想在触摸屏幕中的任何位置时关闭弹出窗口
,则必须在弹出窗口的设置内容视图
之后和显示位置
之前添加这些行
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.any_drawable);
popup.setBackgroundDrawable(new BitmapDrawable(getResources(),
bitmap));
popup.setOutsideTouchable(true);
popup.setFocusable(true);
美好的这正是我一直在寻找的解决方案。谢谢。你还没有初始化那些
EditText
,这就是为什么让NPE检查编辑并用正确的ID替换那些ID。你必须初始化你想在Java代码中使用的所有字段,否则你会得到NPE。让我们…@NNE你有兴趣回答另一个问题吗?感谢Silambarasan的周到回答。我对对话框的印象是,它们是相当结构化的,可能无法作为小型数据库浏览器使用,这正是我的意图。我在问题的底部添加了一个草稿弹出窗口的屏幕截图。再次感谢您的时间和回答。顺致敬意,