AutoCompleteTextView和Spinner的使用方法

Posted liguangsunls

tags:

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

首先说明AutoCompleteTextView的使用方法。主要是起到自己主动填充的作用。输入keyword,假设存在该字便能够自己主动填充。代码例如以下

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <AutoCompleteTextView
        android:id="@+id/autotextview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:completionHint="请选择喜欢的图书"
        android:textColor="#000000"
        android:completionThreshold="1"
        />
    <Button
        android:id="@+id/btn_spinner"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Spinner 界面"/>
</RelativeLayout>
MianActivity.java

package lzl.edu.com.autocomplete;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;

public class MainActivity extends Activity {

    private AutoCompleteTextView autoTextView;
    private Button btn_spinner;
    String[] books = {"从你的全世界路过","荆棘鸟","老人与海","第一行代码","我们从未陌生过"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        autoTextView = (AutoCompleteTextView)findViewById(R.id.autotextview);
        btn_spinner = (Button)findViewById(R.id.btn_spinner);
        // android.R.layout.simple_dropdown_item_1line 系统提供的自己主动填充的方法。
        ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,books);
        autoTextView.setAdapter(aa);
        btn_spinner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,BookeSpinner.class);
                startActivity(intent);
            }
        });

    }
}

Spinner通常和arrays.xml结合集合使用。在value目录下创建arrays.xml文件。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="books">
        <item>从你的全世界路过</item>
        <item>你好,世界</item>
        <item>JAVA疯狂讲义</item>
        <item>第一行代码</item>
        <item>荆棘鸟</item>
    </string-array>
</resources>
在xml文件里建立Spinner标签

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="lzl.edu.com.autocomplete.BookeSpinner">

    <Spinner
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/books"
        >
    </Spinner>

</RelativeLayout>
Activity中增加布局就能够了

package lzl.edu.com.autocomplete;

import android.app.Activity;
import android.os.Bundle;

public class BookeSpinner extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_booke_spinner);


    }
}
Spinnner结合Adapter的使用

BooksActivity.java

package lzl.edu.com.autocomplete;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class BookeSpinner extends Activity {

    Spinner spinnerbooks;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_booke_spinner);

        spinnerbooks = (Spinner)findViewById(R.id.spinnerbooks);
        BaseAdapter ba = new BaseAdapter() {
            @Override
            public int getCount() {
                return 100;
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView = new TextView(BookeSpinner.this);
                textView.setText("第"+position+"本书");
                textView.setTextColor(Color.rgb(240, 200, 100));
                textView.setTextSize(20);
                return textView;
            }
        };
        spinnerbooks.setAdapter(ba);
    }
}
xml布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="lzl.edu.com.autocomplete.BookeSpinner">

    <Spinner
        android:id="@+id/spinnerbooks"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
    </Spinner>

</RelativeLayout>


以上是关于AutoCompleteTextView和Spinner的使用方法的主要内容,如果未能解决你的问题,请参考以下文章

Android学习之AutoCompleteTextView和MultiAutoCompleteTextView

AutoCompleteTextView (setText) 和 fragment.detach().attach()

TextInputLayout 和 AutoCompleteTextView

是否可以在 android 中以编程方式创建和填充 AutoCompleteTextView?

AutoCompleteTextView和Spinner的使用方法

View(视图)——AutoCompleteTextView Spinner和消息提示