聚焦编辑文本时,从编辑文本中删除文本



当编辑文本以编程方式聚焦时,我想删除文本,当我尝试这样做时,fragment类抛出null指针异常。

我使用的设计是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>

碎片类

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时,布局会转到顶部,但当用户选择EditText时向下滚动,布局不会一直向上,布局缺失了一些我不知道的东西。

我是安卓系统的新手,请在这里帮我。

binding.LoginUserFeild.setOnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
binding.LoginUserFeild.setText("")      
}
}

最新更新