我希望你能帮助我。 我正在尝试实现一个edittext,当您键入内容时,将出现一个按钮(x)以清除该edittext。我希望在editext为空时禁用此(x)按钮。我能够实现用于清除edittext的按钮,但是当edittext为空时,我无法使按钮消失。有人可以帮忙吗?
compileSdkVersion 28
ext.kotlin_version ='1.3.21'
classpath'com.android.tools.build:gradle:3.3.2'
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.content.ContextCompat
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import br.com.adrianofpinheiro.testesantander.R
import br.com.adrianofpinheiro.testesantander.utils.PhoneNumberFormatType
import br.com.adrianofpinheiro.testesantander.utils.PhoneNumberFormatter
import kotlinx.android.synthetic.main.fragment_contato.*
import kotlinx.android.synthetic.main.fragment_contato.view.*
import java.lang.ref.WeakReference
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Activities that contain this fragment must implement the
* [ContatoFragment.OnFragmentInteractionListener] interface
* to handle interaction events.
* Use the [ContatoFragment.newInstance] factory method to
* create an instance of this fragment.
*
*/
class ContatoFragment : Fragment() {
val COMPOUND_DRAWABLE_RIGHT_INDEX = 2
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view: View = inflater.inflate(R.layout.fragment_contato, container, false)
addRightCancelDrawable(view.findViewById(R.id.etNome))
addRightCancelDrawable(view.findViewById(R.id.etEmail))
addRightCancelDrawable(view.findViewById(R.id.etTelefone))
val nome = view.findViewById<EditText>(R.id.etNome)
nome.onRightDrawableClicked {
it.text.clear()
}
val email = view.findViewById<EditText>(R.id.etEmail)
email?.onRightDrawableClicked {
it.text.clear()
}
val telefone = view.findViewById<EditText>(R.id.etTelefone)
telefone?.onRightDrawableClicked {
it.text.clear()
}
view.btnEnviar.setOnClickListener {
if (!etNome.text.isNullOrEmpty() && !etEmail.text.isNullOrEmpty() && !etTelefone.text.isNullOrEmpty()) {
criarFragmentContato()
} else {
etNome.error = getString(R.string.msg_error_form_nome)
etEmail.error = getString(R.string.msg_error_form_email)
etTelefone.error = getString(R.string.msg_error_form_telefone)
}
}
//Mask Phone
val editText: EditText = view.findViewById(R.id.etTelefone)
val country = PhoneNumberFormatType.PT_BR
val phoneFormatter = PhoneNumberFormatter(WeakReference(editText), country)
editText.addTextChangedListener(phoneFormatter)
return view
}
fun criarFragmentContato() {
val trasaction = fragmentManager?.beginTransaction()
val fragment = ContatoEnviadoFragment()
trasaction?.replace(R.id.fragment_container, fragment)
trasaction?.addToBackStack(null)
trasaction?.commit()
}
private fun addRightCancelDrawable(editText: EditText) {
val cancel = ContextCompat.getDrawable(requireContext(), R.drawable.ic_cancel_gray_24dp)
cancel?.setBounds(0, 0, cancel.intrinsicWidth, cancel.intrinsicHeight)
editText.setCompoundDrawables(null, null, cancel, null)
}
fun EditText.makeClearableEditText(
onIsNotEmpty: (() -> Unit)?,
onClear: (() -> Unit)?,
clearDrawable: Drawable
) {
val updateRightDrawable = {
this.setCompoundDrawables(
null, null,
if (text.isNotEmpty()) clearDrawable else null,
null
)
}
updateRightDrawable()
this.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
if (text.isNotEmpty()) {
onIsNotEmpty?.invoke()
}
updateRightDrawable()
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
})
this.onRightDrawableClicked {
this.text.clear()
this.setCompoundDrawables(null, null, null, null)
onClear?.invoke()
this.requestFocus()
}
}
fun EditText.makeClearableEditText(onIsNotEmpty: (() -> Unit)?, onCleared: (() -> Unit)?) {
compoundDrawables[COMPOUND_DRAWABLE_RIGHT_INDEX]?.let { clearDrawable ->
makeClearableEditText(onIsNotEmpty, onCleared, clearDrawable)
}
}
fun EditText.onRightDrawableClicked(onClicked: (view: EditText) -> Unit) {
this.setOnTouchListener { v, event ->
var hasConsumed = false
if (v is EditText) {
if (event.x >= v.width - v.totalPaddingRight) {
if (event.action == MotionEvent.ACTION_UP) {
onClicked(this)
}
hasConsumed = true
}
}
hasConsumed
}
}
}