这篇文章说的是popupwindow的一个弹出和消失,其实也没有什么难度主要是有几个小的点子害怕以后忘记,所以写了篇博客,代码也很短就不介绍了,看看就懂了。
代码:
package com.jk.popupwindowdemo;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
/**
*
* @author jk 这段代码说的是popupwindow的使用,我们通过点击textview来弹出一个popupwindow,然后通过点击
* popupwindow外部来使popupwindow消失。
*
*/
public class MainActivity extends Activity {
// 声明点击的TextView
TextView tv;
// 声明弹出的ListView
ListView lv;
// 声明数据源
ArrayList<String> data;
PopupWindow pw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化
init();
}
private void init() {
// 实例化数据源
data = new ArrayList<String>();
// 为数据源添加数据
for (int i = 0; i < 5; i++) {
data.add("item" + i);
}
// 绑定点击的TextView
tv = (TextView) findViewById(R.id.tv_main);
// 为绑定的TextView设定监听事件
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (pw == null) {
showPopupWindow();
System.out.println("1");
} else {
System.out.println("2");
pw.dismiss();
pw = null;
}
}
private void showPopupWindow() {
// 将配置文件转化为一个view
View contentView = LayoutInflater.from(MainActivity.this)
.inflate(R.layout.lv_popupwindow, null, false);
// 在我们转化的view里面找到listview
lv = (ListView) contentView.findViewById(R.id.lv_pop);
// 为lv设置监听事件
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(MainActivity.this, data.get(arg2),
Toast.LENGTH_SHORT).show();
}
});
// 初始化adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_list_item_1,
android.R.id.text1, data);
// 为listview设置适配器
lv.setAdapter(adapter);
// 初始化弹出窗口
pw = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, true);
pw.setFocusable(true);
// 为返回的作为contentview的内容的view设置监听
pw.getContentView().setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (pw != null) {
pw.dismiss();
pw = null;
}
return false;
}
});
// 显示popupwindow
pw.showAsDropDown(tv);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lv_pop"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>