觉得博文有用,请点赞,请留言,请关注,谢谢!~
老规矩,先上GIF动态图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧:
项目下载: http://download.csdn.net/detail/iwanghang/9716587
MainActivity.java:
package com.iwanghang.popupwindow; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private EditText et_input; /** * PopupWindow * 一个弹出窗口,可以用来显示一个任意的视图。弹出窗口是一个浮动的容器出现在顶部的当前活动。 */ private PopupWindow popupWindow; private ListView listView; private MyAdapter myAdapter; private ArrayList<String> msgs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_input = (EditText) findViewById(R.id.et_input); et_input.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (popupWindow == null){ popupWindow = new PopupWindow(MainActivity.this); popupWindow.setWidth(et_input.getWidth()); int height = DensityUtil.dip2px(MainActivity.this,200); // dp->px popupWindow.setHeight(height); popupWindow.setContentView(listView); popupWindow.setFocusable(true); // 设置焦点 } popupWindow.showAsDropDown(et_input,0,0); } }); listView = new ListView(this); listView.setBackgroundResource(R.color.colorMy); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { // 1、得到数据 String msg = msgs.get(i); // 2、设置输入框 et_input.setText(msg); if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); popupWindow = null; } } }); // 准备数据 msgs = new ArrayList<>(); for (int i = 0; i <50 ; i++) { msgs.add(i+".iwanghang"); } myAdapter = new MyAdapter(); listView.setAdapter(myAdapter); } /** * listView的适配器 */ class MyAdapter extends BaseAdapter{ @Override public int getCount() { return msgs.size(); } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder viewHolder; if (view == null) { view = View.inflate(MainActivity.this, R.layout.item_main, null); viewHolder = new ViewHolder(); viewHolder.tv_msg = (TextView) view.findViewById(R.id.tv_msg); viewHolder.iv_delete = (ImageView) view.findViewById(R.id.iv_delete); view.setTag(viewHolder); }else { viewHolder = (ViewHolder) view.getTag(); } // 根据位置得到数据 final String msg = msgs.get(i); viewHolder.tv_msg.setText(msg); // 设置删除 viewHolder.iv_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 1、从集合删除 msgs.remove(msg); // 2、刷新ui 适配器刷新 myAdapter.notifyDataSetChanged(); } }); return view; } } static class ViewHolder{ TextView tv_msg; ImageView iv_delete; } }DensityUtil.java:
package com.iwanghang.popupwindow; import android.content.Context; public class DensityUtil { /** * 根据手机的分辨率从 dip 的单位 转成为 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } }activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.iwanghang.popupwindow.MainActivity"> <EditText android:id="@+id/et_input" android:hint="请输入内容..." android:ellipsize="middle" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv_down_arrow" android:src="@drawable/down_arrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignRight="@+id/et_input" android:layout_alignEnd="@+id/et_input" /> </RelativeLayout>item_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <ImageView android:src="@drawable/user" android:layout_gravity="center" android:gravity="center" android:layout_width="30dp" android:layout_height="30dp" /> <TextView android:id="@+id/tv_msg" android:text="123456" android:layout_gravity="center" android:gravity="center" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv_delete" android:src="@drawable/delete" android:layout_gravity="center" android:gravity="center" android:layout_width="25dp" android:layout_height="25dp" /> </LinearLayout>
转载请注明出处: http://blog.csdn.net/iwanghang/article/details/53763364
欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式
微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com
沈阳或周边城市公司有意开发Android,请与我联系
联系方式
微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com
项目下载: http://download.csdn.net/detail/iwanghang/9716587
觉得博文有用,请点赞,请留言,请关注,谢谢!~