我正在使用https://github.com/hbb20/CountryCodePickerProject库作为国家代码选择器。这就是 UI 设计师的做法。但不幸的是,任何人都可以有 XML 解决方案吗?您必须使用 hbb20 国家代码选择器和材料设计文本布局 + 材料设计编辑文本。
问问题
1366 次
1 回答
1
在您的项目中创建此类。
class CustomTextInputLayout @JvmOverloads
/**
* Constructor
* @param context Context
* @param attrs Attribute Set for view
* @param defStyleAttr Int style from attr
*/
constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : TextInputLayout(context, attrs, defStyleAttr) {
private var bounds: Rect? = null
private var recalculateMethod: Method? = null
private var collapsingTextHelper: Any? = null
/**
* Companion Object
*/
companion object {
const val COLLAPSING_HELPER = "collapsingTextHelper"
const val COLLAPSED_BOUNDS = "collapsedBounds"
const val RECALCULATE = "recalculate"
}
/**
* Init constructors
*/
init {
init()
}
/**
* On layout changes
* @param changed Boolean
* @param left Int
* @param top Int
* @param right Int
* @param bottom Int
*/
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
super.onLayout(changed, left, top, right, bottom)
adjustBounds()
}
/**
* Init function call the TextInputLayout class and the secondary internal class CollapsingTextHelper
* @see TextInputLayout
*/
private fun init() {
try {
//Search internal and private class over object called as variable
val cthField = TextInputLayout::class.java.getDeclaredField(COLLAPSING_HELPER)
cthField.isAccessible = true
collapsingTextHelper = cthField.get(this)
//Search in private class the other component to create a view
val boundsField = collapsingTextHelper?.javaClass?.getDeclaredField(COLLAPSED_BOUNDS)
boundsField?.isAccessible = true
bounds = boundsField?.get(collapsingTextHelper) as Rect
recalculateMethod = collapsingTextHelper?.javaClass?.getDeclaredMethod(RECALCULATE)
} catch (e: NoSuchFieldException) {
collapsingTextHelper = null
bounds = null
recalculateMethod = null
e.printStackTrace()
} catch (e: IllegalAccessException) {
collapsingTextHelper = null
bounds = null
recalculateMethod = null
e.printStackTrace()
} catch (e: NoSuchMethodException) {
collapsingTextHelper = null
bounds = null
recalculateMethod = null
e.printStackTrace()
}
}
/**
* Adjust Bounds of the view for padding
* and changes for the view
*/
private fun adjustBounds() {
if (collapsingTextHelper == null)
return
try {
bounds?.left = editText?.left!! + editText?.paddingLeft!!
recalculateMethod?.invoke(collapsingTextHelper)
} catch (e: InvocationTargetException) {
e.printStackTrace()
} catch (e: IllegalAccessException) {
e.printStackTrace()
} catch (e: IllegalArgumentException) {
e.printStackTrace()
}
}
}
现在,您可以在设计文件中将此类用作 XML 组件。然后使用约束或您正在使用的任何父布局将您的国家/地区代码选择器 XML 组件放在此编辑文本之上。
<com.xxx.xxx.utils.CustomTextInputLayout
android:id="@+id/userNameLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:errorEnabled="true"
android:layout_marginStart="@dimen/_20sdp"
android:layout_marginEnd="@dimen/_20sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/inputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/emailOrMobile"
android:inputType="textEmailAddress"
android:theme="@style/customUnderLinePrimary" />
</com.xxx.xxx.utils.CustomTextInputLayout>
将此代码添加到您的活动或片段中。请确保您使用的可绘制对象在背景中是透明的或相同的颜色。
inputText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.transparent, 0, 0, 0)
inputText.compoundDrawablePadding = 80
于 2021-02-14T09:39:31.990 回答