BÀI 75 - TẠO HỘP THOẠI CHẠM ĐỂ CHỌN TRONG ANDROID

  • Đây thực chất là sự kết hợp của TextInputLayout và AutoCompleteTextView.

  • Khi người dùng bấm vào hộp thoại này nó sẽ hiện ra các thông tin được nạp sẵn và chỉ cần chọn để điền thông tin gì đó.

  • Bài này mình sẽ hướng dẫn điền tên thành phố lấy từ một tệp json trên brower. Nghĩa là máy chủ sẽ tự động cung cấp thông tin thay vì phải sửa code.

  • Đầu tiên trong xml tạo hộp thoại như thế này.

<com.google.android.material.textfield.TextInputLayout

android:id="@+id/textinoutlayout_covid_chontinhthanh"

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"

android:padding="10dp"

android:background="@color/white"

android:layout_width="match_parent"

android:layout_height="wrap_content">

<AutoCompleteTextView

android:id="@+id/autocompleteTextView_covid_chontinhthanh"

android:hint="Chạm để chọn tỉnh, thành"

android:padding="2dp"

android:inputType="none"

android:focusable="false"

android:layout_width="match_parent"

android:layout_height="wrap_content"

tools:ignore="HardcodedText,LabelFor" />

</com.google.android.material.textfield.TextInputLayout>

  • Tạo biến toàn cục:

AutoCompleteTextView atTinhThanh;

ProgressDialog progressCovid;


ArrayList <ClassTraCuuCaNhiemCovid> arrayListTraCuuCovid;

ArrayList <String> arrayListFindCity;

ArrayAdapter <String> adapterFindCity;

  • Trong OnCreate() tạo đoạn chương trình sau:

atTinhThanh = findViewById(R.id.autocompleteTextView_covid_chontinhthanh);


if (arrayListFindCity == null) {

arrayListFindCity = new ArrayList <>();

}

if (arrayListTraCuuCovid == null) {

arrayListTraCuuCovid = new ArrayList <>();

}


progressCovid = new ProgressDialog(this);

progressCovid.setMessage(getString(R.string.doi_ti_dang_tai));


adapterFindCity = new ArrayAdapter <>(this,

R.layout.support_simple_spinner_dropdown_item,

arrayListFindCity);


arrayListTraCuuCovid.clear();

arrayListFindCity.clear();


progressCovid.show();


new AsyncTaskTraCuuCity().execute();


//....


atTinhThanh.addTextChangedListener(new TextWatcher() {

@Override

public void beforeTextChanged(CharSequence s, int start, int count, int after) {


}


@Override

public void onTextChanged(CharSequence s, int start, int before, int count) {


}


@Override

public void afterTextChanged(Editable s) {

for (int i=0; i<arrayListTraCuuCovid.size(); i++){

if (TextUtils.equals(s, arrayListTraCuuCovid.get(i).getCity())){

ResetBangSoLieu(i);

}

}

}

});

  • Viết một AsyncTask như dưới nữa là xong

@SuppressLint("StaticFieldLeak")

private class AsyncTaskTraCuuCity extends AsyncTask<Void, Void, Void>{


@Override

protected Void doInBackground(Void... voids) {

RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(getServer.linkTraCuuCovid, response -> {

if (response != null) {

for (int i=0; i<response.length(); i++) {

try {

JSONObject jsonObject = response.getJSONObject(i);

int id = jsonObject.getInt( "province_id");

String city = jsonObject.getString("province_name");

String link = jsonObject.getString("link");

String code = jsonObject.getString("source");


arrayListTraCuuCovid.add(new ClassTraCuuCaNhiemCovid(id, city, link, code));


} catch (JSONException e) {

e.printStackTrace();

}

}

for (int j=0; j < arrayListTraCuuCovid.size(); j++) {

arrayListFindCity.add(arrayListTraCuuCovid.get(j).getCity());

}

}

}, error -> { });

requestQueue.add(jsonArrayRequest);

return null;

}


@Override

protected void onPostExecute(Void unused) {

super.onPostExecute(unused);


adapterFindCity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

atTinhThanh.setAdapter(adapterFindCity);

atTinhThanh.setThreshold(1);


if (progressCovid.isShowing()) {

progressCovid.cancel();

}

}

}

  • Để lọc tìm kiếm bằng cách nhập từ bàn phím. Trong xml bỏ đoạn code inputStyte đi và trong java có thể tăng ký tự so sánh ở chỗ atTinhThanh.setThreshold(1);