如何在导航项单击时隐藏和取消隐藏视图



我有一个导航抽屉,里面有3个项目。例如,当我点击项目1时,我需要它来显示编辑文本,当我单击项目2或3时,我希望它隐藏显示的内容并显示其他内容,即属于项目2和项目3的内容。

起初,我在每个项目上都尝试了Toast消息,效果很好,但当我制作了一个编辑文本,并在您单击项目1时使其可见时,它会向我显示该编辑文本,但当我们返回并单击项目2或项目3以隐藏项目1的编辑文本时,什么也没发生。。抽屉好像没有反应了,我不知道。

这是我的xml代码:

<com.google.android.material.navigation.NavigationView
android:id="@+id/drawer_nav"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/black"
app:menu="@menu/navigation"
app:itemTextColor="@color/white"
app:itemIconTint="@color/white"
android:layout_gravity="start"
/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/txtSummary"
android:layout_width="200dp"
android:visibility="gone"
android:layout_height="200dp"
android:gravity="top">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/inputET"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:gravity="right"
android:scrollIndicators="left"
android:textColor="@color/black"
android:background="@color/white"
android:inputType="textMultiLine"/>
</com.google.android.material.textfield.TextInputLayout>

这是我的Java代码:

txtSummary = findViewById(R.id.txtSummary);
textInputEditText = findViewById(R.id.inputET);
navigationView = findViewById(R.id.drawer_nav);
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

switch (item.getItemId()){
case R.id.item1:
Toast.makeText(getApplication()," Item 1 icon has been clicked  ",Toast.LENGTH_LONG).show();
txtSummary.setVisibility(View.VISIBLE);
textInputEditText.setVisibility(View.VISIBLE);
return true;

case R.id.item2:
txtSummary.setVisibility(View.GONE);
textInputEditText.setVisibility(View.GONE);
Toast.makeText(getApplication()," Item 2 icon has been clicked  ",Toast.LENGTH_LONG).show();
return true;
case R.id.item3:
txtSummary.setVisibility(View.GONE);
textInputEditText.setVisibility(View.GONE);
Toast.makeText(getApplication()," Item 3 icon has been clicked  ",Toast.LENGTH_LONG).show();

return true;
}

return true;
}
});```

尝试更改布局代码。将您的编辑文本放在任何其他布局中。参考以下代码

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/txtSummary"
android:layout_width="200dp"
android:layout_height="200dp"
android:gravity="top"
android:visibility="gone">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/inputET"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:background="@color/white"
android:gravity="right"
android:inputType="textMultiLine"
android:scrollIndicators="left"
android:textColor="@color/black" />
</com.google.android.material.textfield.TextInputLayout>
</FrameLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/drawer_nav"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/navigation" />
</androidx.drawerlayout.widget.DrawerLayout>

最新更新