Android中带自定义微调器的复选框和textview
作者:互联网
我在微调框中有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,checkbox,android-activity,android-spinner,custom-adapter 来源: https://codeday.me/bug/20190829/1763402.html