设置drawable后如何获取默认的编辑线颜色?

时间:2016-12-22 05:45:12

标签: java android android-drawable android-textinputedittext

我正在使用TextInputEditText。为此,我在点击相同的编辑文本时以编程方式设置drawable。

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

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

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

我该怎么办才能让它恢复到默认状态?

设置drawable后看起来像这样。

enter image description here

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

enter image description here

代码:

    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>

任何人都可以帮忙,谢谢你..

2 个答案:

答案 0 :(得分: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"
    />

现在编码&#34; edittext_bg&#34;您也可以根据自己的选择进行自定义:

RES /抽拉/ 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> 

答案 1 :(得分:0)

但你为什么要申请一个抽奖? 无论如何,您可以在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>

ANS 2

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

textInputLayout.setBackgroundResource(R.drawable.your_bg);

并点击另一个编辑文本集

textInputLayout.setBackgroundResource(0);