如何在纯文本中键入内容时填充 运行 文本视图
How to fill on the run a textView while typing something in a plainText
所以我有一个空的纯文本,旁边还有一个空的文本视图。
EditText editText = (EditText) findViewById(R.id.TextVw);
TextView textView = (TextView) findViewById(R.id.textView);
String example = editText.getText().toString();
因此,当我开始在 Edittext(edittext)
中输入内容时,它应该同时(实时)出现在 TextView(textview)
中(示例)是您从 editText
。
我该怎么做?
一个简短的答案是在 editText 的文本更改时更新 textView。
像这样为 editText 实现文本更改侦听器:
editText.addTextChangedListener(object: TextWatcher{
override fun afterTextChanged(p0: Editable?) {
//This is where you set the text in the text view
//Also check if string is not empty or null, if its null, then set "" as textView's text
p0?.let{
if(it.toString().isNotEmpty()){
textView.setText(it.toString())
}else{
textView.setText("")
}
} ?: run {
textView.setText("")
}
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
//This will return the previous value of editText before user typed a character
Log.d("Example", "beforeTextChanged: ${p0.toString()}")
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
//This gives the live value of what the user just typed.
Log.d("Example", "onTextChanged: ${p0.toString()}")
}
})
抱歉,我正在使用 kotlin,所以这个答案也适用于 kotlin。但是 java.
的实现是相同的
如有任何问题请告诉我!
所以我有一个空的纯文本,旁边还有一个空的文本视图。
EditText editText = (EditText) findViewById(R.id.TextVw);
TextView textView = (TextView) findViewById(R.id.textView);
String example = editText.getText().toString();
因此,当我开始在 Edittext(edittext)
中输入内容时,它应该同时(实时)出现在 TextView(textview)
中(示例)是您从 editText
。
我该怎么做?
一个简短的答案是在 editText 的文本更改时更新 textView。 像这样为 editText 实现文本更改侦听器:
editText.addTextChangedListener(object: TextWatcher{
override fun afterTextChanged(p0: Editable?) {
//This is where you set the text in the text view
//Also check if string is not empty or null, if its null, then set "" as textView's text
p0?.let{
if(it.toString().isNotEmpty()){
textView.setText(it.toString())
}else{
textView.setText("")
}
} ?: run {
textView.setText("")
}
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
//This will return the previous value of editText before user typed a character
Log.d("Example", "beforeTextChanged: ${p0.toString()}")
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
//This gives the live value of what the user just typed.
Log.d("Example", "onTextChanged: ${p0.toString()}")
}
})
抱歉,我正在使用 kotlin,所以这个答案也适用于 kotlin。但是 java.
的实现是相同的如有任何问题请告诉我!