黑马程序员技术交流社区
标题:
ANDROID PopupWindow的使用
[打印本页]
作者:
woshiku
时间:
2015-9-9 10:56
标题:
ANDROID PopupWindow的使用
package com.dropitem;
import java.util.ArrayList;
import java.util.List;
import com.myinter.MyClickListener;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
public class Main extends Activity implements OnClickListener, MyClickListener {
private EditText ed;
private ImageView image;
private PopupWindow pop = null;
private String[] strs = new String[] {
"first", "second", "third", "fourth", "fifth"
};
Context context;
List<String> list;
private ListView listView;
private ListView listViewOne;
private MyAdapter myAdapter;
private void init(){
context = Main.this;
ed = (EditText)findViewById(R.id.editText1);
image = (ImageView)findViewById(R.id.imageView1);
image.setOnClickListener(this);
}
private void initData(){
list = new ArrayList<String>();
for(int i=0;i<10;i++){
list.add(88888+i+"");
}
}
private void initList(){
listView = new ListView(this);
myAdapter = new MyAdapter(context, list);
myAdapter.setMyClickListener(this);
listView.setAdapter(myAdapter);
}
private void initListOne(){
listViewOne = new ListView(this);
listViewOne.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, strs));
}
private void showDrop(){
if(pop == null){
pop = new PopupWindow(listView, ed.getWidth(), 400);
}
pop.setFocusable(true);
pop.setBackgroundDrawable(new BitmapDrawable());
pop.showAsDropDown(ed, 0,0);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
initData();
initList();
initListOne();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDrop();
}
@Override
public void onChange(MyAdapter adapter, int position,int height) {
// TODO Auto-generated method stub
//Log.e(null, position+"halou");
list.remove(position);
myAdapter.notifyDataSetChanged();
pop.update(ed.getWidth(), height*list.size()>400?400:height*list.size());
}
}
附件中含程序,有需要的同学自行下载
DropItem.rar
2015-9-9 10:55 上传
点击文件名下载附件
1.34 MB, 下载次数: 33
下拉列表
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2