简体   繁体   English

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

[英]how to settext button in bottom sheet dialog fragment?

i have one class for bottomsheetdialog fragment.I looked at many places but I'm confused.i want to change text of button in bottom sheet.i get this error 'android.view.View android.view.View.findViewById(int)' on a null object reference.我有一个 class 用于 bottomsheetdialog 片段。我看了很多地方,但我很困惑。我想更改底部工作表中按钮的文本。我收到此错误 'android.view.View android.view.View.findViewById(int) ' 在 null object 参考。 here are my codes;这是我的代码;

 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");
     }
 }

Another class for BottomSheetDialogFragment BottomSheetDialogFragment 的另一个 class

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);
}

} }

You can solve this by having a listener interface in your fragment that returns the BottomSheet fragment's View back to your activity, so you can then access the BottomSheetDialogFragment underlying views normally by findViewById() method.您可以通过在您的片段中使用一个侦听interface来解决此问题,该接口将BottomSheet片段的View返回给您的活动,这样您就可以通过findViewById()方法正常访问BottomSheetDialogFragment底层视图。

Here I decided to use the Singleton pattern for the BottomSheetDialogFragment to set a listener instance from the activity.在这里,我决定使用BottomSheetDialogFragmentSingleton模式来设置活动的侦听器实例。

So in your fragment add a listener;所以在你的片段中添加一个监听器; it's named below FragmentListener , call the listener callback in onCreateView() or in onViewCreated()它被命名为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);
    }

}

implement the listener by your activity, and change the text in your callback, and instantiate the BottomSheetDialogFragment using the singleton pattern instead.通过您的活动实现侦听器,更改回调中的文本,并改为使用 singleton 模式实例化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");
    }

 }

Wish that solves your problem希望能解决你的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM