Android UI 控件--(multi)AutoCompleteTextView、查看原文件、修改颜色

AutoCompleteTextView是一种能够自动补全的文本框,而multiAutoCompleteTextView是一种能够识别分隔符的自动补全文本框。

public class MainActivity extends AppCompatActivity {
    //申明一个自动补全文本框
    private AutoCompleteTextView actv;
    //为自动补全文本框设立一个数据适配器
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
        //mactv = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView);
        /*在这里我们“重写”了android.R.layout.simple_dropdown_item_1line,因为自带的布局文件文本是白色的
         我们这里需要黑色的,于是按住Ctrl+对应的布局文件,我们就可以进入android源文件内部查看这个布局,将代码
         复制粘贴在一个新的自定义布局文件里面(这里是myadapter),再在里面设置textcolor,就达成了将文本颜色
         变成黑色的目的
         */
        adapter = new ArrayAdapter<String>(this, R.layout.myadapter);
        //加入自动提示的备选文本
        adapter.add("1");
        adapter.add("124");
        adapter.add("1234");
        adapter.add("134");
        adapter.add("2345");
        adapter.add("2356");
        adapter.add("2567");
        //这样一来在输入的时候就可以有自动提示了
        actv.setAdapter(adapter);
    }
}

multiAutoCompleteTextView的用法也大同小易,只不过在setAdapter之前要再设置一个分词器

mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

这句话是将英文的逗号作为分词器,也就是在逗号之后输入文本就又会进行提示。如果想要自己设置分词器的话,就重写它的三个方法就行了,还是按住Ctrl选择语句,进入CommaTokenizer,将public int findTokenStart(CharSequence text, int cursor),public int findTokenEnd(CharSequence text, int cursor)和public CharSequence terminateToken(CharSequence text)复制粘贴过来然后覆盖。
比如这里将英文句号作为分词标志:

mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer() {
                               @Override
                               public int findTokenStart(CharSequence text, int cursor) {
                                   int i = cursor;

                                   while (i > 0 && text.charAt(i - 1) != '.') {
                                       i--;
                                   }
                                   while (i < cursor && text.charAt(i) == ' ') {
                                       i++;
                                   }

                                   return i;
                               }

                               @Override
                               public int findTokenEnd(CharSequence text, int cursor) {
                                   int i = cursor;
                                   int len = text.length();

                                   while (i < len) {
                                       if (text.charAt(i) == '.') {
                                           return i;
                                       } else {
                                           i++;
                                       }
                                   }

                                   return len;
                               }

                               @Override
                               public CharSequence terminateToken(CharSequence text) {
                                   int i = text.length();

                                   while (i > 0 && text.charAt(i - 1) == ' ') {
                                       i--;
                                   }

                                   if (i > 0 && text.charAt(i - 1) == '.') {
                                       return text;
                                   } else {
                                       if (text instanceof Spanned) {
                                           SpannableString sp = new SpannableString(text + ". ");
                                           TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
                                                   Object.class, sp, 0);
                                           return sp;
                                       } else {
                                           return text + ". ";
                                       }
                                   }
                               }
                           }
        );

就可以了

其中myadapter布局文件如下:(实现将文本改为黑色):

<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml
**
** Copyright 2008, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/dropDownItemStyle"
    android:textAppearance="?android:attr/textAppearanceLargePopupMenu"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:textColor="#ff000000"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee" />

倒数第三行就是修改颜色的
Android颜色格式:
颜色”#12345678”
12位是指的透明度,00完全透明,ff完全不透明,34位指的红色,56位指绿色,78位指蓝色
例如”#ffffff00”是指完全不透明的黄色”#70ff00ff”指半透明的紫色,”#ff000000”指不透明的黑色

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值