我有两个编辑文本视图,我在其中添加了两个drawable。一个是edit_text_bottom_line,另一个是edit_drawable。
现在我想在编辑文本输入上显示edit_text_bottom_line drawable。所以我可以看到这样的布局:
这很有用。现在再次我想显示另一个drawable当我们再次点击编辑文本输入和浮动提示时,它应该看起来像这样:
这也有效。但是现在,如果我再次点击编辑文本视图,那么它会更改布局edit_text_bottom_line drawable如下所示:
我希望在用户开始输入时显示蓝色可绘制,直到他在编辑文本视图上以及编辑文本被聚焦之后。
如何确定编辑文本是否有焦点?我尝试使用onFocusChangeListener,如果编辑文本是聚焦的,则显示蓝色可绘制但下次单击时会出现偏差。
布局:
<?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="wrap_content"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="380dp"
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:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="07dp"
android:textSize="14sp"
android:imeOptions="actionDone"
android:background="@drawable/edit_text_bottom_line"
android:textStyle="bold"
android:inputType="text"
android:textColor="@color/lighttextcolor">
</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:imeOptions="actionDone"
android:background="@drawable/edit_text_bottom_line"
android:inputType="text"
android:textColor="@color/lighttextcolor" />
</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:layout_marginTop="02dp">
<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:imeOptions="actionDone"
android:textColorHint="@color/grey"
android:textStyle="bold"
android:background="@drawable/edit_text_bottom_line"
android:inputType="number"
android:textColor="@color/lighttextcolor" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="false"
android:layout_centerHorizontal="true"
android:layout_below="@+id/linearp"
android:layout_marginTop="130dp">
<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>
代码:
containerLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edt_Item_Name.setFocusable(false);
edt_Item_Unit.setFocusable(false);
edt_Item_quantity.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.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_Name.setText("");
edt_Item_quantity.setText("");
edt_Item_Unit.setText("");
}
});
edt_Item_Name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if(b) {
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_drawable));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
}
}
});
edt_Item_Unit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if(b) {
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_drawable));
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
}
}
});
edt_Item_quantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_drawable));
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
}
});
edt_Item_Name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edt_Item_Name.setFocusableInTouchMode(true);
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_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,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));
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
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
}
});
edt_Item_quantity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edt_Item_quantity.setFocusableInTouchMode(true);
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
}
});
edt_Item_quantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if(b) {
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_drawable));
}
else {
}
}
});
或者更确切地说应用了两个布局,我在主题中将强调色设置为蓝色,因此默认情况下编辑文本如果聚焦得很好就会采用蓝色,但是当我更改灰线的可绘制之后,怎么能我转到编辑文本的默认蓝线?
请帮助,谢谢..
答案 0 :(得分:0)
只需做一件事就可以使你的输入文字布局如下:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@android:style/TextAppearance.Small">
<EditText
android:id="@+id/user_name"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="@dimen/_10sdp"
android:hint="@string/full_name"
android:inputType="text"
android:background="@android:color/transparent"
/>
</android.support.design.widget.TextInputLayout>
它会解决你的问题。
答案 1 :(得分:0)
使用以下命令更改xml TextInputLayout和TextInputEditText:
<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:layout_marginTop="02dp"
android:textColorHint="@color/grey">
<android.support.design.widget.TextInputEditText
android:id="@+id/editTextItemQuantity"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="05dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="@string/quantity"
android:imeOptions="actionDone"
android:inputType="number"
android:textColor="@color/lighttextcolor"
android:textSize="14sp"
android:textStyle="bold"
android:theme="@style/style_edittext_color_control"/>
</android.support.design.widget.TextInputLayout>
在res/values/styles.xml
添加此内容:
<style name="style_edittext_color_control" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">@color/grey</item>
<item name="colorControlActivated">@color/blue</item>
</style>
希望这会给你正确的结果。