代码之家  ›  专栏  ›  技术社区  ›  Nuwan Withanage

更改区域设置后,autocompleteTextView不工作

  •  -1
  • Nuwan Withanage  · 技术社区  · 7 年前

    在这里,“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"/>
    

    活动\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>
    

    对于英语来说,它在所有安卓版本中都可以正常工作。但是,在根据用户偏好,特别是“僧伽罗语”和“印地语”更改了“地区”语言之后,“autocompletetextview”并没有在真实设备和模拟器中加载Android Nougat(24)的国家/地区。

    最后,当调试gable设备有android nougat时,我用android“log cat”监视这个问题。也就是说,当“locale”语言为英语时,“log cat”显示以下内容,并正确加载“auto complete text view”。

    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
    

    但是,当“locale”语言为“sinhala”或“hindi”时,上面的“logcat”行不会显示,也不会加载到“auto-complete文本视图”。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Nuwan Withanage    7 年前

    用于加载“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”是一个问题。