Android EditText
在本教程中,我们将在Android应用程序中实现EditText。
我们将使用XML以及以编程方式创建EditText。
Android EditText
Android EditText是TextView的子类,允许用户输入和编辑。
通常在登录屏幕和表单中使用。
EditText XML定义
通过以下方式在XML中定义EditText。
<EditText
android:id="@+id/inNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:hint="@string/enter_a_number_here"
android:imeOptions="actionNext"
android:inputType="number"
EditText属性
android:id用于在EditText上设置唯一标识符
layout_margin用于在EditText视图外设置页边距。
"提示"用于设置EditText的提示。
" imeOptions"用于设置最右下方的键盘按钮的行为。
可以将其设置为完成,搜索,下一个等。
将其设置为下一个会将光标移至屏幕中存在的下一个EditText。inputType用于指定允许的输入格式。
默认情况下是文本。
可以根据我们的需要设置电子邮件,数字,numberDecimal等。
以编程方式创建EditText
我们必须创建EditText对象,然后将其添加到屏幕布局中。
val editText = EditText(this) linearLayout.addView(editText)
EditText TextWatcher
TextWatcher是一个界面,用于在键入输入之前,之后和期间监听EditText中的更改。
它包含三个需要重写的功能。
afterTextChanged:键入内容后立即触发
beforeTextChanged:在下一个输入之前被触发。
onTextChanged:它在输入期间被触发。
为了在EditText上设置TextWatcher,我们在一个addTextChangedListener函数中调用该接口。
editText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
toast(message = "Number is $s")
}
})
Kotlin对象与Java对象不同。
onTextChanged()函数在参数方面与afterTextChanged()不同。
我们的示例应用程序将侦听EditText输入中的更改,并在以编程方式定义的TextView中对其进行更新。
Android EditText示例项目结构
1.布局代码
下面给出了activity_main.xml布局的代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/inNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:hint="@string/enter_a_number_here"
android:imeOptions="actionNext"
android:inputType="number"
</LinearLayout>
2.活动代码
MainActivity.kt类的代码如下:
package net.androidly.androidlyedittext
import android.content.Context
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.text.Editable
import android.text.TextWatcher
import android.view.inputmethod.EditorInfo
import android.widget.*
import kotlinx.android.synthetic.main.activity_main.*
import android.widget.LinearLayout
class MainActivity : AppCompatActivity() {
val txtAfter by lazy { TextView(this) }
val txtOn by lazy { TextView(this) }
val txtBefore by lazy { TextView(this) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val inNumber = findViewById<edittext>(R.id.inNumber)
inNumber.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
toast(message = "Number is $s")
}
})
val editText = EditText(this)
editText.apply {
setText("Androidly EditText")
hint = "Keep entering"
val lp = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
lp.setMargins(8, 8, 8, 8)
layoutParams = lp
imeOptions = EditorInfo.IME_ACTION_NEXT
setTextColor(ContextCompat.getColor(this@MainActivity, android.R.color.holo_purple))
smartTextWatcher(
on = { txtOn.apply { text = "onTextChanged: $it" } },
after = { txtAfter.apply { text = "smartTextWatcher: $it" } },
before = { txtBefore.apply { text = "beforeTextChanged: $it" } }
)
}
linearLayout.addView(editText)
val editText2 = EditText(this)
editText2.apply {
hint = "Enter something"
val lp = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
lp.setMargins(8, 8, 8, 8)
layoutParams = lp
setBackgroundColor(ContextCompat.getColor(this@MainActivity, android.R.color.holo_green_dark))
imeOptions = EditorInfo.IME_ACTION_DONE
smartTextWatcher(
on = { txtOn.apply { text = "onTextChanged: $it" } },
after = { txtAfter.apply { text = "smartTextWatcher: $it" } },
before = { txtBefore.apply { text = "beforeTextChanged: $it" } }
)
}
linearLayout.addView(editText2)
txtAfter.text = "AfterTextChanged :"
txtAfter.setPadding(8, 8, 8, 8)
linearLayout.addView(txtAfter)
txtOn.text = "OnTextChanged :"
txtOn.setPadding(8, 8, 8, 8)
linearLayout.addView(txtOn)
txtBefore.text = "BeforeTextChanged :"
txtBefore.setPadding(8, 8, 8, 8)
linearLayout.addView(txtBefore)
}
fun Context.toast(context: Context = applicationContext, message: String, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(context, message, duration).show()
}
fun EditText.smartTextWatcher(on: (String) -> Unit, after: (String) -> Unit, before: (String) -> Unit) {
this.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
after.invoke(s.toString())
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
before.invoke(s.toString())
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
on.invoke(s.toString())
}
})
}
}
</edittext>
在上面的代码中,我们使用" by lazy"以编程方式和惰性方式创建了TextViews。
这意味着TextView实例仅在类中被调用时才会创建。
findViewById <EditText>用于从XML获取EditText。
当EditText中的文本更改时,我们将显示祝酒词。
Toast功能是Kotlin中的扩展功能。
" apply" lambda表达式用于设置EditText上的属性,而无需每次都调用实例。
要以编程方式设置布局参数,我们需要执行以下操作:
val lp = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT) lp.setMargins(8, 8, 8, 8) layoutParams = lp
funEditText.smartTextWatcher是一个扩展函数,用于通过使用lambda表达式来缩短TextWatcher接口的详细代码。
对于每个参数,我们都会更新lambda表达式内的相应文本视图。

