简体   繁体   English

使用片段上的按钮打开AlertDialog

[英]Open AlertDialog with button on Fragment

I try to open an AlertDialog with a Image button on 1 of 3 Fragments I have in my MainActivity, but im Always having 1 of 2 Problems. 我尝试在MainActivity中的3个片段中的1个片段上使用“图像”按钮打开AlertDialog,但是我始终遇到2个问题中的1个。

  1. The Method openDialog() cannot be referenced from a nonstatic context 无法从非静态上下文引用方法openDialog()
  2. .Non-static method 'getSupportFragmentManager()' cannot be referenced from a static context 。非静态方法'getSupportFragmentManager()'不能从静态上下文中引用

I tried several different approaches but all of them had a Problem like this. 我尝试了几种不同的方法,但所有方法都有这样的问题。

My Goal is to have the ImageButton on my timetableFragment.java open a Dialog. 我的目标是让我的timetableFragment.java上的ImageButton打开一个对话框。 Currently I have created an extra class for the AlertDialog. 目前,我已经为AlertDialog创建了一个额外的类。

Im a beginner please try to keep it simple. 我是一个初学者,请尝试保持简单。

My main acticity: 我的主要目的是:

public class MainActivity extends AppCompatActivity { 公共类MainActivity扩展了AppCompatActivity {

private FrameLayout bar_todo;
private FrameLayout bar_timetable;
private FrameLayout bar_notes;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    todoFragment todoFragment = new todoFragment();
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, todoFragment).commit();


    bar_todo = findViewById(R.id.image_todo);
    bar_timetable = findViewById(R.id.image_timetable);
    bar_notes = findViewById(R.id.image_notes);


    bar_todo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            todoFragment todoFragment = new todoFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, todoFragment).commit();
        }
    });


    bar_timetable.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            timetableFragment timetableFragment = new timetableFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, timetableFragment).commit();
        }
    });

    bar_notes.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            notesFragment notesFragment = new notesFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, notesFragment).commit();
        }
    });


}


public static void openDialog() {
    SchulfachDialog schulfachDialog = new SchulfachDialog();
    schulfachDialog.show(getSupportFragmentManager(), "ecameo");
}

} }

AlertDialog Class: AlertDialog类:

public class SchulfachDialog extends AppCompatDialogFragment {
private EditText editTextName;


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.popup_schulfach, null);

    builder.setView(view)
            .setTitle("Add new subject")
            .setMessage("Message")
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });

    return builder.create();
}

} }

timetableFragment (Fragment the button is in) timetableFragment (按钮所在的片段)

public class timetableFragment extends Fragment {
private static final String TAG = "todoFragment";


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    setRetainInstance(true);
    View rootview = inflater.inflate(R.layout.timetable_fragment, container, false);

    ImageButton btn_fach = getView().findViewById(R.id.btnSchulfach);
    btn_fach.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MainActivity.openDialog();
        }
    });

    return rootview;


}

} }

Hey the SchulfachDialog you have created is a DialogFragment, and a DialogFragment can be opened by a fragment by itself, so you can directly open the dialog from your timetableFragment using childFragmentManager in the onClick like, 嘿,您创建的SchulfachDialog是一个DialogFragment,并且DialogFragment可以单独由一个片段打开,因此您可以使用onClick之类的childFragmentManager直接从时间表中打开对话框,

  btn_fach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              SchulfachDialog schulfachDialog = new SchulfachDialog();
              schulfachDialog.show(getChildFragmentManager(), "ecameo");

           }
        });

Also, 也,

View rootview = inflater.inflate(R.layout.timetable_fragment, container, false);
ImageButton btn_fach = getView().findViewById(R.id.btnSchulfach); 

Here instead of "getView()" use "rootview" otherwise it will give NullPointerException 在这里使用“ rootview”代替“ getView()”,否则会给出NullPointerException

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

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