代码如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// 生成ListView
ListView listview = new ListView(MainActivity.this);
String strs = new String;
strs = "asfsafdasdf";
strs = "桂 地要仍地沃尔夫仍";
strs = "asfsafdasdf";
ArrayAdapter<String> adapter=new
ArrayAdapter<String>(MainActivity.this,R.layout.common_poplist_item_single_textview,R.id.content,strs);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.common_poplist_item_single_textview, strs);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
System.out.println(position);
}
});
PopupWindow window = new PopupWindow(MainActivity.this);
// 设置宽高
window.setWidth(200);
window.setHeight(120);
// 填充
window.setContentView(listview);
// 外部可部
window.setOutsideTouchable(true);
// 加了下面这行,onItemClick才好用
window.setFocusable(true);
window.showAsDropDown(button1);
}
});
}
经测试,发现2.3的真机上OnItemClick能触发..但是在4.1,4.2的真机上测试无均反应..麻烦各位帮我验证一下是否如此,该如何解决.
PopupWindow ListView OnItemClick 无效
------解决方案--------------------
点下按钮直接显示popwindow就好了
------解决方案--------------------
你把它写到外面试试呀
------解决方案--------------------