首页 > 解决方案 > 关闭时的软键盘强制片段更改

问题描述

在过去的几天里,这对我来说是最好的。我有主要活动和两个片段。第一个是屏幕的上半部分,第二个是屏幕的下半部分。第二个片段有 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, fragmentChart)
                .replace(R.id.bottom_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>

标签: androidandroid-fragmentsandroid-softkeyboard

解决方案


推荐阅读