android AutoCompleteTextView和Spinner选中项加亮

Posted Ender.Lu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android AutoCompleteTextView和Spinner选中项加亮相关的知识,希望对你有一定的参考价值。

package com.example.spinnerexample;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {
    private Spinner spinner;
    private TextView tv;
    private static final String[] m = { "A型", "B型", "O型", "AB型", "其他" };

    private AutoCompleteTextView at1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) this.findViewById(R.id.textView1);
        at1 = (AutoCompleteTextView) this.findViewById(R.id.at1);
        
        spinner = (Spinner) this.findViewById(R.id.spinner1);

        /*
         * ArrayAdapter<String> adapter = new
         * ArrayAdapter<String>(this,R.layout.spinner, m){
         * 
         * @Override public View getDropDownView(int position, View convertView,
         * ViewGroup parent) { if(convertView==null){ convertView =
         * getLayoutInflater().inflate(R.layout.spinner_item, parent, false); }
         * TextView label = (TextView) convertView.findViewById(R.id.label);
         * label.setText(getItem(position)); if
         * (spinner.getSelectedItemPosition() == position)
         * label.setTextColor(Color.RED); //ImageView icon = (ImageView)
         * convertView.findViewById(R.id.icon);
         * //icon.setVisibility(spinner.getSelectedItemPosition() ==
         * position?View.VISIBLE:View.INVISIBLE);
         * 
         * return convertView; } };
         */
        ArrayAdapter<String> adapter1 = new  ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,m){
             @Override
                public View getView(int position, View convertView, ViewGroup parent)
                {
                 TextView label = (TextView) super.getView(position,
                            convertView, parent);
                  
                 if (at1.getText().toString().indexOf(m[position])==0) {
                        label.setTextColor(Color.RED);
                        
                    }else{
                        label.setTextColor(Color.BLACK);
                    }
                 return label;
                }
        };    
        at1.setAdapter(adapter1);
        
        at1.setOnFocusChangeListener(new OnFocusChangeListener() {  
            @Override  
            public void onFocusChange(View v, boolean hasFocus) {  
                AutoCompleteTextView view = (AutoCompleteTextView) v;  
                if (hasFocus) {  
                        view.showDropDown();  
                }  
            }  
        });  
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, m) {
            @Override
            public View getDropDownView(int position, View convertView,
                    ViewGroup parent) {
                TextView label = (TextView) super.getView(position,
                        convertView, parent);
                
                
                if (spinner.getSelectedItemPosition() == position) {
                    label.setTextColor(Color.RED);
                    label.setVisibility(View.VISIBLE);
                }
                return label;
            }
        };
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub

            }

            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    // 使用数组形式操作
    class SpinnerSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            tv.setText("你的血型是:" + m[arg2]);

        }

        public void onNothingSelected(AdapterView<?> arg0) {
        }
    }

}

http://blog.csdn.net/i_lovefish/article/details/17337999

http://www.jb51.net/article/49133.htm

http://www.tuicool.com/articles/uYBrie

http://www.it1352.com/124657.html

http://gundumw100.iteye.com/blog/1773670

以上是关于android AutoCompleteTextView和Spinner选中项加亮的主要内容,如果未能解决你的问题,请参考以下文章

Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )

android 21 是啥版本

Android逆向-Android基础逆向(2-2)

【Android笔记】android Toast

图解Android - Android核心机制

Android游戏开发大全的目录