久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

自動調(diào)整 EditText

Autosizing EditText(自動調(diào)整 EditText)
本文介紹了自動調(diào)整 EditText的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

Android 最近添加了對根據(jù)視圖大小以及最小和最大文本大小調(diào)整 TextViews 文本大小的支持.
https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html

Android recently added the support for resizing TextViews text size based on the view size and the min and max text size.
https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html

很遺憾,它們不支持 EditTexts,那么 EditText 有沒有其他替代品?

Unfortunately, they don't support EditTexts, so Is there any other alternatives for EditText?

推薦答案

我和你一樣被卡住了,EditText 是 TextView 的子對象但不支持自動調(diào)整大小 ????

I was stuck as you, how EditText is child of TextView but don't support autosize ????

我通過某種 hack 實現(xiàn)了這一點.首先我看到了 TextView 代碼在 EditTextView 上復(fù)制和實現(xiàn)為擴展(在 Kotlin 中),但是.. 有很多方法,所以最后我放棄了這個選項.

I have achieve this with some kind of hack. First I saw the TextView code to copy and implement as extension (in Kotlin) on EditTextView, but.. there is a bulk of methods, so endly I discard that option.

我所做的是使用不可見的 TextView(是的,我知道這是一個完整的 hack,對此我不太滿意,但 Google 應(yīng)該為此感到羞恥)

What I have do, it's to use a TextView invisible (yes I know is a complete hack, am not very happy with that but Google should be ashamed about this)

這是我的 xmls

    <TextView android:id="@+id/invisibleTextView"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:focusable="false"
    app:autoSizeTextType="uniform"
    app:autoSizeMinTextSize="@dimen/text_min"
    app:autoSizeMaxTextSize="@dimen/text_max"
    app:autoSizeStepGranularity="@dimen/text_step"
    android:textAlignment="center"
    app:layout_constraintLeft_toLeftOf="@id/main"
    app:layout_constraintRight_toRightOf="@id/main"
    app:layout_constraintTop_toBottomOf="@id/textCount"
    app:layout_constraintBottom_toBottomOf="@id/main"
    android:visibility="invisible"
    tool:text="This is a Resizable Textview" />


<EditText android:id="@+id/resizableEditText"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:textAlignment="center"
    app:layout_constraintLeft_toLeftOf="@id/main"
    app:layout_constraintRight_toRightOf="@id/main"
    app:layout_constraintTop_toBottomOf="@id/textCount"
    app:layout_constraintBottom_toBottomOf="@id/main"
    android:maxLength="@integer/max_text_length"
    tool:text="This is a Resizable EditTextView" />

注意:重要的是兩個視圖具有相同的寬度/高度

Note: It's important both view have the same width/height

然后在我的代碼中,我使用來自這個 textview 的自動計算來在我的 EditTextView 上使用.

Then on my code I use the autocalculations from this textview to use on my EditTextView.

private fun setupAutoresize() {
    invisibleTextView.setText("a", TextView.BufferType.EDITABLE) //calculate the right size for one character
    resizableEditText.textSize = autosizeText(invisibleTextView.textSize)
    resizableEditText.setHint(R.string.text_hint)

    resizableEditText.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(editable: Editable?) {
            resizableEditText.textSize = autosizeText(invisibleTextView.textSize)
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            textCount.text = currentCharacters.toString()
            val text = if (s?.isEmpty() ?: true) getString(R.string.text_hint) else s.toString()
            invisibleTextView.setText(text, TextView.BufferType.EDITABLE)
        }
    })
}

private fun autosizeText(size: Float): Float {
    return size / (resources.displayMetrics.density + MARGIN_FACTOR /*0.2f*/)
}

請注意,要更改提示的大小,我使用此 Android EditText 提示大小.

As note, for change the size of hint i use this Android EditText Hint Size.

我知道這是一個艱難的解決方法,但至少我們確信即使在未來版本上進行可調(diào)整大小的更改時,它仍能繼續(xù)工作,而專有或廢棄的 github 庫將失敗.

I know it's a hard workaround, but at least we are sure this will continue working even when resizable change on future versions, while a propetary or abandoned github lib will fail.

我希望有一天,谷歌會聽到我們的聲音并在孩子身上實現(xiàn)這個奇妙的功能,我們可以避免所有這些事情

I hope some day, google hear us and implement on childs this wonderfull feature, and we could avoid all this stuff

希望對你有幫助

這篇關(guān)于自動調(diào)整 EditText的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Cut, copy, paste in android(在android中剪切、復(fù)制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數(shù)的數(shù)字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 成人免费看片 | 亚洲精品免费视频 | 天天曰天天曰 | 国产精品99久久久久久久vr | 精品天堂| 一区二区三区在线播放视频 | 成人午夜视频在线观看 | 欧美激情精品久久久久久变态 | 成人免费视频7777777 | 久久久.com| 久久一起草 | 人人人干 | 99精品视频网 | 草草视频在线免费观看 | av在线免费观看网址 | 欧美日韩国产高清视频 | 久久精品1| 黄a在线播放 | 国产一区二区三区免费观看在线 | 国产精品久久777777 | 成人自拍视频网站 | caoporn免费在线视频 | 欧美黄在线观看 | 日本成人在线观看网站 | 性高朝久久久久久久3小时 av一区二区三区四区 | 二区欧美 | 人人澡人人射 | 日日夜精品视频 | 免费在线一区二区 | 99热这里有精品 | 在线免费看91 | 日韩毛片 | 99热在这里只有精品 | 有码一区| 欧美日韩综合视频 | 久久久www成人免费精品 | 亚洲乱码一区二区三区在线观看 | 欧美一级艳情片免费观看 | 国产精品二区三区在线观看 | 91久操视频 | 51ⅴ精品国产91久久久久久 |