gpt4 book ai didi

java - 如何在底部的对话框 fragment 中设置文本按钮?

转载 作者:行者123 更新时间:2023-12-03 10:12:02 24 4
gpt4 key购买 nike

我有一个针对bottomsheetdialog片段的类。我看了很多地方,但我很困惑。我想更改 Bottom Sheet 中的按钮文本。我收到此错误'android.view.View android.view.View.findViewById(int) '在空对象引用上。
这是我的代码;

 public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(final Bundle savedInstanceState) {

bottomSheetFragment=new BottomSheetFragment();
View viewDialog=bottomSheetFragment.getView();
assert viewDialog != null;
MaterialButton btn_titresim=viewDialog.findViewById(R.id.btn_titresim);
btn_titresim.setText("text");
}
}

BottomSheetDialogFragment的另一个类
public class BottomSheetFragment extends BottomSheetDialogFragment {
public BottomSheetFragment() {}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Objects.requireNonNull(getDialog()).setOnShowListener(dialog -> {

BottomSheetDialog d = (BottomSheetDialog) dialog;
View bottomSheetInternal =
d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
assert bottomSheetInternal != null;
BottomSheetBehavior.from(bottomSheetInternal).setState(BottomSheetBehavior.STATE_EXPANDED);
});
return inflater.inflate(R.layout.layout_popup, container, false);
}

}

最佳答案

您可以通过在片段中包含一个监听器interface来解决此问题,该监听器会将BottomSheet片段的View返回到您的 Activity ,因此您通常可以通过BottomSheetDialogFragment方法访问底层 View 的findViewById()基础 View 。

在这里,我决定对BottomSheetDialogFragment使用单例模式来设置 Activity 中的监听器实例。

因此,在您的片段中添加一个监听器;它在FragmentListener下命名,在onCreateView()onViewCreated()中调用监听器回调

public class BottomSheetFragment extends BottomSheetDialogFragment {

public BottomSheetFragment() {}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

interface FragmentListener {
void getView(View view);
}

static FragmentListener mFragmentListener;

public static BottomSheetFragment newInstance(FragmentListener listener) {
mFragmentListener = listener;
return new BottomSheetFragment();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Objects.requireNonNull(getDialog()).setOnShowListener(dialog -> {

BottomSheetDialog d = (BottomSheetDialog) dialog;
View bottomSheetInternal =
d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
assert bottomSheetInternal != null;
BottomSheetBehavior.from(bottomSheetInternal).setState(BottomSheetBehavior.STATE_EXPANDED);
});

View view = inflater.inflate(R.layout.layout_popup, container, false);

// Trigger the listener callback to return the view back to the activity
// mFragmentListener.getView(view); // Not working in all devices

return inflater.inflate(R.layout.layout_popup, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
// Trigger the listener callback to return the view back to the activity
mFragmentListener.getView(view);
}

}

通过您的 Activity 实现监听器,并更改回调中的文本,并使用单例模式实例化 BottomSheetDialogFragment
 public class MainActivity extends AppCompatActivity implements BottomSheetFragment.FragmentListener {

@Override
protected void onCreate(final Bundle savedInstanceState) {

bottomSheetFragment = BottomSheetFragment.newInstance(this);

}

@Override
public void getView(View view) {
// Setting the text
((MaterialButton) view.findViewById(R.id.btn_titresim)).setText("text");
}

}

希望能解决您的问题

关于java - 如何在底部的对话框 fragment 中设置文本按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61198221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com