Android:AutoCompleteTextView不在末尾添加点点并增加其大小

时间:2016-01-12 12:27:54

标签: java android user-interface autocompletetextview

我正在开发一个Android项目,我在其中使用Places API获取地址并使用AutoCompleteTextView显示它们。现在,它工作正常,但我想在地址末尾添加点点,所以它不会被切断。

我检查了其他SO链接并更改了XML中的某些值,但它无效。另外,我想知道如何设置结果以适应屏幕的宽度,高度似乎没问题。

以下是XML代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ScrollView01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_travel_register"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="32dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="32dp">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/register_travel_transparent_background"
        android:orientation="vertical"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:paddingTop="22dp">
  <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">

            <internetlegion.com.restaurantlunchify.Templates.MaterialDesignIconsTextView
                style="@style/TextViewAppearance.Title1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="0dp"
                android:text="@string/material_icon_account"
                android:textColor="@color/main_color_grey_300"
                android:textSize="22dp" />

            <AutoCompleteTextView
                android:id="@+id/restaurant_address_autocomplete"
                android:layout_width="270dp"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:ellipsize="end"
                android:scrollHorizontally="true"
                android:background="@drawable/edit_text_background_accent"
                android:ems="10"
                android:hint="Address"
                android:lineSpacingExtra="1dp"
                android:textColor="@color/main_color_grey_600"
                android:textCursorDrawable="@drawable/edit_text_cursor_drawable_main">
            </AutoCompleteTextView>

        </LinearLayout>
// Other fields and all
</All layout endings>

以下是它现在的样子:

enter image description here

请告诉我应该怎么做。谢谢。

修改

自动完成的适配器代码:

  autoCompView.setAdapter(new GooglePlacesAutocompleteAdapter(this, R.layout.search_list_item));
        //autoCompView.setOnItemClickListener(this);

        autoCompView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selection = (String) parent.getItemAtPosition(position);

                for (int i = 0; i < resultList.size(); i++) {
                    if (resultList.get(i).equals(selection)) {
                        String selectedAddress = (String) resultList.get(i);
                        String[] strings = selectedAddress.split(",");
                        if (strings.length > 2) {
                            autoCompView.setText(strings[0]);
                            restRestaurant.setRestaurantAddress(strings[0]);

                            restaurantCityField.setText(strings[1]);
                            restRestaurant.setCity(strings[1]);


                        } else {
                            autoCompView.setText(selectedAddress);
                            restRestaurant.setRestaurantAddress(selectedAddress);
                        }
                        autoCompView.requestFocus();
                        break;
                    }
                }
            }
        });

        autoCompView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                autoCompView.requestFocus();
            }
        });



  class GooglePlacesAutocompleteAdapter extends ArrayAdapter implements Filterable {

        public GooglePlacesAutocompleteAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

        @Override
        public int getCount() {
            return resultList.size();
        }

        @Override
        public String getItem(int index) {
            return (String) resultList.get(index);
        }

        @Override
        public Filter getFilter() {
            return new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    FilterResults filterResults = new FilterResults();
                    if (constraint != null) {
                        // Retrieve the autocomplete results.
                        resultList = autocomplete(constraint.toString());

                        // Assign the data to the FilterResults
                        filterResults.values = resultList;
                        filterResults.count = resultList.size();
                    }
                    return filterResults;
                }

                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    if (results != null && results.count > 0) {
                        notifyDataSetChanged();
                    } else {
                        notifyDataSetInvalidated();
                    }
                }
            };

        }
    }

search_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

2 个答案:

答案 0 :(得分:0)

您需要在search_list_item.xml

中添加android:ellipsize="end"
<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:layout_height="wrap_content" />

答案 1 :(得分:0)

请使用以下内容:

listitem.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:scrollHorizontally="true"
    android:ellipsize="end"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />

我的代码:

String[] myData = new String[]{"Africa (AF) Africa (AF) Africa (AF) Africa (AF)", "America (AFM)", "Apple (AMP)"};
AutoCompleteTextView text = (AutoCompleteTextView) findViewById(R.id.first_state);
CustomArrayAdapter adapter = new CustomArrayAdapter(this, R.layout.listitem, myData);

截图:

BNK's screenshot