te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>kotlin - Prevent android soft keyboard from covering EditText inputs - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

kotlin - Prevent android soft keyboard from covering EditText inputs - Stack Overflow

programmeradmin5浏览0评论

I'm trying to figure out how to use android:windowSoftInputMode="adjustResize" to prevent the soft keyboard from covering a list of EditText items. Here is a simple app with a single layout and activity class that reproduces my issue:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }
}

Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=";
    xmlns:app=";
    xmlns:tools=";
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/mytitle"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="Static Header"
        android:gravity="center_horizontal"
        android:textSize="24sp">
    </TextView>
    <ScrollView
        android:id="@+id/nested_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content">
            <EditText
                android:layout_width="match_parent"
                android:text="item"
                android:inputType="textLongMessage"
                android:layout_marginBottom="10dp"
                android:layout_height="wrap_content">
            </EditText>
            ...
             repeat 39 more edit texts
            ...

        </LinearLayout>
    </ScrollView>
</LinearLayout>

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=";
    xmlns:tools=";>

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Shop2"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:windowSoftInputMode="adjustResize"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

See image here

In the linked image, I've clicked an EditText near the bottom of the screen, and even though it appeared to receive focus correctly, the soft keyboard is still covering the input and it's hard to see where you're typing. I'd like the layout to resize itself above the keyboard.

I have a static header which I want to prevent being bumped off the screen by panning when the keyboard appears, that's why I've chosen adjustResize.

I'm open to using a ListView or other list container view, but that's slightly tangential to my issue which is I would like to understand why this simpler setup doesn't behave as I expect.

I'm trying to figure out how to use android:windowSoftInputMode="adjustResize" to prevent the soft keyboard from covering a list of EditText items. Here is a simple app with a single layout and activity class that reproduces my issue:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }
}

Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android/apk/res/android"
    xmlns:app="http://schemas.android/apk/res-auto"
    xmlns:tools="http://schemas.android/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/mytitle"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="Static Header"
        android:gravity="center_horizontal"
        android:textSize="24sp">
    </TextView>
    <ScrollView
        android:id="@+id/nested_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content">
            <EditText
                android:layout_width="match_parent"
                android:text="item"
                android:inputType="textLongMessage"
                android:layout_marginBottom="10dp"
                android:layout_height="wrap_content">
            </EditText>
            ...
             repeat 39 more edit texts
            ...

        </LinearLayout>
    </ScrollView>
</LinearLayout>

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android/apk/res/android"
    xmlns:tools="http://schemas.android/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Shop2"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:windowSoftInputMode="adjustResize"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

See image here

In the linked image, I've clicked an EditText near the bottom of the screen, and even though it appeared to receive focus correctly, the soft keyboard is still covering the input and it's hard to see where you're typing. I'd like the layout to resize itself above the keyboard.

I have a static header which I want to prevent being bumped off the screen by panning when the keyboard appears, that's why I've chosen adjustResize.

I'm open to using a ListView or other list container view, but that's slightly tangential to my issue which is I would like to understand why this simpler setup doesn't behave as I expect.

Share Improve this question asked Feb 17 at 16:29 lrouleaulrouleau 1
Add a comment  | 

1 Answer 1

Reset to default 0

I was able to get this working by padding out the keyboard inset. Now the layout resizes above the keyboard:

      ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
        val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
        val ime = insets.getInsets(WindowInsetsCompat.Type.ime())
        v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom + ime.bottom)
        insets
    }

This solution looks reasonable, but I'd appreciate any alternative solutions or additional advice as I'm new to android development. My initial impression from reading various docs was that I wouldn't need to do this manual padding adjustment with android:windowSoftInputMode="adjustResize" set.

发布评论

评论列表(0)

  1. 暂无评论