英文:
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
}
}
这样就完成了。这会给你所需的输出。
英文:
-
Create an interface like this
interface OnDataChangeListener { fun onChange(string: String) }
-
In your fragment's constructor pass an instance of this interface as a parameter
class MyFragment(val onDataChangeListener: OnDataChangeListener): Fragment
-
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()) } })
-
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<TextView>(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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论