简体   繁体   中英

Access textview on another layout

I have made a popup, which works fine, but I'd like to write to the TextView on it. Using the standard method doesn't work (just crashes it), despite Eclipse finding the TextView's id and not showing any problems. The popup is in another XML layout.

Generates the popup

    public PopupWindow pw;

public void popUpShow() {
    LayoutInflater inflater = (LayoutInflater)
           this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    pw = new PopupWindow(
           inflater.inflate(R.layout.popup, null, false), 
           400, 
           600, 
           true);
            pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);
}

Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_layout"
android:orientation="vertical"
android:padding="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#AAA"
>

<TextView
    android:id="@+id/popupOut"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text=""
/>

<Button
    android:id="@+id/popupclose"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/close"
    android:onClick="popUpHide" />

</LinearLayout>

Before creating your PopupWindow, keep a reference to the view Inflated

ViewGroup v = (ViewGroup)inflater.inflate(R.layout.popup, null, false);

pw = new PopupWindow(
       v, 
       400, 
       600, 
       true);
        pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);

TextView view = (TextView)v.findViewById(R.id.textView1);

If the textView is in a dialog, then the view id should be retrieved by

Dialog dialog = new Dialog(this);
TextView view = (TextView)dialog.findViewById(R.id.textView1);

Try

 public void popUpShow() {
    LayoutInflater inflater = (LayoutInflater)
           this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout t = (LinearLayout) inflater.inflate(R.layout.item1, null, false);
    PopupWindow pw = new PopupWindow(
           t, 
           400, 
           600, 
           true);

    pw.showAtLocation(t.findViewById(R.id.main), Gravity.CENTER, 0, 0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM