微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

近距离力量片段变化的软键盘

如何解决近距离力量片段变化的软键盘

最近几天这让我受益匪浅。我有主要活动和两个片段。第一个是屏幕的上半部分,第二个是屏幕的下半部分。第二个片段有 EditText。对焦时,键盘打开。如果用户添加文本, 在关闭底部片段变化。它由与顶部片段相同的内容填充。这是不希望的。如果用户添加文本并且文本框保持为空,则键盘关闭并保留底部片段。所需的行为是无论 EditText 内容如何,​​底部片段都保持不变。谢谢你的帮助!我认为这是所有相关代码

AndroidManifest

android:windowSoftInputMode="adjustPan"

主要活动

public class Main extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

private DrawerLayout drawer;
FragmentChart fragmentChart;
FragmentTable fragmentTable;
FragmentStudy fragmentStudy;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    fragmentChart = new FragmentChart();
    fragmentTable = new FragmentTable();
    fragmentStudy = new FragmentStudy();

    getwindow().setSoftInputMode(WindowManager.LayoutParams.soFT_INPUT_ADJUST_PAN);

    drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawer,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().replace(R.id.top_fragment_layout,fragmentChart).commit();
        getSupportFragmentManager().beginTransaction().replace(R.id.bottom_fragment_layout,fragmentTable).commit();
        navigationView.setCheckedItem(R.id.practice);
    }
}

@Override
public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) {
    if (item.getItemId() == R.id.study) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.top_fragment_layout,fragmentChart)
                .replace(R.id.bottom_fragment_layout,fragmentStudy)
                .commit();
    }
    if (item.getItemId() == R.id.practice) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.top_fragment_layout,fragmentTable)
                .commit();
    }
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public void onBackpressed() {
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackpressed();
    }
}

主要xml

<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".Main"
tools:openDrawer="start">

    <FrameLayout
        android:id="@+id/top_fragment_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"/>

    <FrameLayout
        android:id="@+id/bottom_fragment_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/top_fragment_layout" />

</androidx.drawerlayout.widget.DrawerLayout>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。