アダプタをオートコンプリートテキストビューに設定するにはどうすればよいですか?
例に入る前に、Androidのオートコンプリートテキストビューとは何かを知っておく必要があります。オートコンプリートテキストビューは編集テキストと同じで、editextのサブクラスですが、リストからの提案をドロップダウンリストとして表示します。テキストビューをオートコンプリートするには、しきい値を設定する必要があります。たとえば、しきい値を1に設定しているため、ユーザーが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を配列アダプターに追加しています。アダプターをオートコンプリートテキストビューに設定し、しきい値を1に追加しました。アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します-
最初は、上記のように画面が表示され、そのテキストビューにjaと入力すると、以下に示すようにアダプタからの結果が表示されます-
上記の結果では、提案は1つだけです。Jを削除し、そのテキストビューにcと入力すると、以下に示すように複数の提案が表示されます-
-
Androidアプリで簡単なテキストファイルを読む方法は?
この例は、Androidアプリで簡単なテキストファイルを読み取る方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 新しいAndroidリソースディレクトリ(raw.xml)を作成し、res/rawにテキストファイルを追加します ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf
-
Androidでテキストメッセージに自動返信する方法
通常、テキストメッセージにすばやく返信する場合は、しばらく返信しないと人々が心配する可能性があります。自動返信機能はプラットフォームに組み込まれていませんが、Androidで自動テキスト返信を設定するのはありがたいことに簡単です。 いくつかのアプリを使用して、運転中、会議中、休暇中、またはその他の方法であなたに連絡しようとする人々に自動応答を送信できます。 Androidでテキストに自動返信する方法は次のとおりです。 AndroidAutoで運転中にテキストに自動的に応答する 運転中の自動応答に主に関心がある場合、Android Autoを使用すると、ワンタップで受信テキストに自動応答でき