09 January 2017

Android: AutoCompleteTextView - Tự động gợi ý tìm kiếm (Android Studio)

activity_main.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <AutoCompleteTextView
        android:id="@+id/auto_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
         />
</RelativeLayout>
MainActivity.java
Java Android 2017
package com.example.lonely.demoautocompletetextview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends AppCompatActivity {

    String[] name = {"Antonio", "Antonio 1", "Antonio 2", " Antonio 3"};
    private AutoCompleteTextView autoName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        autoName = (AutoCompleteTextView) findViewById(R.id.auto_name);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, name);
        autoName.setThreshold(3);
        autoName.setAdapter(arrayAdapter);
    }
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang