该复选框,TextView的在Android的定制微调 [英] The checkbox and textview with custom spinner in Android

查看:145
本文介绍了该复选框,TextView的在Android的定制微调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有7个项目中的微调框。我已创建了TextView的和复选框微调框。我想要做以下逻辑。

您可以请给我任何建议做:

 私有静态最后的String [] select_qualification = {
        选择资质,第10 /以下,12号,文凭,UG,
        PG,博士学位};
 

如果我会检查UG复选框,它会自动检查状态 10 /以下 12复选框文凭并且禁用复选框的选中状态 PG 博士

如果是检查任何复选框意味着previous项目复选框自动需要检查状态,下一个项目的复选框自动需要取消选中状态。

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

 公共类CustomAdapter扩展ArrayAdapter<字符串> {

    公共CustomAdapter(上下文的背景下,INT RESOURCEID,字符串[]对象){
        超(背景下,RESOURCEID,对象);
        // TODO自动生成构造函数存根
    }

    @覆盖
    公共查看getDropDownView(INT位置,查看convertView,
            ViewGroup中父){
        返回getCustomView(位置,convertView,父母);
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        返回getCustomView(位置,convertView,父母);
    }

    公共查看getCustomView(INT位置,查看convertView,
            ViewGroup中父){

        LayoutInflater充气= getLayoutInflater();

        查看排= inflater.inflate(R.layout.spinner_item,父母,假);
        最后的TextView标签=(TextView中)一行
                .findViewById(R.id.spinneritemqualification);
        label.setText(select_qualification [位置]);

        最终的复选框checkboxButton =(复选框)行
                .findViewById(R.id.checkboxquali);
        如果((位置== 0)){

            checkboxButton.setVisibility(View.INVISIBLE);
        }
        checkboxButton.setOnCheckedChangeListener(新OnCheckedChangeListener(){

             @覆盖
             公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
                 // TODO自动生成方法存根
                 的System.out.println(位置+器isChecked);
                //整数POS =(整数)buttonView.getTag();
                 INT为getPosition =(整数)buttonView.getTag();
                 Toast.makeText(getApplicationContext(),
                        点:+为getPosition,Toast.LENGTH_LONG)
                        。显示();

                 如果(器isChecked){
                     checkboxButton.setChecked(真正的);
                 }
                 其他 {
                     checkboxButton.setChecked(假);
                 }
             }
         });
 

解决方案

我试图实现你想要什么。这里是它的演示。

您可以修改为您的要求。

定义适配器:

 进口android.content.Context;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.CheckBox;
进口android.widget.CompoundButton;
进口android.widget.TextView;

进口的java.util.ArrayList;
进口的java.util.List;


公共类MyAdapter扩展ArrayAdapter< StateVO> {
    私人语境mContext;
    私人的ArrayList< StateVO> listState;
    私人MyAdapter myAdapter;
    私人布尔isFromView = FALSE;

    公共MyAdapter(上下文的背景下,INT资源,列表和LT; StateVO>对象){
        超(背景下,资源对象);
        this.mContext =背景;
        this.listState =(ArrayList的< StateVO>)的对象;
        this.myAdapter =这一点;
    }

    @覆盖
    公共查看getDropDownView(INT位置,查看convertView,
                                ViewGroup中父){
        返回getCustomView(位置,convertView,父母);
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        返回getCustomView(位置,convertView,父母);
    }

    公共查看getCustomView(最终诠释的立场,观点convertView,
                              ViewGroup中父){

        最后ViewHolder持有人;
        如果(convertView == NULL){
            LayoutInflater layoutInflator = LayoutInflater.from(mContext);
            convertView = layoutInflator.inflate(R.layout.spinner_item,NULL);
            持有人=新ViewHolder();
            holder.mTextView =(TextView中)convertView
                    .findViewById(R.id.text);
            holder.mCheckBox =(复选框)convertView
                    .findViewById(R.id.checkbox);
            convertView.setTag(保持器);
        } 其他 {
            支架=(ViewHolder)convertView.getTag();
        }

        holder.mTextView.setText(listState.get(位置).getTitle());

        //检查从getview天气检查事件火()或用户输入
        isFromView = TRUE;
        holder.mCheckBox.setChecked(listState.get(位置).isSelected());
        isFromView = FALSE;

        如果((位置== 0)){
            holder.mCheckBox.setVisibility(View.INVISIBLE);
        } 其他 {
            holder.mCheckBox.setVisibility(View.VISIBLE);
        }
        holder.mCheckBox.setTag(位置);
        holder.mCheckBox.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){

            @覆盖
            公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
                INT为getPosition =(整数)buttonView.getTag();

                如果(!isFromView){
                    Log.e(isFromView,isFromView +!);
                    如果(为getPosition == 4){//凸轮制定一条规则,根据它的任何位置
                        如果(!listState.get(4).isSelected()){
                            如果(器isChecked){
                                的for(int i = 0; I< listState.size();我++){
                                    如果(ⅰ4;){
                                        listState.get(我).setSelected(真正的);
                                    }否则,如果(我== 4){
                                        listState.get(我).setSelected(真正的);
                                    } 其他 {
                                        listState.get(ⅰ).setSelected(假);
                                    }
                                }
                            }
                        } 其他 {
                            如果(!器isChecked){
                                的for(int i = 0; I< listState.size();我++){
                                    如果(ⅰ4;){
                                        listState.get(ⅰ).setSelected(假);
                                    }否则,如果(我== 4){
                                        listState.get(ⅰ).setSelected(假);
                                    }
                                }
                            }
                        }
                        myAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
        返回convertView;
    }

    私有类ViewHolder {
        私人TextView的mTextView;
        私人复选框mCheckBox;
    }
}
 

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

 公共类StateVO {
    私人字符串名称;
    选择私人布尔;

    公共字符串的getTitle(){
        返回称号;
    }

    公共无效的setTitle(字符串名称){
        this.title =称号;
    }

    公共布尔isSelected(){
        返回所选择的;
    }

    公共无效的setSelected(布尔选择){
        this.selected =选择;
    }
}
 

微调项目布局:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT>

    <的TextView
        机器人:ID =@ + ID /文
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:重力=center_vertical
        机器人:文本=文本
        机器人:textAlignment =万有引力/>

    <复选框
        机器人:ID =@ + ID /复选框
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentEnd =真
        机器人:layout_alignParentRight =真/>
< / RelativeLayout的>
 

微调您的活动:

 <微调
        机器人:ID =@ + ID /微调
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_marginTop =10dp/>
 

方法来设置微调数据:

 私人无效setSpinnerData(){
        最终的String [] select_qualification = {
                选择资质,第10 /以下,12号,文凭,UG,
                PG,博士学位};
        微调微调=(微调)findViewById(R.id.spinner);

        ArrayList的< StateVO> listVOs =新的ArrayList<>();

        的for(int i = 0; I< select_qualification.length;我++){
            StateVO stateVO =新StateVO();
            stateVO.setTitle(select_qualification [I]);
            stateVO.setSelected(假);
            listVOs.add(stateVO);
        }
        MyAdapter myAdapter =新MyAdapter(MainActivity.this,0,
                listVOs);
        spinner.setAdapter(myAdapter);
    }
 

享受编码。!!

I am having the 7 items in the spinner box. I have created the spinner box with textview and checkbox. I want to do the following logic.

Can you please give me any suggestion to do:

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

If I will check the UG checkbox, it will automatically checked state the checkbox for 10th/Below, 12th,Diploma and also disable the checked state of checkbox for PG and phd.

If am checking the any check box means the previous item checkbox automatically need to checked state and next item checkbox automatically need to unchecked state.

Right now this is my adapter file:

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);
                 }
             }
         });

解决方案

I have tried to implement what you want. And here is Demo of it.

You can modify as your requirement.

Custom adapter :

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;
    }
}

Basic VO to handle state of checkbox :

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;
    }
}

Spinner item layout :

<?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 in your activity :

 <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp" />

Method to set data in spinner :

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);
    }

Enjoy coding.!!

这篇关于该复选框,TextView的在Android的定制微调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆