public class main extends Activity {@OverrIDepublic 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,VIEwGroup parent) { if (mAdapter == null) { return null; } mAdapter.getDropDownVIEw(position,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; } }}解决方法 我想出,为了使这项工作,您必须在主活动中设置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,testarray) { @OverrIDe public boolean isEnabled(int position) { return position != 1; } @OverrIDe public boolean areAllitemsEnabled() { return false; } @OverrIDe public VIEw getDropDownVIEw(int position,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的自定义布局.它用于设置下拉视图的显示.
<?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更改文件中的文本颜色全部内容,希望文章能够帮你解决android更改文件中的文本颜色所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)