2

我得到以下信息:

  • 一个名为的类DConce,其中包含我要使用的一个对话框的代码:

        public class DConce extends DialogFragment{
    
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder dshow = new AlertDialog.Builder(getActivity());
    
    LayoutInflater inflater = getActivity().getLayoutInflater();
    
    dshow.setView(inflater.inflate(R.layout.dialogconc, null))
    
           .setPositiveButton("Send", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
    
                   //TODO
    
    
               }
           })
           .setNegativeButton("Close", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   DConce.this.getDialog().cancel();
               }
           });      
    
    return dshow.create();
    
    }
    

和另一个类到一个片段public final class Sincr extends Fragment中,我需要在其中调用对话框:

 case R.id.btnConce:
    //here      
            break;

我该怎么做?

提前致谢。

4

4 回答 4

2

修改您的 DialogFragment:

public class DConce extends DialogFragment{

  public static DConce newInstance(){
    DConce f = new DConce();
    return f;
  }

  public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder dshow = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

    dshow.setView(inflater.inflate(R.layout.dialogconc, null))
      .setPositiveButton("Send", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
           //TODO
        }
      })
      .setNegativeButton("Close", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
          DConce.this.getDialog().cancel();
        }
      });     
    return dshow.create();
  }    
}

然后展示它;

case R.id.btnConce:
  DConce.newInstance().show(getChildFragmentManager(), null);
  break;

您当然可以选择传入一个Stringfortag参数,以唯一标识片段。

于 2013-04-03T10:26:40.603 回答
2

您需要添加到代码中的是 newInstance,例如:

public static DConce newInstance(){
    DConce arg = new DConce();
    return arg;
  }

然后在活动中:

FragmentManager manager = getSupportFragmentManager(); // or getFragmentManager, depends on which api lvl you are working on but supportFragmentManager will make you dialog work also on devices lower than api lvl 11(3.0 - > Honeycomb)
DialogFragment Dialog = DConce.newInstance();
Dialog.show(manager, "tag");
于 2013-04-03T10:30:55.947 回答
-1

按照这里的android文档:http: //developer.android.com/reference/android/app/DialogFragment.html

于 2013-04-03T10:29:40.707 回答
-1

创建静态方法并将您的代码放入 .

并直接按类名调用,即Dcon.myAlertMessage()

于 2013-04-03T10:31:34.133 回答