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) {
}
} }
补充SpinnerArrayAdapter类
调用方法如下
方法1:
List<String> list = new ArrayList<String>();
list.add("新增");
list.add("已确认");
ArrayAdapter<String> aAdapter = new SpinnerArrayAdapter<String>(this,R.layout.spinner_dropdownlist_item, R.id.text,list,txtstatus);
aAdapter.setDropDownViewResource(R.layout.spinner_dropdownlist_item);
txtstatus.setAdapter(aAdapter);
方法2:
ArrayAdapter<String> tblproductionperiod_adapter = new SpinnerArrayAdapter<String>(this,R.layout.spinner_item, R.id.text,tblproductionperiod_list ,txtperiod);
tblproductionperiod_adapter.setDropDownViewResource(R.layout.spinner_dropdownlist_item);
txtperiod.setAdapter(tblproductionperiod_adapter);
package eip.util; import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast; import java.util.List; import eip.frame.R;
import eip.module.IPOProgressActivity; public class SpinnerArrayAdapter<T> extends ArrayAdapter {
private Spinner spinner;
public SpinnerArrayAdapter(Context context, int resource, int textViewResourceId, List<T> list ,Spinner zspinner) {
super(context ,resource,textViewResourceId,list);
this.spinner = zspinner;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) { if(convertView==null){
convertView = ((Activity)this.getContext()).getLayoutInflater().inflate(R.layout.spinner_dropdownlist_item, parent, false);
} TextView label = (TextView) super.getView(position,
convertView, parent).findViewById(R.id.text); if (this.spinner.getSelectedItemPosition() == position) {
label.setTextColor(Color.GREEN);
}
return convertView;
}
}
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——AutoCompleteTextView、Spinner和消息提示
layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- Android AutoCompleteTextView和MultiAutoCompleteTextView使用
Android AutoCompleteTextView和MultiAutoCompleteTextView的功能类似于百度或者Google在搜索栏输入信息的时候,弹出的与输入信息接近的提示信息: 它 ...
- Android学习之Spinner
Android给我们提供了一个spinner控件,这个控件主要就是一个列表,那么我们就来说说这个控件吧,这个控件在以前的也看见过,但今天还是从新介绍一遍吧.Spinner位于 android.widg ...
- Android中Spanner获取选中内容和选中位置,根据位置选择对象
作为一名菜鸟,关于spanner获取选中的内容文字代码,网上后很多 但是根据给出的位置来自动选择对象,这个代码一直没找到 后来找人问了问,才知道就一句话的事,特意在这里记录下 array.xml X ...
- Android AutoCompleteTextView控件实现类似百度搜索提示,限制输入数字长度
Android AutoCompleteTextView 控件实现类似被搜索提示,效果如下 1.首先贴出布局代码 activity_main.xml: <?xml version="1 ...
- AutoCompleteTextView和Spinner的使用方法
首先说明AutoCompleteTextView的使用方法.主要是起到自己主动填充的作用.输入keyword,假设存在该字便能够自己主动填充.代码例如以下 activity_main.xml < ...
- Android学习笔记(23):列表项的容器—AdapterView的子类们
AdapterView的子类的子类ListView.GridView.Spinner.Gallery.AdapterViewFlipper和StackView都是作为容器使用,Adapter负责提供各 ...
- 复选框(checkox)全选、全不选、反选、获得选中项值的用例
HTML部分: <div class="all"> <ul> <li><input type="checkbox" v ...
- 刷新各ifream当前页,下拉项改变触发事件js,给选中项加背景色js
<script type="text/javascript" language="javascript"> //刷新框架各页面 function r ...
随机推荐
- CSS——清除浮动
<div id="main" class="clear"> <div id="left"> </div> ...
- python学习笔记五 模块下(基础篇)
shevle 模块 扩展pickle模块... 1.潜在的陷进 >>> import shelve>>> s = shelve.open("nb" ...
- flume ng之组件介绍
1.channel 2.source 3.sink 4.直接读取文件Source,有哪两种方式? 5.Channel有几种方式? 6.Sink在设置存储数据时,数据较多,较少的情况下,该如何处理? F ...
- iOS奔溃日志总结
1,http://www.cnblogs.com/qingjoin/p/3515902.html 2,http://blog.csdn.net/u012269653/article/details/4 ...
- raid0,raid1,raid10,raid5,raid50,raid6,raid60的功能总结简述
1,raid0的特性:采用剥离,数据将在几个磁盘上进行分割.数据被分成很多数据块,每一数据块会被写入不同的磁盘.从而, 每一磁盘的工作负荷都得到了降低,这有助于加速数据传输.RAID-0可让磁盘更好地 ...
- 【leetcode❤python】 112. Path Sum
#-*- coding: UTF-8 -*-# Definition for a binary tree node.# class TreeNode(object):# def __init_ ...
- BZOJ 3144 切糕(最小割)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3144 题意: 思路:我们假设没有那个D的限制.这样就简 单了.贪心的话,我们只要在每一个 ...
- Flip Game poj1753
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32961 Accepted: 14407 Description Fli ...
- Nodejs之socket广播
nodejs发送udp广播还是蛮简单的,我们先写个服务器用于接收广播数据,代码如下: var dgram = require("dgram"); var server = dgra ...
- 【转载】C++知识库内容精选 尽览所有核心技术点
原文:C++知识库内容精选 尽览所有核心技术点 C++知识库全新发布. 该知识库由C++领域专家.CSDN知名博客专家.资深程序员和项目经理安晓辉(@foruok)绘制C++知识图谱,@wangshu ...