如何在片段(Fragment)的编辑框被编辑时通知活动(Android)?

huangapple go评论59阅读模式
英文:

How to notify activity when a fragment's edit text has been edited (Android)?

问题

我在一个片段中有一个编辑框。
我还在我的活动中有一个字符串字段,用于启动该片段,每当编辑框中的文本更改时,我希望相应地更改活动中的字符串,但我不太确定如何做到这一点。

英文:

I have an edittext in one of my fragments.
I also have a String field in my activity that starts the fragment, and every time the text changes in the edittext, I want to change the string in my activity accordingly but I'm not sure how to do that.

答案1

得分: 1

1. 创建一个类似这样的接口

       interface OnDataChangeListener {
            fun onChange(string: String)
       }

2. 在你的 Fragment 的构造函数中传递这个接口的实例作为参数

       class MyFragment(val onDataChangeListener: OnDataChangeListener): Fragment

3. 现在当编辑框中的文本发生变化时调用这个接口的方法

       editText.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) {
            }

            override fun afterTextChanged(s: Editable?) {
                onDataChangeListener.onChange(s.toString())
            }
        })

4. 现在在我们的 Activity 中实现这个接口

       class MainActivity : AppCompatActivity(), OnDataChangeListener {

         override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              setContentView(R.layout.activity_main)

              AppUtils.addFragment(this, MyFragment(this), R.id.fragment_container)
         }

         override fun onChange(string: String) {
              val textView = findViewById<TextView>(R.id.textView)
              textView.text = string
         }
       }

这样就完成了这会给你所需的输出
英文:
  1. Create an interface like this

    interface OnDataChangeListener {
         fun onChange(string: String)
    }
    
  2. In your fragment's constructor pass an instance of this interface as a parameter

    class MyFragment(val onDataChangeListener: OnDataChangeListener): Fragment
    
  3. Now when the text changes in the edit text, call this interface's method

    editText.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) {
         }
    
         override fun afterTextChanged(s: Editable?) {
             onDataChangeListener.onChange(s.toString())
         }
     })
    
  4. Now implement this interface in our activity

    class MainActivity : AppCompatActivity(), OnDataChangeListener {
    
      override fun onCreate(savedInstanceState: Bundle?) {
           super.onCreate(savedInstanceState)
           setContentView(R.layout.activity_main)
    
           AppUtils.addFragment(this, MyFragment(this), R.id.fragment_container)
      }
    
      override fun onChange(string: String) {
           val textView = findViewById&lt;TextView&gt;(R.id.textView)
           textView.text = string
      }
    }
    

That's it. This should give you the required output.

答案2

得分: 0

创建一个接口,由Activity实现,并将其传递给你的Fragment。然后,在你的Fragment内部的EditText上使用editText.addTextWatcher()添加一个TextWatcher,并在onTextChanged内部通知监听器。这种方法基于Android文档

英文:

Create an interface that the Activity implements and pass it to your Fragment. Then, add a TextWatcher to the EditText with editText.addTextWatcher() inside your Fragment, and notify the listener inside onTextChanged. This approach is based off the Android Docs.

huangapple
  • 本文由 发表于 2020年10月23日 02:37:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64488538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定