当编辑文本获得焦点时从编辑文本中删除文本
Remove Text from the edit text when edit text is focused
我想在以编程方式聚焦编辑文本时删除文本,当我尝试这样做时片段 class 抛出空指针异常。
我使用的设计是 Material Text Input Layout,当我在文本输入布局中设置提示时,提示在 TextInputLayout 周围的框中向上移动,我不希望这样。我只想要盒子,
所以我在 XML 文件的 EditTextField 中将文本设置为提示,并且我想在用户单击 EditText 或聚焦 EditText 时删除文本。
这里是为了更好地理解问题而录制的屏幕
https://www.youtube.com/watch?v=6EgBymObb4A
这里是布局XML文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".screens.fragments.login_fragment"
android:background="@color/black"
android:focusable="true"
android:focusableInTouchMode="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<TextView
android:id="@+id/txt_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textAllCaps="false"
android:fontFamily="@font/raleway_semi_bold"
android:textColor="@color/white"
android:textSize="32sp"
android:letterSpacing="0.1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="@id/txt_login_des"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/txt_login_des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Your Credintials to get access"
android:textColor="#9CFFFFFF"
android:fontFamily="@font/raleway_medium"
android:textSize="12sp"
app:layout_constraintTop_toBottomOf="@id/txt_login"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="15dp"
android:layout_marginStart="20dp"/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/user_id_feild"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="30dp"
app:boxCornerRadiusBottomEnd="8dp"
app:boxCornerRadiusBottomStart="8dp"
app:boxCornerRadiusTopEnd="8dp"
app:boxCornerRadiusTopStart="8dp"
app:boxStrokeColor="@color/white"
app:boxStrokeErrorColor="#FF0000"
app:boxStrokeWidth="1dp"
android:layout_marginBottom="20dp"
app:layout_constraintTop_toBottomOf="@id/txt_login_des"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/password_feild">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/LoginUserIDField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusedByDefault="false"
android:text="User ID"
android:textColor="#74FFFFFF"
android:drawableStart="@drawable/custom_user_edit_txt_input"
android:drawablePadding="10dp"
android:padding="10dp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/password_feild"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="40dp"
app:boxCornerRadiusBottomEnd="8dp"
app:boxCornerRadiusBottomStart="8dp"
app:boxCornerRadiusTopEnd="8dp"
app:boxCornerRadiusTopStart="8dp"
app:boxStrokeColor="@color/white"
app:boxStrokeErrorColor="#FF0000"
app:boxStrokeWidth="1dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/btn_signIn">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/UserLoginPassword"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="•••••••••••••"
android:drawableStart="@drawable/custom_pass_txt_input"
android:drawablePadding="10dp"
android:textColor="#74FFFFFF"
android:padding="10dp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_signIn"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_marginBottom="10dp"
android:backgroundTint="@color/white"
android:text="Sign In"
android:textAllCaps="false"
android:textColor="@color/black"
app:rippleColor="@color/black"
app:strokeColor="@color/white"
app:strokeWidth="1dp"
app:layout_constraintBottom_toTopOf="@id/btn_register"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_register"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_marginBottom="50dp"
android:text="Register"
android:textAllCaps="false"
android:textColor="@color/white"
app:rippleColor="@color/black"
app:strokeColor="@color/white"
app:strokeWidth="1dp"
app:layout_constraintBottom_toTopOf="@id/bottom_fab_btns_lyt"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<LinearLayout
android:id="@+id/bottom_fab_btns_lyt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_facebook"
style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="50dp"
android:layout_height="50dp"
app:strokeWidth="1dp"
app:strokeColor="@color/white"
app:icon="@drawable/icons8_facebook"
app:iconTintMode="add"
app:iconSize="24dp"
app:iconTint="#000000"
app:iconPadding="0dp"
android:background="@drawable/fab_background"
android:backgroundTintMode="add"
app:backgroundTintMode="add"
app:rippleColor="@color/white"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_google"
style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="50dp"
android:layout_height="50dp"
app:strokeWidth="1dp"
app:strokeColor="@color/white"
app:icon="@drawable/google_cute_icon"
app:iconTintMode="add"
app:iconSize="24dp"
app:iconTint="#000000"
app:iconPadding="0dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:background="@drawable/fab_background"
android:backgroundTintMode="add"
app:backgroundTintMode="add"
app:rippleColor="@color/white"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_twitter"
style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="50dp"
android:layout_height="50dp"
app:strokeWidth="1dp"
app:strokeColor="@color/white"
app:icon="@drawable/google_cute_icon"
app:iconTintMode="add"
app:iconSize="24dp"
app:iconTint="#000000"
app:iconPadding="0dp"
android:background="@drawable/fab_background"
android:backgroundTintMode="add"
app:backgroundTintMode="add"
app:rippleColor="@color/white"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
片段Class
package com.ak_applications.kottry.screens.fragments
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.AnimatedVectorDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import androidx.fragment.app.Fragment
import android.view.View
import android.view.ViewGroup
import com.ak_applications.kottry.R
import com.ak_applications.kottry.databinding.FragmentLoginFragmentBinding
class login_fragment : Fragment(R.layout.fragment_login_fragment) {
private var _binding: FragmentLoginFragmentBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentLoginFragmentBinding.inflate(inflater, container, false)
val view = binding.root
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
我试过了
if(binding.LoginUserFeild.isFocused)
{
binding.LoginUserFeild.setText("")
binding.LoginUserFeild.setTextColor(Color.parseColor("#ffffff"))
}
还有
if(binding.LoginUserFeild.isFocused)
{
binding.userLoginfeild.setHint("") // which is TextInputLayout.
}
我还使用滚动布局作为片段的父布局,但正如您在屏幕录像中看到的那样,当用户单击 EditText Field 时,布局会转到顶部,但用户向下滚动,而 EditText 选择布局会获胜一直往上走,布局缺少一些我不知道的东西。
我是 android 的新手,请在这里帮助我。
binding.LoginUserFeild.setOnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
binding.LoginUserFeild.setText("")
}
}
我想在以编程方式聚焦编辑文本时删除文本,当我尝试这样做时片段 class 抛出空指针异常。
我使用的设计是 Material Text Input Layout,当我在文本输入布局中设置提示时,提示在 TextInputLayout 周围的框中向上移动,我不希望这样。我只想要盒子,
所以我在 XML 文件的 EditTextField 中将文本设置为提示,并且我想在用户单击 EditText 或聚焦 EditText 时删除文本。
这里是为了更好地理解问题而录制的屏幕
https://www.youtube.com/watch?v=6EgBymObb4A
这里是布局XML文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".screens.fragments.login_fragment"
android:background="@color/black"
android:focusable="true"
android:focusableInTouchMode="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<TextView
android:id="@+id/txt_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textAllCaps="false"
android:fontFamily="@font/raleway_semi_bold"
android:textColor="@color/white"
android:textSize="32sp"
android:letterSpacing="0.1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="@id/txt_login_des"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/txt_login_des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Your Credintials to get access"
android:textColor="#9CFFFFFF"
android:fontFamily="@font/raleway_medium"
android:textSize="12sp"
app:layout_constraintTop_toBottomOf="@id/txt_login"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="15dp"
android:layout_marginStart="20dp"/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/user_id_feild"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="30dp"
app:boxCornerRadiusBottomEnd="8dp"
app:boxCornerRadiusBottomStart="8dp"
app:boxCornerRadiusTopEnd="8dp"
app:boxCornerRadiusTopStart="8dp"
app:boxStrokeColor="@color/white"
app:boxStrokeErrorColor="#FF0000"
app:boxStrokeWidth="1dp"
android:layout_marginBottom="20dp"
app:layout_constraintTop_toBottomOf="@id/txt_login_des"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/password_feild">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/LoginUserIDField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusedByDefault="false"
android:text="User ID"
android:textColor="#74FFFFFF"
android:drawableStart="@drawable/custom_user_edit_txt_input"
android:drawablePadding="10dp"
android:padding="10dp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/password_feild"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="40dp"
app:boxCornerRadiusBottomEnd="8dp"
app:boxCornerRadiusBottomStart="8dp"
app:boxCornerRadiusTopEnd="8dp"
app:boxCornerRadiusTopStart="8dp"
app:boxStrokeColor="@color/white"
app:boxStrokeErrorColor="#FF0000"
app:boxStrokeWidth="1dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/btn_signIn">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/UserLoginPassword"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="•••••••••••••"
android:drawableStart="@drawable/custom_pass_txt_input"
android:drawablePadding="10dp"
android:textColor="#74FFFFFF"
android:padding="10dp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_signIn"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_marginBottom="10dp"
android:backgroundTint="@color/white"
android:text="Sign In"
android:textAllCaps="false"
android:textColor="@color/black"
app:rippleColor="@color/black"
app:strokeColor="@color/white"
app:strokeWidth="1dp"
app:layout_constraintBottom_toTopOf="@id/btn_register"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_register"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_marginBottom="50dp"
android:text="Register"
android:textAllCaps="false"
android:textColor="@color/white"
app:rippleColor="@color/black"
app:strokeColor="@color/white"
app:strokeWidth="1dp"
app:layout_constraintBottom_toTopOf="@id/bottom_fab_btns_lyt"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<LinearLayout
android:id="@+id/bottom_fab_btns_lyt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_facebook"
style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="50dp"
android:layout_height="50dp"
app:strokeWidth="1dp"
app:strokeColor="@color/white"
app:icon="@drawable/icons8_facebook"
app:iconTintMode="add"
app:iconSize="24dp"
app:iconTint="#000000"
app:iconPadding="0dp"
android:background="@drawable/fab_background"
android:backgroundTintMode="add"
app:backgroundTintMode="add"
app:rippleColor="@color/white"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_google"
style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="50dp"
android:layout_height="50dp"
app:strokeWidth="1dp"
app:strokeColor="@color/white"
app:icon="@drawable/google_cute_icon"
app:iconTintMode="add"
app:iconSize="24dp"
app:iconTint="#000000"
app:iconPadding="0dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:background="@drawable/fab_background"
android:backgroundTintMode="add"
app:backgroundTintMode="add"
app:rippleColor="@color/white"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_twitter"
style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="50dp"
android:layout_height="50dp"
app:strokeWidth="1dp"
app:strokeColor="@color/white"
app:icon="@drawable/google_cute_icon"
app:iconTintMode="add"
app:iconSize="24dp"
app:iconTint="#000000"
app:iconPadding="0dp"
android:background="@drawable/fab_background"
android:backgroundTintMode="add"
app:backgroundTintMode="add"
app:rippleColor="@color/white"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
片段Class
package com.ak_applications.kottry.screens.fragments
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.AnimatedVectorDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import androidx.fragment.app.Fragment
import android.view.View
import android.view.ViewGroup
import com.ak_applications.kottry.R
import com.ak_applications.kottry.databinding.FragmentLoginFragmentBinding
class login_fragment : Fragment(R.layout.fragment_login_fragment) {
private var _binding: FragmentLoginFragmentBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentLoginFragmentBinding.inflate(inflater, container, false)
val view = binding.root
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
我试过了
if(binding.LoginUserFeild.isFocused)
{
binding.LoginUserFeild.setText("")
binding.LoginUserFeild.setTextColor(Color.parseColor("#ffffff"))
}
还有
if(binding.LoginUserFeild.isFocused)
{
binding.userLoginfeild.setHint("") // which is TextInputLayout.
}
我还使用滚动布局作为片段的父布局,但正如您在屏幕录像中看到的那样,当用户单击 EditText Field 时,布局会转到顶部,但用户向下滚动,而 EditText 选择布局会获胜一直往上走,布局缺少一些我不知道的东西。
我是 android 的新手,请在这里帮助我。
binding.LoginUserFeild.setOnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
binding.LoginUserFeild.setText("")
}
}