Skip to content

Instantly share code, notes, and snippets.

@CodeK1988
Last active December 2, 2019 08:25
Show Gist options
  • Select an option

  • Save CodeK1988/52dd3500e34f7610e056f9e858b8dfbc to your computer and use it in GitHub Desktop.

Select an option

Save CodeK1988/52dd3500e34f7610e056f9e858b8dfbc to your computer and use it in GitHub Desktop.
material 相关控件使用记录
1.MaterialButton
常规使用
android:insetTop="0dp"
android:insetBottom="0dp"
android:textAppearance="?android:attr/textAppearance"
selector enable false
app:backgroundTint="@color/login_selector_button"
login_selector_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary" android:state_enabled="true" />
<item android:alpha="0.5" android:color="?attr/colorPrimary" />
</selector>
2.TextInputLayout+AppCompatEditText
常规输入手机号获取验证码
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputPhone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
app:layout_constraintBottom_toBottomOf="@+id/spaceTopEdPhone"
app:layout_constraintLeft_toRightOf="@+id/guideLeft"
app:layout_constraintRight_toLeftOf="@+id/guideRight"
>
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/edPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/login_selector_edit"
android:hint="请输入手机号码"
android:inputType="phone"
android:maxLength="11"
android:paddingTop="0dp"
android:paddingBottom="20dp"
android:textColorHint="#999999"
android:textCursorDrawable="@drawable/login_cursordrawable"
tools:text="111111111111" />
</com.google.android.material.textfield.TextInputLayout>
login_selector_edit.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<layer-list>
<item android:bottom="0dp" android:left="-2dp" android:right="-2dp" android:top="-2dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke android:width="1dp" android:color="#E7E7E7" />
</shape>
</item>
</layer-list>
</item>
<item>
<layer-list>
<item android:bottom="0dp" android:left="-2dp" android:right="-2dp" android:top="-2dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke android:width="1dp" android:color="#E7E7E7" />
</shape>
</item>
</layer-list>
</item>
</selector>
login_cursordrawable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:width="1dp" />
<solid android:color="#E7E7E7" />
</shape>
edPhoneNumber.setOnFocusChangeListener { v, hasFocus ->
if(hasFocus){
textInputPhone.hint = "请输入手机号码"
v.setPadding(0, dip(15),0,dip(15))
}else{
if(edPhoneNumber.text.isNullOrEmpty()){
v.setPadding(0, 0,0,dip(20))
}else{
v.setPadding(0, dip(15),0,dip(15))
if(edPhoneNumber.text.toString().length!=11){
textInputPhone.hint = "手机号码输入不完整哦"
}else{
textInputPhone.hint = "请输入手机号码"
}
}
}
}
这种体验感就会比正常的会好一点 哈哈
else 扩大textview的点击区域
inline fun View.expandViewHitArea(child : View) {
post {
val parentRect = Rect()
val childRect = Rect()
getHitRect(parentRect)
child.getHitRect(childRect)
childRect.left = 0
childRect.top = 0
childRect.right = parentRect.width()
childRect.bottom = parentRect.height()
touchDelegate = TouchDelegate(childRect, child)
}
}
inline fun View.expandViewHitArea(extraSpace: Int) {
(parent as View).post {
val touchableArea = Rect()
getHitRect(touchableArea)
touchableArea.top -= extraSpace
touchableArea.bottom += extraSpace
touchableArea.left -= extraSpace
touchableArea.right += extraSpace
(parent as View).touchDelegate = TouchDelegate(touchableArea, this)
}
}
thanks
https://medium.com/@zpcat/expand-views-touch-area-in-android-e8089bb5b828
https://android.jlelse.eu/android-change-touch-area-of-view-by-touchdelegate-fc19f2a34021
3.EditText
触摸屏幕 收起键盘和焦点 以及其他点击事件 editText.clearFocus()
clRoot.setOnTouchListener { _, _ ->
clRoot.isFocusable = true
clRoot.isFocusableInTouchMode = true
clRoot.requestFocus()
val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(clRoot.windowToken, 0)
false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment