Android中EditText+图片+下拉框
程序员文章站
2022-07-14 18:08:09
...
效果说明:EditText内部右侧加图片,点击EditText显示下拉框
<EditText
android:id="@+id/et"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:drawableRight="@mipmap/jiantou_down" //内部右侧添加图片
android:background="@drawable/yuanjiao" //背景边框样式
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:focusable="false" //禁止输入
android:cursorVisible="true"
android:textCursorDrawable="@null" //不显示光标
android:text="北京机场"/>
drawable/yuanjiao.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/edit_text_color2" /> //内部颜色
<corners android:topLeftRadius="1dp"
android:topRightRadius="1dp"
android:bottomRightRadius="1dp"
android:bottomLeftRadius="1dp"/> //边框角(直越大圆角幅度越大)
<stroke android:width="1dp" android:color="@color/edit_text_color" /> //边框颜色
</shape>
绘制PopupWindow下拉框
res/anim/dialog_top_in.xml(展示动画)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" >
<translate
android:duration="200"
android:fromYDelta="-1"
android:toYDelta="0" />
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="200"/>
</set>
res/anim/dialog_top_out.xml(退出动画)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--%p指相对于父容器-->
<translate
android:duration="200"
android:fromYDelta="0"
android:toYDelta="-1" />
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="200"/>
</set>
res/values/styles.xml中(切记别写反了)
<style name="AnimTop">
<item name="android:windowEnterAnimation">@anim/dialog_top_in</item>
<item name="android:windowExitAnimation">@anim/dialog_top_out</item>
</style>
自定义PopupWindow
package com.test;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import com.test;
import com.test.StationPoupAdapter;
import java.util.ArrayList;
import java.util.List;
public class StationPopupWindow extends PopupWindow {
private Context context;
private View mMenuView;
public StationPopupWindow(Context context, int width, int height) {
super(context);
this.context = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.dialog_station, null);
// 设置SelectPicPopupWindow的View
this.setContentView(mMenuView);
// 设置SelectPicPopupWindow弹出窗体的宽
if (width==0){
width = ViewGroup.LayoutParams.MATCH_PARENT;
}
// this.setHeight(height);
// this.setWidth(width);
// 设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
// 设置SelectPicPopupWindow弹出窗体动画效果
this.setAnimationStyle(R.style.AnimTop);
// 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0x00000000);
// 设置SelectPicPopupWindow弹出窗体的背景
this.setBackgroundDrawable(dw);
ListView lv_dialog_station = mMenuView.findViewById(R.id.lv_dialog_station);
List<String> list = new ArrayList();
list.add("北京机场");
list.add("上海机场");
list.add("香港机场");
list.add("澳门机场");
StationPoupAdapter adapter = new StationPoupAdapter(context,list);
lv_dialog_station.setAdapter(adapter);
lv_dialog_station.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
listener.onItemClickListener(list.get(i));
dismiss();
}
});
}
OnListener listener;
public void setOnItemClick(OnListener listener){
this.listener = listener;
}
public interface OnListener{
void onItemClickListener(String string);
}
}
Activity中使用
StationPopupWindow station;
private void showStationPopupWindow(EditText v, Activity activity) {
if (station!=null&&station.isShowing()){
return;
}
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
width = width * 5 / 6;
int height = activity.getWindowManager().getDefaultDisplay().getHeight();
height = height * 3 / 5;
station = new StationPopupWindow(activity, width, height);
darkenBackground(1.0f,activity);
//x、y指的是偏移量坐标点
int[] location = new int[2];
v.getLocationOnScreen(location);
//指定下拉框的位置(位于EditText的下方)
station.showAtLocation(v, Gravity.NO_GRAVITY, location[0],
location[1] + v.getHeight());
station.setOnItemClick(new StationPopupWindow.OnListener() {
@Override
public void onItemClickListener(String string) {
v.setText(string);
}
});
station.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
darkenBackground(1f,activity);
chooseDownORTop(false);
}
});
}
//改变背景颜色
private void darkenBackground(Float bgcolor, Activity activity) {
//背景灰色效果
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.alpha = bgcolor;//0.0-1.0透明度
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
activity.getWindow().setAttributes(lp);
}
EditText引用
EditText et_station = findViewById(R.id.et_station);
Drawable drawable = getResources().getDrawable(R.mipmap.jiantou_down);
// 这一步必须要做,否则不会显示.
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//在代码中设置drawableRight(Editext内图片位置)
et_station.setCompoundDrawables(null,null,drawable,null);
et_station.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showStationPopupWindow((EditText) view, (Activity) context);
}
});
上一篇: Objective-C里的字符串