有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

另一个布局上的java Access文本视图

我制作了一个弹出窗口,效果很好,但我想在上面写入文本视图。尽管Eclipse找到了TextView的id并且没有显示任何问题,但使用标准方法不起作用(只是使其崩溃)。弹出窗口位于另一个XML布局中

生成弹出窗口

    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:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:id="@+id/popup_layout"
安卓:orientation="vertical"
安卓:padding="10dp"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:background="#AAA"
>

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

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

</LinearLayout>

共 (2) 个答案

  1. # 1 楼答案

    在创建弹出窗口之前,请保留对视图的引用

    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);
    
  2. # 2 楼答案

    如果文本视图位于对话框中,则视图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);
    }