问题描述
最近几天这让我受益匪浅。我有主要活动和两个片段。第一个是屏幕的上半部分,第二个是屏幕的下半部分。第二个片段有 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 (将#修改为@)