主要活动
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\u主
<?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>
可绘制/edt\U焦点\U背景
<?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>
<?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>
输出
https://www.youtube.com/watch?v=OvCqTc_y124
编辑
TextView
然后执行以下步骤
-
创建
tv_text_color.xml
res/color
目录式
-
创建
tv_bg.xml
res/drawable
目录式
的示例代码
<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>
的示例代码
电视_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>
现在在你的文本视图中这样使用
<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"
/>
输出
具有
textview
https://www.youtube.com/watch?v=Iu898vafXEk
你也可以做同样的效果
EditText
使用选择器
创建
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>
现在用在你的
editext
这样地
<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" />