EditText既可以输入编辑也可以下拉选择 EditText +ListPopupWindow
程序员文章站
2022-05-30 23:33:31
...
1、效果展示
2、布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lum.mylistpopupwindow.MainActivity">
<EditText
android:id="@+id/edit_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账户:"
android:textSize="30dp"
android:layout_marginTop="60dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
</LinearLayout>
3、功能实现
package com.example.lum.mylistpopupwindow;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListPopupWindow;
public class MainActivity extends AppCompatActivity implements View.OnFocusChangeListener {
private EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditText = (EditText) findViewById(R.id.edit_id);
mEditText.setOnFocusChangeListener(this); //对edit 进行焦点监听
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
showListPopulWindow(); //调用显示PopuWindow 函数
}
}
private void showListPopulWindow() {
final String[] list = {"1", "2", "3","4","5","6","7","8","9","0"};//要填充的数据
final ListPopupWindow listPopupWindow;
listPopupWindow = new ListPopupWindow(this);
listPopupWindow.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list));//用android内置布局,或设计自己的样式
listPopupWindow.setAnchorView(mEditText);//以哪个控件为基准,在该处以mEditText为基准
listPopupWindow.setModal(true);
listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {//设置项点击监听
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
mEditText.setText(list[i]);//把选择的选项内容展示在EditText上
listPopupWindow.dismiss();//如果已经选择了,隐藏起来
}
});
listPopupWindow.show();//把ListPopWindow展示出来
}
}
文章参考:
1、【Android】EditText下拉菜单ListPopupWindow
https://blog.csdn.net/crab0314/article/details/79608705
2、EditText+ListPopupWindow实现可编辑的下拉列表
https://blog.csdn.net/u014293306/article/details/52402537
3、Android开发笔记(一百二十一)列表弹窗PopupMenu和ListPopupWindow
https://www.2cto.com/kf/201608/543275.html
4、Android自定义PopupWindow显示在控件上方或者下方
https://www.cnblogs.com/woaixingxing/p/5563171.html
5、PopupWindow学习之弹出方向(一)
https://blog.csdn.net/final1992/article/details/50487435
6、android PopupWindow在控件的各个方向上的显示
https://blog.csdn.net/kujing823/article/details/53100876