使弹出窗口文本视图可单击



我的活动有一个弹出窗口,弹出窗口包含一些文本视图,我想使该文本可点击。

主要活动.java:

public class ListViewForDeleteContact extends AppCompatActivity {
    ListView myListView;
    protected  void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LayoutInflater layoutInflater=(LayoutInflater)ListViewForDeleteContact.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View dfg= layoutInflater.inflate(R.layout.popupWindow,(ViewGroup)findViewById(R.id.popupId));
                PopupWindow popupWindow=new PopupWindow(dfg,420,300,true);
                popupWindow.showAtLocation(dfg, Gravity.CENTER, 0, 0);
                popupWindow.setOutsideTouchable(true);
            }
        });
    }
}

主.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/trans">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

弹出窗口.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/popupId"
                android:background="#546e7a"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="are you sure"
        android:layout_margin="20dp"
        android:id="@+id/textView7"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="no"
        android:id="@+id/textView8"
        android:layout_below="@+id/textView7"
        android:layout_centerHorizontal="true"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="yes"
        android:textColor="@color/colorAccent"
        android:id="@+id/textView9"
        android:layout_below="@+id/textView7"
        android:layout_alignBottom="@+id/textView8"
        android:layout_alignEnd="@+id/textView7"/>
</RelativeLayout>

主要.xml活动使用弹出窗口.xml为了显示弹出窗口,我想使可点击的文本视图存在于弹出窗口.xml中。

您可以在 dfg View 变量中使用 findViewById 方法访问 TextView。

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    LayoutInflater layoutInflater=(LayoutInflater)ListViewForDeleteContact.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View dfg= layoutInflater.inflate(R.layout.popupWindow,(ViewGroup)findViewById(R.id.popupId));
    TextView textView7 = (TextView) dfg.findViewById(R.id.textView7);
    textView7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // do something when the textview is clicked
        }
    });
    ... // for other textviews
    PopupWindow popupWindow=new PopupWindow(dfg,420,300,true);
    popupWindow.showAtLocation(dfg, Gravity.CENTER, 0, 0);
    popupWindow.setOutsideTouchable(true);
}

最新更新