码迷,mamicode.com
首页 > 移动开发 > 详细

android AutoCompleteTextView和Spinner选中项加亮

时间:2016-04-07 01:06:46      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

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选中项加亮

标签:

原文地址:http://www.cnblogs.com/coolyylu/p/5361749.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!