选择为 textView 和 editText 添加边框形状时,
when selected add border shape for textView and editText ,
我想为 TextView
和 EditText
创建边框形状,并在 view
被 选中时显示它。
就像这张图片.
在 Drawable 中创建 xml 文件并使用此代码:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/transparent" />
<corners android:radius="5dp" />
<stroke
android:width="3dp"
android:color="@color/yellow" />
</shape>
设置为EditText的背景
使用 android:width="0dp"
创建另一个后,当用户单击时,您可以简单地更改它们。
.....
editText1.setOnClickListener{
editText1.setBackground(shape1);
editText2.setBackground(shape0);
}
editText2.setOnClickListener{
editText1.setBackground(shape0);
editText2.setBackground(shape1);
}
您无法复制和执行此代码,但这是您的指南。!
您应该使用可绘制选择器来实现您的 UI。
首先创建一个background_edit_text_default.xml
作为EditText未被用户选中时的背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#333D46" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
然后创建一个background_edit_text_selected.xml
,当EditText被用户选中时作为背景。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#EDB90E" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
接下来创建一个 background_edit_text.xml
用作 EditText 的背景。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/background_edit_text_default" android:state_focused="false" />
<item android:drawable="@drawable/background_edit_text_selected" android:state_focused="true" />
</selector>
最后在布局文件中将 background_edit_text.xml
设置为 EditText 的背景,例如 activity_main
.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/conteiner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_edit_text" />
<TextView
android:layout_width="match_parent"
android:layout_height="10dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_edit_text" />
</LinearLayout>
大功告成,无需在代码中添加任何内容。
试试这个方法
MainActivity
public class MainActivity extends AppCompatActivity {
EditText edtEmail, edtPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtEmail = findViewById(R.id.edtEmail);
edtPassword = findViewById(R.id.edtPassword);
edtEmail.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
edtEmail.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.edt_focus_bg));
else
edtEmail.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.edt_unfocus_bg));
}
});
edtPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
edtPassword.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.edt_focus_bg));
else
edtPassword.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.edt_unfocus_bg));
}
});
}
}
layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical"
tools:context=".FirstFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="Email"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/edtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:background="@drawable/edt_focus_bg"
android:imeOptions="actionNext"
android:inputType="textEmailAddress" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="Password"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/edtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:background="@drawable/edt_unfocus_bg"
android:imeOptions="actionNext"
android:inputType="textPassword" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="10dp"
android:background="@drawable/edt_focus_bg"
android:gravity="center"
android:text="Login"
android:textColor="#fae81e" />
</LinearLayout>
drawable/edt_focus_bg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#fae81e" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
drawable/edt_unfocus_bg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#333D46" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
OUTPUT
https://www.youtube.com/watch?v=OvCqTc_y124
编辑
如果您想在登录中添加相同的效果TextView
,请按照以下步骤操作
- 在
res/color
目录中创建一个 tv_text_color.xml
,如
- 在
res/drawable
目录中创建一个 tv_bg.xml
,如
Sample code for tv_text_color.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Focused and not pressed -->
<item android:state_focused="true"
android:state_pressed="false"
android:color="#000000" />
<!-- Focused and pressed -->
<item android:state_focused="true"
android:state_pressed="true"
android:color="#fae81e" />
<!-- Unfocused and pressed -->
<item android:state_focused="false"
android:state_pressed="true"
android:color="#fae81e" />
<!-- Default color -->
<item android:color="#000000" />
</selector>
Sample code for tv_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/edt_focus_bg"
android:state_selected="true"/>
<item android:drawable="@drawable/edt_focus_bg"
android:state_pressed="true"/>
<item android:drawable="@drawable/edt_unfocus_bg"/>
</selector>
Now use like this in your TextView
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="10dp"
android:textColor="@color/tv_text_color"
android:background="@drawable/tv_bg"
android:clickable="true"
android:gravity="center"
android:text="Login"
/>
OUTPUT with textview
https://www.youtube.com/watch?v=Iu898vafXEk
编辑 2
You can also do the same effect for EditText
using selector
在 res/drawable
目录中创建一个 edt_selector.xml
文件,如下所示
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/edt_focus_bg"
android:state_focused="true"/>
<item android:drawable="@drawable/edt_unfocus_bg"/>
</selector>
Now use in your editext
like this
<EditText
android:id="@+id/edtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:background="@drawable/edt_selector"
android:imeOptions="actionNext"
android:inputType="textPassword" />
将此添加到您的颜色文件夹:
选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="your_border_color"/>
<item android:color="@android:color/transparent"/>
</selector>
将其添加到您的可绘制文件夹中:
背景:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="back_ground_color" />
<corners android:radius="5dp" />
<stroke
android:width="3dp"
android:color="@color/selector" />
</shape>
到您的编辑文本:
<EditText>
android:background="@drawable/background"
</EditText>
我想为 TextView
和 EditText
创建边框形状,并在 view
被 选中时显示它。
就像这张图片.
在 Drawable 中创建 xml 文件并使用此代码:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/transparent" />
<corners android:radius="5dp" />
<stroke
android:width="3dp"
android:color="@color/yellow" />
</shape>
设置为EditText的背景
使用 android:width="0dp"
创建另一个后,当用户单击时,您可以简单地更改它们。
.....
editText1.setOnClickListener{
editText1.setBackground(shape1);
editText2.setBackground(shape0);
}
editText2.setOnClickListener{
editText1.setBackground(shape0);
editText2.setBackground(shape1);
}
您无法复制和执行此代码,但这是您的指南。!
您应该使用可绘制选择器来实现您的 UI。
首先创建一个background_edit_text_default.xml
作为EditText未被用户选中时的背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#333D46" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
然后创建一个background_edit_text_selected.xml
,当EditText被用户选中时作为背景。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#EDB90E" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
接下来创建一个 background_edit_text.xml
用作 EditText 的背景。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/background_edit_text_default" android:state_focused="false" />
<item android:drawable="@drawable/background_edit_text_selected" android:state_focused="true" />
</selector>
最后在布局文件中将 background_edit_text.xml
设置为 EditText 的背景,例如 activity_main
.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/conteiner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_edit_text" />
<TextView
android:layout_width="match_parent"
android:layout_height="10dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_edit_text" />
</LinearLayout>
大功告成,无需在代码中添加任何内容。
试试这个方法
MainActivity
public class MainActivity extends AppCompatActivity {
EditText edtEmail, edtPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtEmail = findViewById(R.id.edtEmail);
edtPassword = findViewById(R.id.edtPassword);
edtEmail.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
edtEmail.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.edt_focus_bg));
else
edtEmail.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.edt_unfocus_bg));
}
});
edtPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
edtPassword.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.edt_focus_bg));
else
edtPassword.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.edt_unfocus_bg));
}
});
}
}
layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical"
tools:context=".FirstFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="Email"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/edtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:background="@drawable/edt_focus_bg"
android:imeOptions="actionNext"
android:inputType="textEmailAddress" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="Password"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/edtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:background="@drawable/edt_unfocus_bg"
android:imeOptions="actionNext"
android:inputType="textPassword" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="10dp"
android:background="@drawable/edt_focus_bg"
android:gravity="center"
android:text="Login"
android:textColor="#fae81e" />
</LinearLayout>
drawable/edt_focus_bg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#fae81e" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
drawable/edt_unfocus_bg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#333D46" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
OUTPUT
https://www.youtube.com/watch?v=OvCqTc_y124
编辑
如果您想在登录中添加相同的效果TextView
,请按照以下步骤操作
- 在
res/color
目录中创建一个tv_text_color.xml
,如
- 在
res/drawable
目录中创建一个tv_bg.xml
,如
Sample code for
tv_text_color.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Focused and not pressed -->
<item android:state_focused="true"
android:state_pressed="false"
android:color="#000000" />
<!-- Focused and pressed -->
<item android:state_focused="true"
android:state_pressed="true"
android:color="#fae81e" />
<!-- Unfocused and pressed -->
<item android:state_focused="false"
android:state_pressed="true"
android:color="#fae81e" />
<!-- Default color -->
<item android:color="#000000" />
</selector>
Sample code for
tv_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/edt_focus_bg"
android:state_selected="true"/>
<item android:drawable="@drawable/edt_focus_bg"
android:state_pressed="true"/>
<item android:drawable="@drawable/edt_unfocus_bg"/>
</selector>
Now use like this in your TextView
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="10dp"
android:textColor="@color/tv_text_color"
android:background="@drawable/tv_bg"
android:clickable="true"
android:gravity="center"
android:text="Login"
/>
OUTPUT with
textview
https://www.youtube.com/watch?v=Iu898vafXEk
编辑 2
You can also do the same effect for
EditText
using selector
在 res/drawable
目录中创建一个 edt_selector.xml
文件,如下所示
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/edt_focus_bg"
android:state_focused="true"/>
<item android:drawable="@drawable/edt_unfocus_bg"/>
</selector>
Now use in your
editext
like this
<EditText
android:id="@+id/edtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:background="@drawable/edt_selector"
android:imeOptions="actionNext"
android:inputType="textPassword" />
将此添加到您的颜色文件夹:
选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="your_border_color"/>
<item android:color="@android:color/transparent"/>
</selector>
将其添加到您的可绘制文件夹中:
背景:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="back_ground_color" />
<corners android:radius="5dp" />
<stroke
android:width="3dp"
android:color="@color/selector" />
</shape>
到您的编辑文本:
<EditText>
android:background="@drawable/background"
</EditText>