gpt4 book ai didi

android更改微调器中项目的文本颜色

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:33:31 24 4
gpt4 key购买 nike

我有一个微调器,在某些情况下,某些选项不应该是可选的。我有使项目不可选择的代码,但它不会使文本颜色变灰以指定该项目不可选择。如何更改微调器中应禁用的项目的文本颜色?这是代码:

public class main extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

PatchedSpinner pSpinner = (PatchedSpinner) findViewById(R.id.spinner2);
ArrayList<String> testarray = new ArrayList<String>();
testarray.add("item0");
testarray.add("item1");
testarray.add("item2");
testarray.add("item3");

ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, testarray) {

@Override
public boolean isEnabled(int position) {
return position != 1;
}

public boolean areAllItemsEnabled() {
return false;
}
};
pSpinner.setAdapter(spinnerAdapter);
}

下面是用于创建能够禁用微调器中某些项目的微调器的自定义微调器类。

public class PatchedSpinner extends Spinner {

public PatchedSpinner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public PatchedSpinner(Context context) {
super(context);
}

public PatchedSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public boolean performClick() {
// this line removed, we do not want to delegate the click to the spinner.
// boolean handled = super.performClick();

Context context = getContext();

final DropDownAdapter adapter = new DropDownAdapter(getAdapter());

CharSequence mPrompt = getPrompt();

AlertDialog.Builder builder = new AlertDialog.Builder(context);
if (mPrompt != null) {
builder.setTitle(mPrompt);
}
builder.setSingleChoiceItems(adapter, getSelectedItemPosition(), this).show();

return true;
}

private static class DropDownAdapter implements ListAdapter, SpinnerAdapter {
private SpinnerAdapter mAdapter;

public DropDownAdapter(SpinnerAdapter adapter) {
mAdapter = adapter;
}

public int getCount() {
return mAdapter == null ? 0 : mAdapter.getCount();
}

public Object getItem(int position) {
return mAdapter == null ? null : mAdapter.getItem(position);
}

public long getItemId(int position) {
return mAdapter == null ? -1 : mAdapter.getItemId(position);
}

public View getView(int position, View convertView, ViewGroup parent) {
return getDropDownView(position, convertView, parent);
}

public View getDropDownView(int position, View convertView,ViewGroup parent) {
if (mAdapter == null) {
return null;
}
mAdapter.getDropDownView(position, convertView, parent);
}

public boolean hasStableIds() {
return mAdapter != null && mAdapter.hasStableIds();
}

public void registerDataSetObserver(DataSetObserver observer){
if (mAdapter != null) {
mAdapter.registerDataSetObserver(observer);
}
}

public void unregisterDataSetObserver(DataSetObserver observer) {
if (mAdapter != null) {
mAdapter.unregisterDataSetObserver(observer);
}
}

// PATCHED
public boolean areAllItemsEnabled() {
if (mAdapter instanceof BaseAdapter) {
return ((BaseAdapter) mAdapter).areAllItemsEnabled();
} else {
return true;
}
}

// PATCHED
public boolean isEnabled(int position) {
if (mAdapter instanceof BaseAdapter) {
return ((BaseAdapter) mAdapter).isEnabled(position);
} else {
return true;
}
}

public int getItemViewType(int position) {
return 0;
}

public int getViewTypeCount() {
return 1;
}

public boolean isEmpty() {
return getCount() == 0;
}
}
}

最佳答案

我发现要完成这项工作,您必须在主 Activity 中设置 ArrayAdapter 时覆盖 getDropDownView。

public class main extends Activity {  
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

PatchedSpinner pSpinner = (PatchedSpinner) findViewById(R.id.spinner2);
ArrayList<String> testarray = new ArrayList<String>();
testarray.add("item0");
testarray.add("item1");
testarray.add("item2");
testarray.add("item3");
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, testarray) {

@Override
public boolean isEnabled(int position) {
return position != 1;
}

@Override
public boolean areAllItemsEnabled() {
return false;
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null) {
Context mContext = this.getContext();
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}

TextView tv = (TextView) v.findViewById(R.id.spinnerTarget);
tv.setText(testarray.get(position));

switch (position) {
case 0:
tv.setTextColor(Color.RED);
break;
case 1:
tv.setTextColor(Color.BLUE);
break;
default:
tv.setTextColor(Color.BLACK);
break;
}
return v;
}
};

pSpinner.setAdapter(spinnerAdapter);
}

正在扩充的布局是一个名为 row.xml 的自定义布局。它用于设置下拉 View 的显示。

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14pt" />

关于android更改微调器中项目的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5836254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com