码迷,mamicode.com
首页 > 其他好文 > 详细

AutoCompleteTextView和Spinner的用法

时间:2015-08-18 19:25:24      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:spinner   baseadapter   autocompletetextview   

首先说明AutoCompleteTextView的用法,主要是起到自动填充的作用,输入关键字,如果存在该字便可以自动填充。代码如下

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的用法

标签:spinner   baseadapter   autocompletetextview   

原文地址:http://blog.csdn.net/u011521890/article/details/47754915

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