make popupWindow TextView可点击吗?

时间:2016-06-11 16:23:26

标签: java android xml sqlite

我的活动中有一个PopupWindow,PopupWindow包含一些TextView我想让这个文本可点击。请帮助我在android中新建。

main activity.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);

            }
        });
    }
}

main.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>

popupWindow.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>

main.xml活动使用popupWindow.xml显示弹出窗口我想制作popupWindow.xml中存在的可点击TextView。

1 个答案:

答案 0 :(得分:0)

您可以在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);

}