自定义autocompletetextview

时间:2018-01-10 07:55:24

标签: java android autocomplete

我需要使用自定义适配器实现具有自动完成功能的自定义搜索功能。我编写了自己的自定义适配器,代码如下:
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/icon_bg"
    android:orientation="vertical"
    tools:context="com.emc.kulkaa.myfinancials.MainActivity">

    <AutoCompleteTextView
        android:id="@+id/edt_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView"
        android:layout_alignStart="@+id/imageView"
        android:layout_below="@+id/imageView"
        android:layout_margin="16dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/round_border_edittext"
        android:drawableEnd="@drawable/clear"
        android:drawableLeft="@drawable/white"
        android:drawableStart="@drawable/white"
        android:ems="10"
        android:hint="@string/search_hint"
        android:singleLine="true"
        android:textColor="@color/colorWhite"
        android:textColorHint="@color/colorWhite" />

</LinearLayout>    

custom_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/cardview_color">

    <TextView
        android:id="@+id/autoCompleteItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="15sp"
        style="@style/simple"/>

</LinearLayout>  

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name = "android:autoCompleteTextViewStyle">@style/simple</item>
    </style>

    <style name="simple" parent="Widget.AppCompat.AutoCompleteTextView">
        <item name="android:popupBackground">@drawable/simpleautocustom </item>
    </style>

</resources>  

simpleautocustom.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorWhite" />

    <stroke
        android:width="0dip"
        android:color="@color/colorBlue" />

    <corners android:radius="20dip" />

    <padding
        android:bottom="10dip"
        android:left="25dip"
        android:right="25dip"
        android:top="10dip" />
</shape>  

java code

public class MainActivity extends AppCompatActivity {

    AutoCompleteTextView mEdtSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        String arr[] = {"DSA LSA", "CHILD", "AICHILES", "CHILDREN", "FRANCE", "FRENCH"};
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_item, R.id.autoCompleteItem, arr);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mEdtSearch = (AutoCompleteTextView) findViewById(R.id.edt_search);
        mEdtSearch.setFocusable(true);
        mEdtSearch.setFocusableInTouchMode(true);
        mEdtSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mEdtSearch.setAdapter(adapter);
            }
        });
    }
}  

上面的代码工作正常,这是输出的截图:
mywork
但是它仍然不符合下面给出的预期输出UI:
expected output
如何更改样式和自定义形状以便实现输出?

2 个答案:

答案 0 :(得分:1)

我已经为您尝试了一些东西,希望它能解决您的问题,您可以根据需要自定义xmls,即custom_item.xml TextView的背景。

<强> 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:background="#9A000000"
              android:orientation="vertical">

    <AutoCompleteTextView
        android:id="@+id/edt_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView"
        android:layout_alignStart="@+id/imageView"
        android:layout_below="@+id/imageView"
        android:layout_margin="16dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/round_border_edittext"
        android:drawableEnd="@drawable/clear"
        android:drawableLeft="@drawable/white"
        android:drawableStart="@drawable/white"
        android:ems="10"
        android:hint="@string/search_hint"
        android:popupBackground="@android:color/transparent"
        android:singleLine="true"
        android:textColor="@color/colorWhite"
        android:textColorHint="@color/colorWhite"/>

</LinearLayout>

<强> custom_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent">

    <TextView
        android:id="@+id/autoCompleteItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/bg_rounded_border"
        android:maxLines="1"
        android:padding="15dp"
        android:singleLine="true"
        android:textColor="@color/white"
        android:textSize="15sp"/>

</RelativeLayout>

<强> bg_rounded_border.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <solid android:color="@color/hint_color"/>
    <corners android:radius="@dimen/padding_less"/>

</shape>

休息您的MainActivity(java)代码相同,无需为列表项文本视图设置style。 以下是输出的屏幕截图。output

答案 1 :(得分:1)

您可以删除styles

只有您应将android:padding="7dp"添加到父级LinearLayout,并将android:background="@drawable/simple_auto"添加到custom_item中的TextView。这是工作。

custom_item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:padding="7dp"
    android:background="@color/cardview_color">

    <TextView
        android:id="@+id/autoCompleteItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/simple_auto"
        android:padding="10dp"
        android:textSize="15sp"/>

</LinearLayout>