我有一个包含两个元素的 Bottomsheet xml,Follow 和 Copy link.. 当我点击 Follow 时,它应该被改变动态地取消关注。我尝试使用 setText,但没有用。
请指导我如何更改底部对话框的文本。
这是我的 Bottomsheet xml 文件
<LinearLayout app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:layout_width="match_parent"
android:layout_height="180dp"
android:orientation="vertical"
android:background="#ffffff"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="@+id/bottom_sheet_follow"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:foreground="?android:attr/selectableItemBackground"
android:padding="16dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="@drawable/ic_mode_follow_grey_24dp"
/>
<TextView
android:id="@+id/followTView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="Follow"/>
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_sheet_copy"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:foreground="?android:attr/selectableItemBackground"
android:padding="16dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="@drawable/ic_copy_grey_24dp"
/>
<TextView
android:id="@+id/copyLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="Copy link"/>
</LinearLayout>
Bottomsheet Java 代码
View bottomSheetView = View.inflate(mContext, R.layout.bottom_sheet_dialog_for_sharedposts, null);
mDialog = new BottomSheetDialog(mContext);
mDialog.setContentView(bottomSheetView);
mDialog.show();
followBtnLayout = bottomSheetView.findViewById(R.id.bottom_sheet_follow);
followTView = bottomSheetView.findViewById(R.id.followTView);
followBtnLayout .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
followTView.setText("Unfollow");
}
});
但是,Follow TextView (我的意思是 followTView)不会更改为Unfollow。 请告诉我哪里出错了...
提前致谢...
最佳答案
你可以用 bool 值来做,我已经用我的 imageview 做了。 可能对你有帮助
//Initialize boolean as true by default
public boolean showingFirst = true;
holder.ivFavorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (showingFirst){
Toast.makeText(mActivity, "Item added to favorite", Toast.LENGTH_SHORT).show();
holder.ivFavorite.setImageResource(R.drawable.filled_heart);
showingFirst = false;
}else {
holder.ivFavorite.setImageResource(R.drawable.ic_favorite);
Toast.makeText(mActivity, "Item removed from favorite", Toast.LENGTH_SHORT).show();
showingFirst = true;
}
}
});
关于android - 如何动态更改Modal Bottomsheet android中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50364133/