在这里,“ AutoCompleteTextView”使用以下代码从“ Locale”加载国家列表。
public static ArrayList getCountryList()
{
Locale[] locale = Locale.getAvailableLocales();
ArrayList<String>countries = new ArrayList<String>();
String country = "";
for( Locale loc : locale ){
country = loc.getDisplayCountry();
if( country.length() > 0 && !countries.contains(country) ){
countries.add( country );
}
}
if(Build.VERSION.SDK_INT<=Build.VERSION_CODES.LOLLIPOP){
countries.add("Sri Lanka");
}
Collections.sort(countries, String.CASE_INSENSITIVE_ORDER);
return countries;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countryAutoTextView.setAdapter(new ArrayAdapter<String>(this, R.layout.country_list_spinner_loader, getCountryList()));
changeLanguageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Locale locale = new Locale(language);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = locale;
res.updateConfiguration(conf, dm);
finish();
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
}
});
}
country_list_spinner_loader.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/location_text_view"
android:maxLines="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="start"
android:ellipsize="marquee"
android:textColor="@color/colorBlack"
android:textSize="14sp"/>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<LinearLayout
android:id="@+id/first_Screen_greeting_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/greeting_layout_height"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#80000000"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="2dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@android:drawable/ic_menu_search"
android:layout_gravity="center"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true">
<AutoCompleteTextView
android:id="@+id/main_activity_country_auto"
android:layout_width="match_parent"
android:layout_height="@dimen/auto_text_height"
android:focusableInTouchMode="true"
android:textColor="@color/colorWhite"
android:textSize="@dimen/country_auto_text_size"
android:theme="@style/Autocomplete" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="@dimen/time_image_view_left_margin"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</LinearLayout>
对于英语,它适用于所有Android版本。但是在根据用户喜好特别更改了“语言环境”语言“ Sinhala”和“ Hindi”之后,“ AutoCompleteTextView”未在实际设备和仿真器中加载Android Nougat(24)的国家/地区。
最后,当可调试山墙设备具有Android Nougat时,我会监视android'Log cat'的问题。也就是说,当“语言环境”语言为英语时,将显示“日志猫”,并正确加载“自动完成文本视图”。
07-03 15:20:01.116 17445-17445/com.sample D/AbsListView: Get MotionRecognitionManager
07-03 15:20:01.119 17445-17445/com.sample D/MotionRecognitionManager: mSContextService = com.samsung.android.hardware.context.ISemContextService$Stub$Proxy@5ba90df
07-03 15:20:01.121 17445-17445/com.sample D/MotionRecognitionManager: motionService = com.samsung.android.gesture.IMotionRecognitionService$Stub$Proxy@40dd12c
motionService = com.samsung.android.gesture.IMotionRecognitionService$Stub$Proxy@40dd12c
07-03 15:20:01.127 17445-17445/com.sample D/TextView: setTypeface with style : 0
07-03 15:20:01.137 17445-17445/com.sample D/ViewRootImpl@ce15bfb[PopupWindow:333598a]: ThreadedRenderer.create() translucent=true
07-03 15:20:01.143 17445-17445/com.samplea D/InputTransport: Input channel constructed: fd=77
07-03 15:20:01.144 17445-17445/com.samplea D/ViewRootImpl@ce15bfb[PopupWindow:333598a]: setView = android.widget.PopupWindow$PopupDecorView{51cd418 V.E...... ......I. 0,0-0,0} touchMode=true
07-03 15:20:01.147 17445-17445/com.sample D/ViewRootImpl@ce15bfb[PopupWindow:333598a]: dispatchAttachedToWindow
07-03 15:20:01.177 17445-17445/com.sample D/ViewRootImpl@ce15bfb[PopupWindow:333598a]: Relayout returned: oldFrame=[0,0][0,0] newFrame=[0,664][1440,820] result=0x27 surface={isValid=true 491593586176} surfaceGenerationChanged=true
mHardwareRenderer.initialize() mSurface={isValid=true 491593586176} hwInitialized=true
07-03 15:20:01.180 17445-17575/com.sample D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000, [1696x412]-format:1
07-03 15:20:01.181 17445-17445/com.sample D/AbsListView: onsize change
07-03 15:20:01.185 17445-17445/com.sample D/TextView: setTypeface with style : 0
07-03 15:20:01.207 17445-17445/com.sample D/ViewRootImpl@ce15bfb[PopupWindow:333598a]: Relayout returned: oldFrame=[0,664][1440,820] newFrame=[0,664][1440,820] result=0x1 surface={isValid=true 491593586176} surfaceGenerationChanged=false
07-03 15:20:01.218 17445-17445/com.sample D/ViewRootImpl@ce15bfb[PopupWindow:333598a]: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
但是,当“区域设置”语言为“僧伽罗语”或“印地语”时,“ Logcat”行上方将不会显示,并且不会将任何内容加载到“自动完成文本视图”中。
答案 0 :(得分:0)
用于加载“ AutoCompleteTextView”的国家/地区列表。
final ArrayAdapter<String>adapter = new ArrayAdapter<String>(this, R.layout.country_list_spinner_loader, DataLists.getCountryList());
countryAutoTextView.setAdapter(adapter);
adapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
Log.d("MAIN_COUNTRY_ADAPTER", "dataset changed");
Object item = adapter.getItem(0);
Log.d("MAIN_COUNTRY_ADAPTER", "item.toString "+ item.toString());
}
});
我使用硬编码国家/地区来代替列表。
public static ArrayList getCountryList()
{
Locale[] locale = Locale.getAvailableLocales();
ArrayList<String>countries = new ArrayList<String>();
String country = "";
for( Locale loc : locale ){
country = loc.getDisplayCountry();
if( country.length() > 0 && !countries.contains(country) ){
countries.add( country );
}
}
if(Build.VERSION.SDK_INT<=Build.VERSION_CODES.LOLLIPOP){
countries.add("Sri Lanka");
}
Collections.sort(countries, String.CASE_INSENSITIVE_ORDER);
Log.d("DATA_LIST_CLASS", "COUNTRIES_LIST_SIZE_IS: " + countries.size() + countries.get(0) + countries.get(1));
return countries;
}
因为这也使用“语言环境”来加载国家/地区。更改设备语言并加载“ AutoCompleteTextView”是一个问题。