Android中带自定义微调器的复选框和textview

Android中带自定义微调器的复选框和textview,第1张

概述我在微调框中有7个项目.我创建了带有textview和复选框的微调框.我想做以下逻辑.能否请你给我任何建议:privatestaticfinalString[]select_qualification={"SelectQualification","10th/Below","12th","Diploma","UG","PG&quo

我在微调框中有7个项目.我创建了带有textvIEw和复选框的微调框.我想做以下逻辑.

能否请你给我任何建议:

private static final String[] select_qualification = {        "Select Qualification", "10th / Below", "12th", "Diploma", "UG",        "PG", "Phd" };

如果我将检查UG复选框,它将自动检查状态10th / Below,12th,Diploma的复选框,并且还禁用PG和phd的复选框的选中状态.

如果选中任何复选框,则表示上一项复选框自动需要选中状态,下一项复选框自动需要取消选中状态.

现在这是我的适配器文件:

public class CustomAdapter extends ArrayAdapter<String> {    public CustomAdapter(Context context, int resourceID, String[] objects) {        super(context, resourceID, objects);        // Todo auto-generated constructor stub    }    @OverrIDe    public VIEw getDropDownVIEw(int position, VIEw convertVIEw,            VIEwGroup parent) {        return getCustomVIEw(position, convertVIEw, parent);    }    @OverrIDe    public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {        return getCustomVIEw(position, convertVIEw, parent);    }    public VIEw getCustomVIEw(int position, VIEw convertVIEw,            VIEwGroup parent) {        LayoutInflater inflater = getLayoutInflater();        VIEw row = inflater.inflate(R.layout.spinner_item, parent, false);        final TextVIEw label = (TextVIEw) row                .findVIEwByID(R.ID.spinneritemqualification);        label.setText(select_qualification[position]);        final CheckBox checkBoxbutton = (CheckBox) row                .findVIEwByID(R.ID.checkBoxquali);        if ((position == 0)) {            checkBoxbutton.setVisibility(VIEw.INVISIBLE);        }        checkBoxbutton.setonCheckedchangelistener(new OnCheckedchangelistener() {             @OverrIDe             public voID onCheckedChanged(Compoundbutton buttonVIEw, boolean isChecked) {                 // Todo auto-generated method stub                 System.out.println("position "+ isChecked);                // Integer pos = (Integer) buttonVIEw.getTag();                  int getposition = (Integer) buttonVIEw.getTag();                   Toast.makeText(getApplicationContext(),                        "CliCKED:" + getposition, Toast.LENGTH_LONG)                        .show();                 if (isChecked) {                     checkBoxbutton.setChecked(true);                 }                 else {                     checkBoxbutton.setChecked(false);                 }             }         });

解决方法:

我试图实现你想要的.这是演示.

您可以根据需要进行修改.

定制适配器:

import androID.content.Context;import androID.util.Log;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ArrayAdapter;import androID.Widget.CheckBox;import androID.Widget.Compoundbutton;import androID.Widget.TextVIEw;import java.util.ArrayList;import java.util.List;public class MyAdapter extends ArrayAdapter<StateVO> {    private Context mContext;    private ArrayList<StateVO> ListState;    private MyAdapter myAdapter;    private boolean isFromVIEw = false;    public MyAdapter(Context context, int resource, List<StateVO> objects) {        super(context, resource, objects);        this.mContext = context;        this.ListState = (ArrayList<StateVO>) objects;        this.myAdapter = this;    }    @OverrIDe    public VIEw getDropDownVIEw(int position, VIEw convertVIEw,                                VIEwGroup parent) {        return getCustomVIEw(position, convertVIEw, parent);    }    @OverrIDe    public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {        return getCustomVIEw(position, convertVIEw, parent);    }    public VIEw getCustomVIEw(final int position, VIEw convertVIEw,                              VIEwGroup parent) {        final VIEwHolder holder;        if (convertVIEw == null) {            LayoutInflater layoutInflator = LayoutInflater.from(mContext);            convertVIEw = layoutInflator.inflate(R.layout.spinner_item, null);            holder = new VIEwHolder();            holder.mTextVIEw = (TextVIEw) convertVIEw                    .findVIEwByID(R.ID.text);            holder.mCheckBox = (CheckBox) convertVIEw                    .findVIEwByID(R.ID.checkBox);            convertVIEw.setTag(holder);        } else {            holder = (VIEwHolder) convertVIEw.getTag();        }        holder.mTextVIEw.setText(ListState.get(position).getTitle());        // To check weather checked event fire from getvIEw() or user input        isFromVIEw = true;        holder.mCheckBox.setChecked(ListState.get(position).isSelected());        isFromVIEw = false;        if ((position == 0)) {            holder.mCheckBox.setVisibility(VIEw.INVISIBLE);        } else {            holder.mCheckBox.setVisibility(VIEw.VISIBLE);        }        holder.mCheckBox.setTag(position);        holder.mCheckBox.setonCheckedchangelistener(new Compoundbutton.OnCheckedchangelistener() {            @OverrIDe            public voID onCheckedChanged(Compoundbutton buttonVIEw, boolean isChecked) {                int getposition = (Integer) buttonVIEw.getTag();                if (!isFromVIEw) {                    Log.e("isFromVIEw", !isFromVIEw + "");                    if (getposition == 4) { // cam make a rule according to it for any position                        if (!ListState.get(4).isSelected()) {                            if (isChecked) {                                for (int i = 0; i < ListState.size(); i++) {                                    if (i < 4) {                                        ListState.get(i).setSelected(true);                                    } else if (i == 4) {                                        ListState.get(i).setSelected(true);                                    } else {                                        ListState.get(i).setSelected(false);                                    }                                }                            }                        } else {                            if (!isChecked) {                                for (int i = 0; i < ListState.size(); i++) {                                    if (i < 4) {                                        ListState.get(i).setSelected(false);                                    } else if (i == 4) {                                        ListState.get(i).setSelected(false);                                    }                                }                            }                        }                        myAdapter.notifyDataSetChanged();                    }                }            }        });        return convertVIEw;    }    private class VIEwHolder {        private TextVIEw mTextVIEw;        private CheckBox mCheckBox;    }}

处理复选框状态的基本VO:

public class StateVO {    private String Title;    private boolean selected;    public String getTitle() {        return Title;    }    public voID setTitle(String Title) {        this.Title = Title;    }    public boolean isSelected() {        return selected;    }    public voID setSelected(boolean selected) {        this.selected = selected;    }}

微调项目布局:

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content">    <TextVIEw        androID:ID="@+ID/text"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:gravity="center_vertical"        androID:text="text"        androID:textAlignment="gravity" />    <CheckBox        androID:ID="@+ID/checkBox"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_alignParentEnd="true"        androID:layout_alignParentRight="true" /></relativeLayout>

您活动中的微调器:

 <Spinner        androID:ID="@+ID/spinner"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_centerHorizontal="true"        androID:layout_margintop="10dp" />

在微调器中设置数据的方法:

private voID setSpinnerData() {        final String[] select_qualification = {                "Select Qualification", "10th / Below", "12th", "Diploma", "UG",                "PG", "Phd"};        Spinner spinner = (Spinner) findVIEwByID(R.ID.spinner);        ArrayList<StateVO> ListVOs = new ArrayList<>();        for (int i = 0; i < select_qualification.length; i++) {            StateVO stateVO = new StateVO();            stateVO.setTitle(select_qualification[i]);            stateVO.setSelected(false);            ListVOs.add(stateVO);        }        MyAdapter myAdapter = new MyAdapter(MainActivity.this, 0,                ListVOs);        spinner.setAdapter(myAdapter);    }

享受编码.!!

总结

以上是内存溢出为你收集整理的Android中带自定义微调器的复选框和textview全部内容,希望文章能够帮你解决Android中带自定义微调器的复选框和textview所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1107931.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存