如何从android中的对话框调用片段?

时间:2016-04-20 12:03:42

标签: android dialog fragment

我想知道,如何从android中的对话框单击动作调用片段。该对话框处于另一个活动中。

下面是对话框部分代码

public static void Bookingconfirm(final Context _context, String title,String strMessage)
{
    final Dialog dialog1 = new Dialog(_context);

    Log.e("Point","1");
    dialog1.setContentView(R.layout.booking_success);
    dialog1.setCanceledOnTouchOutside(true);
    dialog1.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    dialog1.findViewById(R.id.d_location);
    LinearLayout btnok = (LinearLayout) dialog1.findViewById(R.id.booking_ok);


    btnok.setOnClickListener(new OnClickListener()
    {

        @SuppressLint("NewApi") @Override
        public void onClick(View arg0)
        {   
            Log.e("Point","2"); 

            //i wanted the click to fragment here
            // parent class of fragment Home.java and fragment is booking.java


        }
    });


    dialog1.show();

}

3 个答案:

答案 0 :(得分:0)

 Fragment fragment= booking.newInstance();
 Home mainActivity = (Home) context;
        FragmentManager manager = mainActivity.getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.container, fragment, "Frag");
        transaction.commit();

从对话框中点击上面的代码点击Listener ...希望这有帮助

答案 1 :(得分:0)

尝试以下代码:

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new booking());
fragmentTransaction.commit();

答案 2 :(得分:0)

Haroon和Pradeep的答案行不通。 您可以像这样将FragmentManager传递给对话框类:

 import android.support.v4.app.FragmentManager

 class TestDialog(val fragmentManager:FragmentManager) {

   fun show()
   {
      showFragmentButton.setOnClickListener{

       val fragmentTransaction = fragmentManager.beginTransaction()
       val fragment = PurchaseDetail()
        fragmentTransaction.replace(R.id.container, fragment, "cashier_fragment")
        fragmentTransaction.addToBackStack(null)
        fragmentTransaction.commit()

        }
   }
}

以及您的片段中

 TestDialog(getFragmentManager()).show()