在TextView中创建空心文字
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:shadowColor="@color/colorAccent"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="15"
android:text="Hello, I love you"
android:textColor="#fff"
android:textSize="52sp" />
android:shadowRadius用于设置阴影的模糊程度,该值越大,阴影越模糊。
在TextView中实现上文下图的布局
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableBottom="@mipmap/bg"
android:drawablePadding="16dp"
android:text="@string/long_text"
android:textSize="20sp" />
除了上述的drawableBottom, TextView还提供了drawableEnd, drawableLeft, drawableRight等属性设置图片在文字的位置,另外drawablePadding用于设置文本与图像之间的间距。
在TextView中为文本添加超链接
- resource添加链接文本
<string name="link_text">Link is here. <a href="https://www.baidu.com/">百度一下</a></string>
- 添加Textview
<TextView
android:id="@+id/tv_link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/link_text"
android:textSize="20sp" />
- 在onCreate方法里面设置movementMethod
tv_link.movementMethod = LinkMovementMethod.getInstance()
禁止在EditText中插入非法文字
- 定义EditView
<EditText
android:id="@+id/et_money"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="edit"
android:hint="@string/edit_hint"
android:inputType="numberDecimal"
android:textSize="20sp" />
- 添加InputFilter类,定义过滤规则
package com.huhx.wusq.activity
import android.text.InputFilter
import android.text.Spanned
class InputFilterMinMax(min: Float, max: Float) : InputFilter {
private var min: Float = 0.0F
private var max: Float = 0.0F
init {
this.min = min
this.max = max
}
override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int): CharSequence? {
try {
val input = (dest.subSequence(0, dstart).toString() + source + dest.subSequence(dend, dest.length)).toFloat()
if (isInRange(min, max, input))
return null
} catch (nfe: NumberFormatException) {
}
return ""
}
private fun isInRange(a: Float, b: Float, c: Float): Boolean {
return if (b > a) c in a..b else c in b..a
}
}
- 在onCreate方法中设置EditView的filters属性
et_money.filters = arrayOf<InputFilter>(InputFilterMinMax(0.0F, 20.0F))
使用AutoCompleteTextView实现自动提示
- 添加AutoCompleteTextView
<AutoCompleteTextView
android:id="@+id/actv_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:hint="@string/auto_complete_hint"
android:inputType="text"
android:textSize="20sp" />
completionThreshold
为1,表示输入第一个字符时触发自动提示。
- 在onCreate中设置AutoCompleteTextView的adapter
class MainActivity : AppCompatActivity() {
private val myArray = arrayOf("apple", "watermelon", "orange", "pear", "cat", "dog", "fish", "people")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
val adapter = ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, myArray)
actv_search.setAdapter(adapter)
}
}
在EditText右端设置输入提示内容和图标
- 添加EditView
<EditText
android:id="@+id/et_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="edit"
android:hint="@string/edit_hint"
android:inputType="text"
android:textSize="20sp" />
- 在onCreate中设置EditView的属性
val drawable = resources.getDrawable(R.mipmap.ic_launcher, theme)
drawable.setBounds(0, 0, 72, 72)
et_message.setError("必须填写", drawable)