Computer >> 컴퓨터 >  >> 프로그램 작성 >> Android

어댑터를 자동 완성 텍스트 보기로 설정하는 방법은 무엇입니까?

<시간/>

예제를 시작하기 전에 Android에서 자동 완성 텍스트 보기가 무엇인지 알아야 합니다. 자동 완성 텍스트 보기는 편집 텍스트와 같으며 editext의 하위 클래스이지만 목록의 제안을 드롭다운 목록으로 표시합니다. 텍스트 보기를 자동 완성하려면 임계값을 설정해야 합니다. 예를 들어 임계값을 1로 설정하여 사용자가 하나의 문자를 입력하면 임계값 문자에 따라 제안할 것입니다.

이 예는 Textview를 자동 완성하도록 어댑터를 설정하는 방법을 보여줍니다.

1단계 − Android Studio에서 새 프로젝트를 생성하고 파일 ⇒ 새 프로젝트로 이동하여 필요한 모든 세부 정보를 입력하여 새 프로젝트를 생성합니다.

2단계res/layout/activity_main.xml에 다음 코드를 추가합니다. .

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:app="https://schemas.android.com/apk/res-auto"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center"
   android:orientation="vertical"
   tools:context=".MainActivity">
   <AutoCompleteTextView
      android:id="@+id/autoComplete"
      android:layout_width="fill_parent"
      android:hint="Enter programming language"
      android:layout_height="wrap_content" />
</LinearLayout>

위에서 우리는 자동 완성 텍스트 보기를 선언했습니다. 사용자가 문자를 입력하면 드롭다운 메뉴에 제안 목록이 표시됩니다.

3단계src/MainActivity.java에 다음 코드를 추가합니다.

package com.example.andy.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
   RadioButton radioButton;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final AutoCompleteTextView autoCompleteTextView=findViewById(R.id.autoComplete);
      ArrayList arrayList=new ArrayList<>();
      arrayList.add("Android");
      arrayList.add("JAVA");
      arrayList.add("CPP");
      arrayList.add("C Programming");
      arrayList.add("Kotlin");
      arrayList.add("CSS");
      arrayList.add("HTML");
      arrayList.add("PHP");
      arrayList.add("Swift");
      ArrayAdapter arrayAdapter=new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, arrayList);
      autoCompleteTextView.setAdapter(arrayAdapter);
      autoCompleteTextView.setThreshold(1);
      autoCompleteTextView.addTextChangedListener(new TextWatcher() {
         @Override
         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            Log.d("beforeTextChanged", String.valueOf(s));
         }
         @Override
         public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.d("onTextChanged", String.valueOf(s));
         }
         @Override
         public void afterTextChanged(Editable s) {
            Log.d("afterTextChanged", String.valueOf(s));
         }
      });
   }
}

위의 코드에서는 ArrayList에 일부 값을 저장하고 ArrayList를 Array 어댑터에 추가했습니다. 자동 완성 텍스트 보기로 어댑터를 설정하고 임계값을 1로 추가했습니다. 애플리케이션을 실행해 보겠습니다. 실제 Android 모바일 장치를 컴퓨터에 연결했다고 가정합니다. Android 스튜디오에서 앱을 실행하려면 프로젝트의 활동 파일 중 하나를 열고 도구 모음에서 실행 아이콘을 클릭합니다. 모바일 장치를 옵션으로 선택한 다음 기본 화면을 표시할 모바일 장치를 확인하십시오 -

어댑터를 자동 완성 텍스트 보기로 설정하는 방법은 무엇입니까?

처음에는 위와 같이 화면을 표시하고 해당 textview에 ja를 입력하면 아래와 같이 어댑터의 결과가 표시됩니다.

어댑터를 자동 완성 텍스트 보기로 설정하는 방법은 무엇입니까?

위의 결과에는 J를 제거하고 해당 텍스트 보기에 c를 입력하면 아래와 같이 여러 제안이 표시되는 단 하나의 제안만 있습니다. -

어댑터를 자동 완성 텍스트 보기로 설정하는 방법은 무엇입니까?