android.text.TextWatcher

本文详细介绍了TextWatcher接口的功能和使用方法,包括beforeTextChanged、onTextChanged及afterTextChanged三个关键方法的作用和参数含义,帮助开发者更好地理解和使用此接口。



/**
 * When an object of a type is attached to an Editable, its methods will
 * be called when the text is changed.
 */
public interface TextWatcher extends NoCopySpan {
    /**
     * This method is called to notify you that, within <code>s</code>,
     * the <code>count</code> characters beginning at <code>start</code>
     * are about to be replaced by new text with length <code>after</code>.
     * It is an error to attempt to make changes to <code>s</code> from
     * this callback.
     */
    public void beforeTextChanged(CharSequence s, int start,
                                  int count, int after);
    /**
     * This method is called to notify you that, within <code>s</code>,
     * the <code>count</code> characters beginning at <code>start</code>
     * have just replaced old text that had length <code>before</code>.
     * It is an error to attempt to make changes to <code>s</code> from
     * this callback.
     */
    public void onTextChanged(CharSequence s, int start, int before, int count);

    /**
     * This method is called to notify you that, somewhere within
     * <code>s</code>, the text has been changed.
     * It is legitimate to make further changes to <code>s</code> from
     * this callback, but be careful not to get yourself into an infinite
     * loop, because any changes you make will cause this method to be
     * called again recursively.
     * (You are not told where the change took place because other
     * afterTextChanged() methods may already have made other changes
     * and invalidated the offsets.  But if you need to know here,
     * you can use {@link Spannable#setSpan} in {@link #onTextChanged}
     * to mark your place and then look up from here where the span
     * ended up.
     */
    public void afterTextChanged(Editable s);
}
说明:

public void beforeTextChanged(CharSequence s, int start, int count, int after)

s改变前的text字符,start当前起始位置,count被替换字符的长度(删除时为0),after新替代字符的长度


public void onTextChanged(CharSequence s, int start, int before, int count)

s改变后的text字符,start替换前的起始位置(同beforeTextChanged),before被替换字符的长度,count新替代字符的长度


public void afterTextChanged(Editable s)

s改变后的text字符



没有成功改变文本package com.example.bulbpage.setting import android.content.Context import androidx.fragment.app.viewModels import android.os.Bundle import android.text.Editable import android.text.TextWatcher import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.MotionEvent import android.view.View import android.view.ViewGroup import androidx.core.content.ContextCompat import androidx.navigation.fragment.findNavController import com.example.bulbpage.R import com.example.bulbpage.databinding.FragmentDeviceNameBinding class DeviceNameFragment : Fragment() { interface OnDataPassListener { fun onDataPass(data: String) } private var dataPassListener: OnDataPassListener? = null private var _binding: FragmentDeviceNameBinding? = null private val binding get() = _binding!! private val viewModel: DeviceNameViewModel by viewModels() override fun onAttach(context: Context) { super.onAttach(context) dataPassListener = parentFragment as? OnDataPassListener } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { _binding = FragmentDeviceNameBinding.inflate(inflater, container, false) return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) // 初始化图标 binding.typeIn.setCompoundDrawablesWithIntrinsicBounds(R.drawable.devicename, 0, 0, 0) // 输入框监听 binding.typeIn.addTextChangedListener(object : TextWatcher { override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { if (!s.isNullOrEmpty()) { binding.typeIn.setCompoundDrawablesWithIntrinsicBounds( R.drawable.devicename, 0, R.drawable.delete, 0 ) binding.textView2.setTextColor(ContextCompat.getColor(requireContext(), R.color.black)) } else { binding.typeIn.setCompoundDrawablesWithIntrinsicBounds( R.drawable.devicename, 0, 0, 0 ) binding.textView2.setTextColor(ContextCompat.getColor(requireContext(), R.color.text_gray)) } } override fun afterTextChanged(s: Editable?) {} }) // 点击保存 binding.textView2.setOnClickListener { val inputText = binding.typeIn.text.toString() dataPassListener?.onDataPass(inputText) findNavController().popBackStack() } // 删除图标点击 binding.typeIn.setOnTouchListener { view, event -> val DRAWABLE_RIGHT = 2 if (event.action == MotionEvent.ACTION_UP) { val drawable = binding.typeIn.compoundDrawables[DRAWABLE_RIGHT] if (drawable != null && event.rawX >= (binding.typeIn.right - drawable.bounds.width())) { binding.typeIn.setText("") return@setOnTouchListener true } } false } binding.buttonBack.setOnClickListener { findNavController().navigate(R.id.action_DeviceNameFragment_to_EditFragment) } } override fun onDestroyView() { super.onDestroyView() _binding = null } }
最新发布
08-28
package com.example.bulbpage.setting import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.findNavController import com.example.bulbpage.R import com.example.bulbpage.databinding.FragmentEditBinding import kotlinx.coroutines.launch class EditFragment : Fragment() { private var _binding: FragmentEditBinding? = null private val binding get() = _binding!! private val viewModel: EditViewModel by viewModels() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { _binding = FragmentEditBinding.inflate(inflater, container, false) return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) binding.button1.setOnClickListener { findNavController().navigate(R.id.action_EditFragment_to_SelectIconFragment) } binding.button2.setOnClickListener { findNavController().navigate(R.id.action_EditFragment_to_DeviceNameFragment) } // 观察 ViewModel 中的 deviceName viewLifecycleOwner.lifecycleScope.launch { viewModel.deviceName.collect { binding.textView20.text = it } } } override fun onDestroyView() { super.onDestroyView() _binding = null } } package com.example.bulbpage.setting import android.os.Bundle 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 androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.navigation.fragment.findNavController import com.example.bulbpage.R import com.example.bulbpage.databinding.FragmentDeviceNameBinding class DeviceNameFragment : Fragment() { private var _binding: FragmentDeviceNameBinding? = null private val binding get() = _binding!! private val viewModel: EditViewModel by viewModels() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { _binding = FragmentDeviceNameBinding.inflate(inflater, container, false) return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) // 初始化图标 binding.typeIn.setCompoundDrawablesWithIntrinsicBounds(R.drawable.devicename, 0, 0, 0) // 输入框监听 binding.typeIn.addTextChangedListener(object : TextWatcher { override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { if (!s.isNullOrEmpty()) { binding.typeIn.setCompoundDrawablesWithIntrinsicBounds( R.drawable.devicename, 0, R.drawable.delete, 0 ) binding.textView2.setTextColor(resources.getColor(R.color.black)) } else { binding.typeIn.setCompoundDrawablesWithIntrinsicBounds( R.drawable.devicename, 0, 0, 0 ) binding.textView2.setTextColor(resources.getColor(R.color.text_gray)) } } override fun afterTextChanged(s: Editable?) {} }) // 点击保存 binding.textView2.setOnClickListener { val inputText = binding.typeIn.text.toString() viewModel.updateDeviceName(inputText) findNavController().popBackStack() } // 删除图标点击 binding.typeIn.setOnTouchListener { view, event -> val DRAWABLE_RIGHT = 2 if (event.action == MotionEvent.ACTION_UP) { val drawable = binding.typeIn.compoundDrawables[DRAWABLE_RIGHT] if (drawable != null && event.rawX >= (binding.typeIn.right - drawable.bounds.width())) { binding.typeIn.setText("") return@setOnTouchListener true } } false } binding.buttonBack.setOnClickListener { findNavController().navigate(R.id.action_DeviceNameFragment_to_EditFragment) } } override fun onDestroyView() { super.onDestroyView() _binding = null } } textview直接没有文字了
08-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值