Android 带有可变文本的弹出窗口 Window

Android Popup Window with Variable Text

弹出框有点麻烦,android 的新手,不知道该怎么做。我想要一个显示基于 public 变量的特定文本的弹出窗口,因此当单击按钮时,将检查该变量,并根据该变量是什么在 textView 中显示适当的文本。

popup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

        <TextView
            android:id="@+id/text_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Close"
            android:id="@+id/button1"/>     

</LinearLayout>

mainActivity.java

 Button pubtn = (Button)findViewById(R.id.popupOpen);
 pubtn.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  

    View puView = layoutInflater.inflate(R.layout.popup, null);  
    PopupWindow puWindow = new PopupWindow(
               puView, 
               LayoutParams.WRAP_CONTENT,  
               LayoutParams.WRAP_CONTENT);  

             Button btnExit = (Button)puView.findViewById(R.id.button1);
             btnExit.setOnClickListener(new Button.OnClickListener(){

     @Override
     public void onClick(View v) {
      puWindow.dismiss();
     }});
             puWindow.showAsDropDown(pubtn, 0, 0);  
   }});

任何有关如何进行此操作的帮助或指导将不胜感激

你可以使用这个例子:

LayoutInflater inflater = (LayoutInflater) YourActivity.this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.popup_xml,
            (ViewGroup) findViewById(R.id.popup_element));
    popupWindow = new PopupWindow(layout, 300, 190, true);

    popupText = (TextView) layout.findViewById(R.id.popupText);
    popupText.setText(yourString);