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 ...
随机推荐
- java实现按拼音排序名称
private static String[] nameArray = {"张三","李四","王二","付火"}; @ ...
- C# Json 转对象
C# public static UserInfo JsonConvertStringWeiXinInfo(string json) { return (UserInfo)Newtonsoft.Jso ...
- 表结构导出到excel中
SELECT 表名 = case when a.colorder=1 then d.name else '' end, 表说明 = case when a.colorder=1 then isnull ...
- CentOS 7 (无盘安装)PXE服务器的搭建(失败求助版)
折腾了一天半,PXE无盘服务器以暂时失败而告终. 基本原理 1. 首先客户端主机需要支持PXE,大部分主板都支持. 2. PXE服务器需要安装DHCP.TFTP.FTP服务. 3. DHCP服务用来给 ...
- Beaglebone Black–智能家居控制系统 LAS - 用 UART 连接 ESP8266 (ESP-01 版)
这是一块便宜 (¥12.5)的 WiFi 模块,3.3V ,芯片是乐鑫科技(Espressif)出品.它本身是很多玩法,比如这个 NodeMCU (淘宝有套件焊接好一整套的带 USB 接口的,搜 es ...
- plot bar chart using python
Example import matplotlib.pyplot as plt import plotly.plotly as py # Learn about API authentication ...
- Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2)C. Road to Cinema 二分
C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- hiho #1050 : 树中的最长路 树的直径
#1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中, ...
- 1----lua的环境搭建
本人使用的是LDT用来学习lua,原因是因为本人熟悉Eclipse的操作,并且安装方便 首先需要下载并配置JDK,也就是java的运行环境(以下为官网网址) http://www.oracle.com ...
- 对称加密DES和TripleDES
一. 对称加密 对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码(称为对称密码).因此,通信双方都必 ...