android - 关闭时的软键盘强制片段更改
问题描述
在过去的几天里,这对我来说是最好的。我有主要活动和两个片段。第一个是屏幕的上半部分,第二个是屏幕的下半部分。第二个片段有 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>
解决方案
推荐阅读
- c# - Microsoft Graph API - 如何在没有授权码的情况下获取访问令牌?
- c# - 尝试从选择标记传递时,绑定传递 null
- c++ - 通过检查是否存在循环来确定二叉树有效性的函数
- c++ - 从内部函数内部使用外部函数的变量
- centos6.5 - Nebula Graph 在 CentOS 6.5 上失败
- .net - 如果机器人在 botframework v4 中空闲 5 分钟,如何向用户发送消息
- java - Java android中的视频压缩库?
- r - 将数据从 R 加载到 OpenBUGS 时出现问题?
- ios - 需要 iCloud 时如何处理首次启动体验?
- python-3.x - 在 tkinter 中弹出带有 pygame 的弹出窗口,除非关闭/按下确定,否则它不会消失