当从单独的类中调用PopupWindow方法时,如何从xml文件定义PopupWindow中的布局 [英] How to define layout in a PopupWindow from an xml file, when PopupWindow method is called from a separate class

查看:87
本文介绍了当从单独的类中调用PopupWindow方法时,如何从xml文件定义PopupWindow中的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当从单独的类中调用PopupWindow方法时,我希望能够从xml文件定义PopupWindow中的布局.下面的代码根据需要工作,除了布局是从java文件而不是从xml文件提取的.在这种情况下,或者如果可能的话,我不知道如何正确访问xml布局并在PopupWindow中实现.建议和具体建议,不胜感激.谢谢.

I'd like to be able to define layout in a PopupWindow from an xml file when the PopupWindow method is called from a separate class. Below code works as needed EXCEPT that layout is pulled from the java file and not the xml file.I do not know how to properly access xml layout and implement in the PopupWindow in this situation, or if this is possible. Advice and specific suggestions are appreciated. Thanks.

showPopup.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();    
        }
    });
  }
}

Tab3Fragment.java

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>

更新(11月12日,1835年): 这是一个草稿弹出窗口.该应用程序中将有几个为用户提供服务的可编辑下拉字段.

Update (1835, Dec-11): This is a draft popup. There will be several of these servicing user editable dropdown fields in the application.

推荐答案

像这样更改showPopup

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.        
            }
        }
    });
  }
}

如果要在触摸屏幕上的任何位置时取消popup,则必须在弹出窗口setContentView之后和showAtLocation

If you want to dismiss popup on touching anywhere in screen then you have to add these lines after setContentView of popup and before showAtLocation

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.any_drawable);
popup.setBackgroundDrawable(new BitmapDrawable(getResources(),
                    bitmap));
popup.setOutsideTouchable(true);
popup.setFocusable(true);

这篇关于当从单独的类中调用PopupWindow方法时,如何从xml文件定义PopupWindow中的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆