如何在微调器中仅显示文本
how to Display Only Text in spinner
我创建了一个带有自定义适配器的 Spinner,其中我有一个图像视图和一个文本视图,当我从 Spinner 中 select 项目时,我显示带有 selected Item 的图像,它正在 Perfectely很好。
问题:
但问题是,当我 select 任何项目时,它会在下拉列表中显示带有 selected 项目的图像。并在微调器中显示相同的(图像和文本)..
我想要的
我想在微调器中只显示文本而没有图像,如您在
中所见
图像它在微调器中显示带有文本的图像...我不想要这个
这是我正在使用的代码,请给我建议解决方案谢谢
public class SpinnerAdapter extends ArrayAdapter<SpinnerItem> {
private Context mContext;
private ArrayList<SpinnerItem> listState;
public SpinnerAdapter(Context context, int resource,
ArrayList<SpinnerItem> objects) {
super(context, resource, objects);
this.mContext = context;
this.listState = objects;
}
@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);
}
@SuppressLint("InflateParams")
public View getCustomView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.spinner_item_layout,
null);
holder = new ViewHolder();
holder.mTextView = (TextView) convertView.findViewById(R.id.text);
holder.mCheckedImage = (ImageView) convertView
.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTextView.setText(listState.get(position).getTitle());
if (listState.get(position).isSelected()) {
holder.mCheckedImage.setVisibility(View.VISIBLE);
} else {
holder.mCheckedImage.setVisibility(View.INVISIBLE);
}
return convertView;
}
private class ViewHolder {
private TextView mTextView;
private ImageView mCheckedImage;
}
public void setSpinnerAdapter(ArrayList<SpinnerItem> spinnerItems) {
this.listState = spinnerItems;
notifyDataSetChanged();
}
}
private void setBottelCountData() {
final String[] select_qualification = { "", "1", "2",
"3" };
bottelCountList = new ArrayList<>();
for (int i = 0; i < select_qualification.length; i++) {
SpinnerItem spinnerItem = new SpinnerItem();
spinnerItem.setTitle(select_qualification[i]);
if (i == 2) {
spinnerItem.setSelected(false);
} else {
spinnerItem.setSelected(false);
}
bottelCountList.add(spinnerItem);
}
bottelCountAdapter = new SpinnerAdapter(getActivity(), 0,
bottelCountList);
bottelCountAdapter.setDropDownViewResource(R.layout.spinner_item_layout);
bottel_Count_Spinner.setAdapter(bottelCountAdapter);
bottel_Count_Spinner.setSelection(3);
}
bottel_Count_Spinner
.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
if (position == 0) {
for (int count = 0; count < bottelCountList.size(); count++) {
bottelCountList.get(count).setSelected(false);
}
noOfBottel = 0;
return;
}
bottelCountList.get(position).setSelected(true);
for (int count = 0; count < bottelCountList.size(); count++) {
if (position != count) {
bottelCountList.get(count).setSelected(false);
}
}
bottel_Count_Spinner.setPrompt("Hello");
bottelCountAdapter.setSpinnerAdapter(bottelCountList);
noOfBottel = Integer.parseInt(bottelCountList.get(
position).getTitle());
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
xml 微调项布局
<?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="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/checkbox"
android:layout_centerVertical="true"
android:padding="10dp"/>
<ImageView
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:padding="10dp"
android:src="@android:drawable/checkbox_on_background"
android:contentDescription="@string/app_name"
android:layout_marginLeft="10dp"
android:layout_alignParentStart="true" />
</RelativeLayout>
你有两种方法
getView
- 在您 select 之后显示一个值
getDropDownView
- 定义下拉菜单打开时的视图
您必须修改 getCustomView
并处理差异(隐藏复选框图像)。
试试这个
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent, true);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent, false);
}
@SuppressLint("InflateParams")
public View getCustomView(int position, View convertView, ViewGroup parent, boolean isDropDown) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.spinner_item_layout,
null);
holder = new ViewHolder();
holder.mTextView = (TextView) convertView.findViewById(R.id.text);
holder.mCheckedImage = (ImageView) convertView
.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTextView.setText(listState.get(position).getTitle());
if (isDropDown) {
if (listState.get(position).isSelected()) {
holder.mCheckedImage.setVisibility(View.VISIBLE);
} else {
holder.mCheckedImage.setVisibility(View.INVISIBLE);
}
}
else {
holder.mCheckedImage.setVisibility(View.INVISIBLE);
}
return convertView;
}
您需要为 getDropDownView
和 getView
方法分开视图。 getDropDownView
为用于选定值的行调用。另一方面,当您打开微调器时,会为每一行调用 getView
。
试试这个,
<RelativeLayout
android:id="@+id/rlCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="@android:color/white" >
<Spinner
android:id="@+id/spnCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:popupBackground="@android:color/transparent"
android:visibility="invisible" />
<Button
android:id="@+id/btnAllergies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:background="@android:color/transparent"
android:gravity="start|center"
android:paddingBottom="5dp"
android:paddingEnd="5dp"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:paddingStart="10dp"
android:paddingTop="5dp"
android:tag="0"
android:text="Select Country"
android:textColor="#ACACAC"
android:textSize="14sp" />
</RelativeLayout>
我创建了一个带有自定义适配器的 Spinner,其中我有一个图像视图和一个文本视图,当我从 Spinner 中 select 项目时,我显示带有 selected Item 的图像,它正在 Perfectely很好。
问题:
但问题是,当我 select 任何项目时,它会在下拉列表中显示带有 selected 项目的图像。并在微调器中显示相同的(图像和文本)..
我想要的
我想在微调器中只显示文本而没有图像,如您在
图像它在微调器中显示带有文本的图像...我不想要这个
这是我正在使用的代码,请给我建议解决方案谢谢
public class SpinnerAdapter extends ArrayAdapter<SpinnerItem> {
private Context mContext;
private ArrayList<SpinnerItem> listState;
public SpinnerAdapter(Context context, int resource,
ArrayList<SpinnerItem> objects) {
super(context, resource, objects);
this.mContext = context;
this.listState = objects;
}
@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);
}
@SuppressLint("InflateParams")
public View getCustomView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.spinner_item_layout,
null);
holder = new ViewHolder();
holder.mTextView = (TextView) convertView.findViewById(R.id.text);
holder.mCheckedImage = (ImageView) convertView
.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTextView.setText(listState.get(position).getTitle());
if (listState.get(position).isSelected()) {
holder.mCheckedImage.setVisibility(View.VISIBLE);
} else {
holder.mCheckedImage.setVisibility(View.INVISIBLE);
}
return convertView;
}
private class ViewHolder {
private TextView mTextView;
private ImageView mCheckedImage;
}
public void setSpinnerAdapter(ArrayList<SpinnerItem> spinnerItems) {
this.listState = spinnerItems;
notifyDataSetChanged();
}
}
private void setBottelCountData() {
final String[] select_qualification = { "", "1", "2",
"3" };
bottelCountList = new ArrayList<>();
for (int i = 0; i < select_qualification.length; i++) {
SpinnerItem spinnerItem = new SpinnerItem();
spinnerItem.setTitle(select_qualification[i]);
if (i == 2) {
spinnerItem.setSelected(false);
} else {
spinnerItem.setSelected(false);
}
bottelCountList.add(spinnerItem);
}
bottelCountAdapter = new SpinnerAdapter(getActivity(), 0,
bottelCountList);
bottelCountAdapter.setDropDownViewResource(R.layout.spinner_item_layout);
bottel_Count_Spinner.setAdapter(bottelCountAdapter);
bottel_Count_Spinner.setSelection(3);
}
bottel_Count_Spinner
.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
if (position == 0) {
for (int count = 0; count < bottelCountList.size(); count++) {
bottelCountList.get(count).setSelected(false);
}
noOfBottel = 0;
return;
}
bottelCountList.get(position).setSelected(true);
for (int count = 0; count < bottelCountList.size(); count++) {
if (position != count) {
bottelCountList.get(count).setSelected(false);
}
}
bottel_Count_Spinner.setPrompt("Hello");
bottelCountAdapter.setSpinnerAdapter(bottelCountList);
noOfBottel = Integer.parseInt(bottelCountList.get(
position).getTitle());
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
xml 微调项布局
<?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="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/checkbox"
android:layout_centerVertical="true"
android:padding="10dp"/>
<ImageView
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:padding="10dp"
android:src="@android:drawable/checkbox_on_background"
android:contentDescription="@string/app_name"
android:layout_marginLeft="10dp"
android:layout_alignParentStart="true" />
</RelativeLayout>
你有两种方法
getView
- 在您 select 之后显示一个值getDropDownView
- 定义下拉菜单打开时的视图
您必须修改 getCustomView
并处理差异(隐藏复选框图像)。
试试这个
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent, true);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent, false);
}
@SuppressLint("InflateParams")
public View getCustomView(int position, View convertView, ViewGroup parent, boolean isDropDown) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.spinner_item_layout,
null);
holder = new ViewHolder();
holder.mTextView = (TextView) convertView.findViewById(R.id.text);
holder.mCheckedImage = (ImageView) convertView
.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTextView.setText(listState.get(position).getTitle());
if (isDropDown) {
if (listState.get(position).isSelected()) {
holder.mCheckedImage.setVisibility(View.VISIBLE);
} else {
holder.mCheckedImage.setVisibility(View.INVISIBLE);
}
}
else {
holder.mCheckedImage.setVisibility(View.INVISIBLE);
}
return convertView;
}
您需要为 getDropDownView
和 getView
方法分开视图。 getDropDownView
为用于选定值的行调用。另一方面,当您打开微调器时,会为每一行调用 getView
。
试试这个,
<RelativeLayout
android:id="@+id/rlCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="@android:color/white" >
<Spinner
android:id="@+id/spnCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:popupBackground="@android:color/transparent"
android:visibility="invisible" />
<Button
android:id="@+id/btnAllergies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:background="@android:color/transparent"
android:gravity="start|center"
android:paddingBottom="5dp"
android:paddingEnd="5dp"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:paddingStart="10dp"
android:paddingTop="5dp"
android:tag="0"
android:text="Select Country"
android:textColor="#ACACAC"
android:textSize="14sp" />
</RelativeLayout>