当时需要一个帅选框,让用户选择性别,年龄段,选择是否让别人看见自己等。
在网上各中搜,结果还是没什么答案,都是一些很丑很low的东西,我的内心是拒绝的。
这时候在群里小小问了一下 46523908,颜哥直接就给远程写了一堆代码,让我受益至今!更加坚信在这条道上并不孤独的我只要努力总会明白的更多。
算了,直接上代码,思路一看就明白了,这就是个逻辑的问题。
xml文件的代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="@dimen/threeHundred" android:layout_height="@dimen/fivehundred" android:background="@drawable/dialogshape" android:orientation="vertical"> <TextView android:layout_weight="1" android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="0dp" android:background="@color/white" android:gravity="center_vertical" android:paddingLeft="@dimen/fifteen" android:text="@string/shuaixuan" android:textColor="@color/eightzero" android:textSize="@dimen/twozero" /> <TextView android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp" android:background="@color/white" android:gravity="center_vertical" android:paddingLeft="@dimen/fifteen" android:text="@string/xingbie" android:textColor="@color/eightzero" android:textSize="@dimen/fifteen" /> <LinearLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp" android:background="@color/white" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <TextView android:id="@+id/text_sex_man" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawablePadding="5dp" android:gravity="center_vertical" android:padding="12dp" android:text="@string/nan" android:textColor="@color/eightzero" android:drawableRight="@mipmap/weianxia" android:textSize="@dimen/fifteen" /> <!--android:drawableRight="@mipmap/simple_app"--> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <TextView android:id="@+id/text_sex_women" android:layout_width="wrap_content" android:padding="12dp" android:layout_height="wrap_content" android:drawablePadding="5dp" android:gravity="center_vertical" android:text="@string/nv" android:drawableRight="@mipmap/weianxia" android:textColor="@color/eightzero" android:textSize="@dimen/fifteen" /> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <TextView android:id="@+id/text_sex_all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="63dp" android:layout_marginRight="63dp" android:background="@color/white" android:drawablePadding="5dp" android:gravity="center_vertical" android:padding="12dp" android:drawableRight="@mipmap/weianxia" android:text="@string/all" android:textColor="@color/eightzero" android:textSize="@dimen/fifteen" /> </LinearLayout> <TextView android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp" android:gravity="center_vertical" android:paddingLeft="@dimen/fifteen" android:text="@string/age" android:textColor="@color/eightzero" android:textSize="@dimen/fifteen" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3" android:background="@color/white"> <TextView android:id="@+id/text_age_20" android:layout_width="wrap_content" android:layout_height="@dimen/fifty" android:gravity="center" android:text="@string/ten_towten" android:textColor="@color/eightzero" android:textSize="@dimen/fifteen" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/text_age_40" android:layout_toEndOf="@+id/text_age_40" android:layout_marginLeft="32dp" android:layout_marginStart="32dp" /> <TextView android:id="@+id/text_age_10" android:layout_width="wrap_content" android:layout_height="@dimen/fifty" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginLeft="48dp" android:layout_marginStart="48dp" android:gravity="center" android:text="@string/zero_ten" android:textColor="@color/eightzero" android:textSize="@dimen/fifteen" /> <TextView android:id="@+id/text_age_40" android:layout_width="wrap_content" android:layout_height="@dimen/fifty" android:layout_alignLeft="@+id/text_age_10" android:layout_alignStart="@+id/text_age_10" android:layout_below="@+id/text_age_10" android:gravity="center" android:text="@string/four_zero" android:textColor="@color/eightzero" android:textSize="@dimen/fifteen" /> <TextView android:id="@+id/text_age_60" android:layout_width="wrap_content" android:layout_height="@dimen/fifty" android:layout_alignLeft="@+id/text_age_30" android:layout_alignStart="@+id/text_age_30" android:layout_below="@+id/text_age_30" android:gravity="center" android:text="@string/six_zero" android:textColor="@color/eightzero" android:textSize="@dimen/fifteen" /> <TextView android:id="@+id/text_age_50" android:layout_width="wrap_content" android:layout_height="@dimen/fifty" android:layout_alignLeft="@+id/text_age_20" android:layout_alignStart="@+id/text_age_20" android:layout_below="@+id/text_age_20" android:gravity="center" android:text="@string/five_zero" android:textColor="@color/eightzero" android:textSize="@dimen/fifteen" /> <TextView android:id="@+id/text_age_30" android:layout_width="wrap_content" android:layout_height="@dimen/fifty" android:gravity="center" android:text="@string/three_zero" android:textColor="@color/eightzero" android:textSize="@dimen/fifteen" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/text_age_20" android:layout_toEndOf="@+id/text_age_20" android:layout_marginLeft="48dp" android:layout_marginStart="48dp" /> <TextView android:id="@+id/text_age_all" android:layout_width="wrap_content" android:layout_height="@dimen/fifty" android:layout_alignEnd="@+id/text_age_40" android:layout_alignLeft="@+id/text_age_40" android:layout_alignParentBottom="true" android:layout_alignRight="@+id/text_age_40" android:layout_alignStart="@+id/text_age_40" android:gravity="center" android:text="@string/all" android:textColor="@color/eightzero" android:textSize="@dimen/fifteen" /> </RelativeLayout> <TextView android:layout_width="match_parent" android:layout_height="@dimen/fifty" android:background="@color/white" android:gravity="center_vertical" android:paddingLeft="@dimen/fifteen" android:text="@string/isnot" android:textColor="@color/eightzero" android:textSize="@dimen/fifteen" /> <RelativeLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"> <TextView android:id="@+id/text_yes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_centerVertical="true" android:layout_marginLeft="47dp" android:layout_marginStart="47dp" android:background="@color/white" android:gravity="center_vertical" android:text="@string/yes" android:drawableRight="@mipmap/weianxia" android:textColor="@color/eightzero" android:textSize="@dimen/fifteen" /> <TextView android:id="@+id/text_no" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/text_yes" android:layout_alignBottom="@+id/text_yes" android:layout_marginLeft="109dp" android:layout_marginStart="109dp" android:layout_toEndOf="@+id/text_yes" android:layout_toRightOf="@+id/text_yes" android:background="@color/white" android:gravity="center_vertical" android:text="@string/no" android:drawableRight="@mipmap/weianxia" android:textColor="@color/eightzero" android:textSize="@dimen/fifteen" /> </RelativeLayout> <RelativeLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"> <TextView android:id="@+id/text_affirm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="100dp" android:layout_marginRight="100dp" android:background="@color/white" android:gravity="center_vertical" android:text="@string/qveding" android:textColor="@color/eightzero" android:textSize="@dimen/fifteen" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/text_cancel" android:layout_toStartOf="@+id/text_cancel" /> <TextView android:id="@+id/text_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="85dp" android:layout_marginRight="85dp" android:background="@color/white" android:gravity="center_vertical" android:text="@string/qvxiao" android:textColor="@color/eightzero" android:textSize="@dimen/fifteen" android:layout_alignBaseline="@+id/text_affirm" android:layout_alignBottom="@+id/text_affirm" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout> </LinearLayout>
代码
package com.omi.ui.dialog; import android.app.Dialog; import android.content.Context; import android.graphics.drawable.Drawable; import android.support.v4.content.ContextCompat; import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.TextView; import com.omi.R; import com.omi.utils.ToastUtil; import java.util.ArrayList; /** * Created by lujiantao on 2017/4/18 0018. */ public class NearbyDialog implements View.OnClickListener { private Context context; private Dialog dialog; private ArrayList<TextView> sexArrayList; private ArrayList<TextView> ageArrayList; private ArrayList<TextView> selectArrayList; private Drawable drawable1, drawable2; private String sex = ""; private String age = ""; private String select = ""; private OnClickListener onClickListener; public NearbyDialog(Context context) { this.context = context; sexArrayList = new ArrayList<>(); ageArrayList = new ArrayList<>(); selectArrayList = new ArrayList<>(); drawable1 = ContextCompat.getDrawable(context, R.mipmap.anxia); drawable2 = ContextCompat.getDrawable(context, R.mipmap.weianxia); drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight()); drawable2.setBounds(0, 0, drawable2.getMinimumWidth(), drawable2.getMinimumHeight()); } private TextView text_sex_man, text_sex_women, text_sex_all; private TextView text_age_10, text_age_20, text_age_30, text_age_40, text_age_50, text_age_60, text_age_all; private TextView text_yes, text_no; public void init() { dialog = new Dialog(context); dialog.setContentView(R.layout.dialog_near); text_sex_man = (TextView) dialog.findViewById(R.id.text_sex_man); text_sex_women = (TextView) dialog.findViewById(R.id.text_sex_women); text_sex_all = (TextView) dialog.findViewById(R.id.text_sex_all); text_age_10 = (TextView) dialog.findViewById(R.id.text_age_10); text_age_20 = (TextView) dialog.findViewById(R.id.text_age_20); text_age_30 = (TextView) dialog.findViewById(R.id.text_age_30); text_age_40 = (TextView) dialog.findViewById(R.id.text_age_40); text_age_50 = (TextView) dialog.findViewById(R.id.text_age_50); text_age_60 = (TextView) dialog.findViewById(R.id.text_age_60); text_age_all = (TextView) dialog.findViewById(R.id.text_age_all); text_yes = (TextView) dialog.findViewById(R.id.text_yes); text_no = (TextView) dialog.findViewById(R.id.text_no); dialog.findViewById(R.id.text_affirm).setOnClickListener(this); dialog.findViewById(R.id.text_cancel).setOnClickListener(this); addArrayList(); setAllClick(); } public void show(){ if (dialog == null){ init(); } Window window = dialog.getWindow(); window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); dialog.show(); } public void setOnClickListener(OnClickListener onClickListener) { this.onClickListener = onClickListener; } private void addArrayList() { //添加性别选择数组 sexArrayList.add(text_sex_man); sexArrayList.add(text_sex_women); sexArrayList.add(text_sex_all); //添加年龄数组 ageArrayList.add(text_age_10); ageArrayList.add(text_age_20); ageArrayList.add(text_age_30); ageArrayList.add(text_age_40); ageArrayList.add(text_age_50); ageArrayList.add(text_age_60); ageArrayList.add(text_age_all); selectArrayList.add(text_yes); selectArrayList.add(text_no); } /** * 改变性别选择状态 * * @param text */ private void allSex(TextView text) { for (TextView tv : sexArrayList) { tv.setCompoundDrawables(null, null, drawable2, null); } text.setCompoundDrawables(null, null, drawable1, null); } /** * 改变年龄选择状态 * * @param text 选中的View */ private void allAge(TextView text) { for (TextView tv : ageArrayList) { tv.setTextColor(ContextCompat.getColor(context, R.color.mask_color)); } text.setTextColor(ContextCompat.getColor(context, R.color.blue)); } private void allSelect(TextView text){ for (TextView tv : selectArrayList) { tv.setCompoundDrawables(null, null, drawable2, null); } text.setCompoundDrawables(null,null,drawable1,null); } private void setAllClick() { for (TextView tv : sexArrayList) { tv.setOnClickListener(this); } for (TextView tv : ageArrayList) { tv.setOnClickListener(this); } for (TextView tv : selectArrayList) { tv.setOnClickListener(this); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.text_affirm: if (TextUtils.isEmpty(sex)) { ToastUtil.showToast("请选择性别"); return; } if (TextUtils.isEmpty(age)) { ToastUtil.showToast("请选择年龄"); return; } if (TextUtils.isEmpty(select)){ ToastUtil.showToast("请选择是否对他人可见"); return; } if (onClickListener != null) { onClickListener.onClick(sex, age, select); } dialog.dismiss(); break; case R.id.text_cancel: dialog.dismiss(); break; case R.id.text_sex_man: sex = "男"; allSex(text_sex_man); break; case R.id.text_sex_women: sex = "女"; allSex(text_sex_women); break; case R.id.text_sex_all: sex = "全部"; allSex(text_sex_all); break; case R.id.text_age_10: age = "0-10"; allAge(text_age_10); break; case R.id.text_age_20: age = "10-20"; allAge(text_age_20); break; case R.id.text_age_30: age = "20-30"; allAge(text_age_30); break; case R.id.text_age_40: age = "30-40"; allAge(text_age_40); break; case R.id.text_age_50: age = "40-50"; allAge(text_age_50); break; case R.id.text_age_60: age = "50-60"; allAge(text_age_60); break; case R.id.text_age_all: age = "allofthem"; allAge(text_age_all); break; case R.id.text_yes: select = "是"; allSelect(text_yes); break; case R.id.text_no: select = "否"; allSelect(text_no); break; } } public interface OnClickListener { void onClick(String sex, String age, String select); } }
所有代码可以直接copy使用,只是让不知道的同学知道思路 ,具体难看了就需要自己去深究下这里就不多做介绍了。
喜欢的朋友请多多分享自己的存货给大家。
QQ: 532245792 涛,冷静点