2012-03-06 83 views
1

我做了一个弹出窗口,工作正常,但我想写入TextView。尽管Eclipse找到了TextView的id并且没有显示任何问题,但使用标准方法并不行(只是崩溃了)。弹出窗口是另一个XML布局。在另一个布局上访问textview

生成

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

布局

<?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> 
+0

视图可以请你添加一些代码.. – user936414 2012-03-06 05:07:27

回答

1

之前创建PopupWindow,保持对它的引用充气

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); 
+0

正是我在找的东西。谢谢。 – Rhyono 2012-03-06 05:16:06

0

如果TextView的是在一个对话框,然后在视图id应由

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

检索尝试

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); 
} 
弹出
+0

我没有使用对话框。 – Rhyono 2012-03-06 05:12:46

相关问题