AutoCompleteTextView
AutoCompleteTextView的学习
简单使用AutoCompleteTextView
mainactivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
private static final String[] books = new String[]{
"Android项目实战", "Python项目实战", "Android基础入门", "Java项目实战", "微信小程序项目实战"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow()
.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
);
AutoCompleteTextView autocompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocompleteTextView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this,
android.R.layout.simple_dropdown_item_1line,
books);
autocompleteTextView.setAdapter(adapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/piccc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<AutoCompleteTextView
android:id="@+id/autocompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:dropDownWidth="match_parent"
android:layout_toRightOf="@id/piccc"
android:hint="请输入关键字" />
</RelativeLayout>