如何在底部 sheet 对话框片段中设置文本按钮?

how to settext button in bottom sheet dialog fragment?

我有一个 class 用于 bottomsheetdialog fragment.I 看了很多地方,但我 confused.i 想更改底部按钮的文本 sheet.i 得到这个错误 '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");
     }
 }

另一个 class 用于 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 来解决这个问题,returns BottomSheet 片段的 View 回到你的 activity,所以你然后可以通过findViewById()方法正常访问BottomSheetDialogFragment底层视图。

这里我决定使用 Singleton 模式为 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");
    }

 }

希望能解决你的问题