需要实现的效果
可单选、单选某一条可编辑
xml布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBg"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context=".ui.activity.other.SignTypeActivity">
<android.support.v4.widget.NestedScrollView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
tools:ignore="UnknownIdInLayout">
<LinearLayout
android:id="@+id/llSignNormal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimen_10"
android:orientation="vertical">
<RadioGroup
android:id="@+id/rgE"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimen_5"
android:divider="#F2F2F2"
android:showDividers="middle">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="@dimen/dimen_20"
android:text="@string/sign_normal"
android:textColor="@color/text_gray2"
android:textSize="@dimen/text_12_size" />
<RadioButton
android:id="@+id/rbNormal"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_50"
android:layout_gravity="center_vertical"
android:layout_marginTop="@dimen/dimen_5"
android:background="@color/white"
android:button="@null"
android:drawableBottom="@drawable/sender_home_bg_blue"
android:drawableRight="@drawable/icon_dispatcher_selector"
android:gravity="left|center_vertical"
android:paddingEnd="@dimen/dimen_20"
android:paddingStart="@dimen/dimen_20"
android:text="@string/sign_normal"
android:textColor="@color/colorBlack"
android:textSize="@dimen/text_16_size" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_1"
android:background="@color/line" />
</RadioGroup>
<LinearLayout
android:id="@+id/llEO"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_50"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:paddingStart="@dimen/dimen_20"
android:text="@string/sign_e7"
android:textColor="@color/colorBlack"
android:textSize="@dimen/text_16_size" />
<EditText
android:id="@+id/etE99"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="@dimen/dimen_10"
android:layout_marginStart="@dimen/dimen_10"
android:layout_weight="2"
android:background="@null"
android:textSize="@dimen/text_14_size"
android:hint="填写原因,少于20个字"
android:maxLength="20"
android:maxLines="1" />
<RadioButton
android:id="@+id/rbE99"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="@color/white"
android:button="@null"
android:drawableBottom="@drawable/sender_home_bg_blue"
android:drawableRight="@drawable/icon_dispatcher_selector"
android:gravity="left|center_vertical"
android:paddingEnd="@dimen/dimen_20"
android:paddingStart="@dimen/dimen_20"
android:textColor="@color/colorBlack"
android:textSize="@dimen/text_16_size" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>
点击编辑框,清除原来的选择
//编辑
etE99.setOnClickListener({ v ->
val et = v as EditText
if (!et.isCursorVisible) {
etGetFocus(et)
}
})
etE99.onFocusChangeListener = OnFocusChangeListener { v, hasFocus ->
if (hasFocus) {
clearRbutton()
etGetFocus(etE99)
} else {
etLostFocus(etE99)
}
}
private fun clearRbutton() {
if (rbNormal.isChecked) {
rbNormal.isChecked = false
}
}
private fun etGetFocus(etE99: EditText?) {
etE99!!.requestFocus()
etE99.post(Runnable {
val manager = etE99.context.getSystemService(
Context.INPUT_METHOD_SERVICE) as InputMethodManager
manager.showSoftInput(etE99, 0)
})
// 光标置于文字最后
etE99.setSelection(etE99.text.toString().length)
}
private fun etLostFocus(etE99: EditText?) {
etE99!!.clearFocus()
val manager = etE99.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
manager.hideSoftInputFromWindow(etE99.windowToken, 0)
}
/**
* 点击屏幕其他地方,使 etName 失去焦点(EditText)
*/
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
if (ev.action == MotionEvent.ACTION_DOWN) {
// 获取当前焦点所在的控件;
val view = currentFocus
if (view != null && view is EditText) {
val r = Rect()
view.getGlobalVisibleRect(r)
val rawX = ev.rawX.toInt()
val rawY = ev.rawY.toInt()
// 判断点击的点是否落在当前焦点所在的 view 上;
if (!r.contains(rawX, rawY)) {
view.clearFocus()
}
}
}
return super.dispatchTouchEvent(ev)
}
效果:
11.png 22.png
网友评论