如何在底部的对话框片段中设置文本按钮? [英] how to settext button in bottom sheet dialog fragment?

查看:147
本文介绍了如何在底部的对话框片段中设置文本按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

BottomSheetDialogFragment的另一个类

Another class for 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返回到您的活动,因此您可以通常使用findViewById()方法访问BottomSheetDialogFragment底层视图.

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 BottomSheetDialogFragmentunderlying views normally by findViewById() method.

在这里,我决定对BottomSheetDialogFragment使用 Singleton 模式从活动中设置侦听器实例.

Here I decided to use the Singleton pattern for the BottomSheetDialogFragment to set a listener instance from the activity.

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

So in your fragment add a listener; it's named below FragmentListener, call the listener callback in onCreateView() or in 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);
    }

}

通过您的活动实现侦听器,并更改回调中的文本,并改为使用单例模式实例化BottomSheetDialogFragment.

implement the listener by your activity, and change the text in your callback, and instantiate the BottomSheetDialogFragment using the singleton pattern instead.

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

 }

希望能解决您的问题

这篇关于如何在底部的对话框片段中设置文本按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆