2

我正在使用一个TextInputEditText. 为此,我在单击相同的编辑文本时以编程方式设置可绘制对象。

默认情况下,编辑文本显示蓝色底线和提示颜色为蓝色,即我在主题中设置的强调色。

现在我在以编程方式设置drawable之后,我想将编辑文本显示为默认值。

我试图将drawable设置为null或背景资源为0,但这完全从编辑文本中删除了drawable并且底线消失了。

我应该怎么做才能让它恢复默认值?

设置drawable后看起来像这样。

在此处输入图像描述

后来drawable继续,但如果我点击edt_unit我试过 edt_Item_Name.setBackgroundResource(0);:所以底线消失了。

在此处输入图像描述

代码:

    containerLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            edt_Item_Name.setFocusable(false);
            edt_Item_Unit.setFocusable(false);

            linearLayoutRecycleView.setVisibility(View.GONE);
            linearLayoutUnits.setVisibility(View.GONE);
            textInput_Item_quantity.setVisibility(View.VISIBLE);
            textInput_Item_Unit.setVisibility(View.VISIBLE);
            textInput_Item_Unit.setBackgroundResource(0);
            textInput_Item_name.setBackgroundResource(0);

            edt_Item_Name.setText("");
            edt_Item_quantity.setText("");
            edt_Item_Unit.setText("");

        }
    });

    edt_Item_Name.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            edt_Item_Name.setFocusableInTouchMode(true);
            edt_Item_Unit.setFocusable(false);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
        mRecyclerView.setAdapter(mAdapter);

        edt_Item_Name.setText("");


        edt_Item_Name.setBackgroundResource(R.drawable.edit_text_bottom_line);
            edt_Item_Name.setHintTextColor(ContextCompat.getColor(SearchActivity.this,R.color.grey));
        linearLayoutRecycleView.setVisibility(View.VISIBLE);

            linearLayoutUnits.setVisibility(View.GONE);
        textInput_Item_quantity.setVisibility(View.GONE);
        textInput_Item_Unit.setVisibility(View.GONE);

        textInput_Item_name.setBackgroundResource(R.drawable.purple_bg);
            textInput_Item_Unit.setBackgroundResource(0);

        }
    });


    edt_Item_Name.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            SearchActivity.this.mAdapter.getFilter().filter(cs);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub



        }
    });

    edt_Item_Unit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            edt_Item_Unit.setFocusable(true);
            edt_Item_Name.setFocusable(false);
            edt_Item_Unit.setFocusableInTouchMode(true);

            edt_Item_Name.setBackgroundResource(0);

            mUnitsRecyclerView.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
            mUnitsRecyclerView.setAdapter(mUnitsAdapter);

            edt_Item_Unit.setText("");


            edt_Item_Unit.setBackgroundResource(R.drawable.edit_text_bottom_line);
            edt_Item_Unit.setHintTextColor(ContextCompat.getColor(SearchActivity.this,R.color.grey));



            linearLayoutUnits.setVisibility(View.VISIBLE);
            linearLayoutRecycleView.setVisibility(View.GONE);
            textInput_Item_quantity.setVisibility(View.GONE);

            textInput_Item_Unit.setBackgroundResource(R.drawable.purple_bg);
        }
    });

    edt_Item_Unit.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            SearchActivity.this.mUnitsAdapter.getFilter().filter(cs);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });

xml:

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.kiranacustomerapp.Activities.SearchActivity"
    tools:showIn="@layout/activity_search">


        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/white"
                android:orientation="vertical"
                android:id="@+id/linearLayoutContainer">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/linearp">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_height="wrap_content"
            android:id="@+id/linear1"
            android:layout_marginTop="10dp">


            <android.support.design.widget.TextInputLayout
                android:id="@+id/input_layout_item_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:background="@drawable/bg"
                    android:id="@+id/linearLayoutSpinner"
                    android:visibility="gone">

                    <android.support.v7.widget.RecyclerView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#ffffff"
                        android:id="@+id/recyclerview"
                        android:layout_margin="08dp"></android.support.v7.widget.RecyclerView>

                </LinearLayout>

            <android.support.design.widget.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:id="@+id/editTextItemName"
                android:layout_gravity="center_horizontal|center_vertical"
                android:hint="@string/item_name"
                android:focusable="false"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="07dp"
                android:textSize="14sp"
                android:textStyle="bold">

</android.support.design.widget.TextInputEditText>
            </android.support.design.widget.TextInputLayout>
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linear2"
            android:layout_below="@+id/linear1"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/input_layout_item_unit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:background="@drawable/bg"
                    android:id="@+id/linearLayoutUnits"
                    android:visibility="gone">

                    <android.support.v7.widget.RecyclerView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#ffffff"
                        android:id="@+id/recyclerviewUnits"
                        android:layout_margin="08dp"></android.support.v7.widget.RecyclerView>

                </LinearLayout>
                <android.support.design.widget.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:focusable="false"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginBottom="07dp"
                    android:id="@+id/editTextItemUnit"
                    android:layout_gravity="center_horizontal"
                    android:hint="@string/unit"
                    android:textSize="14sp"
                    android:textStyle="bold" />
            </android.support.design.widget.TextInputLayout>
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linear3"
            android:layout_below="@+id/linear2">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/input_layout_item_quantity"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp">

                <android.support.design.widget.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginBottom="05dp"
                    android:id="@+id/editTextItemQuantity"
                    android:layout_gravity="center_horizontal"
                    android:hint="@string/quantity"
                    android:textSize="14sp"
                    android:textColorHint="@color/grey"
                    android:textStyle="bold" />
            </android.support.design.widget.TextInputLayout>
        </LinearLayout>
        </LinearLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true">

        <Button
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:text="Select"
            style="?android:attr/borderlessButtonStyle"
            android:id="@+id/buttonSelect"
            android:background="@drawable/btn_hlf_blue"
            android:textColor="@android:color/white"
            android:textSize="12sp"
            android:layout_alignParentBottom="false"
            android:layout_centerHorizontal="true" />
        </RelativeLayout>
            </RelativeLayout>
        </ScrollView>

</RelativeLayout>

有谁能帮忙谢谢。。

4

2 回答 2

1

你也可以试试这个:

<EditText
    android:id="@+id/et"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="Input your country"
    android:background="@drawable/edittext_bg"
    android:padding="10dp"
    />

现在为“edittext_bg”编写代码,您也可以根据自己的选择进行自定义:

res/drawable/edittext_bg.xml

    <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <!-- Draw a 2dp width border around shape -->
            <stroke
                android:color="#ff1e0c"
                android:width="2dp"
                />
        </shape>
    </item>
    <!-- Overlap the left, top and right border using background color  -->
    <item
        android:bottom="2dp"
        >
        <shape android:shape="rectangle">
            <solid android:color="#fffbce"/>
        </shape>
    </item>
</layer-list> 
于 2016-12-22T06:10:29.620 回答
0

但是你为什么要对它应用一个drawable呢?无论如何,您可以在其中包含您的编辑文本的view下方添加。textInput layout并设置 edt_Item_Name.setBackgroundResource(0);

你的 xml 看起来像这样。

<android.support.design.widget.TextInputLayout
   ...
 >

<EditText
    ....

   />
</android.support.design.widget.TextInputLayout>



<View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/your_color"
        ></View>

更新

    <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:id="@+id/editTextItemName"
            android:layout_gravity="center_horizontal|center_vertical"
            android:hint="@string/item_name"
            android:focusable="false"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="07dp"
            android:textSize="14sp"
            android:textStyle="bold">

<android.support.design.widget.TextInputEditText>
                </android.support.design.widget.TextInputLayout>

在此处添加视图

                   <View
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:background="@color/your_color">
                   </View>
        </LinearLayout>

答案 2

TextInputLayout textInputLayout =(TextInputLayout)findViewById(R.id.input_layout_item_name);

textInputLayout.setBackgroundResource(R.drawable.your_bg);

然后点击另一个编辑文本集

textInputLayout.setBackgroundResource(0);

于 2016-12-22T05:56:09.530 回答