2017-09-28 10 views
-1

私はAndroidを覚えて覚えているので、間違いを理解したい。 これはAlertDialogクラスの確認/キャンセルです。コードを分割し、1つの活動のすべてを入れないために別のクラスでそれを作成した:正しくしていますか?

public class ConversationAlertDialog extends DialogFragment { 

    public static Dialog createDialog(Context context) { 
     // Use the Builder class for convenient dialog construction 
     AlertDialog.Builder builder = new AlertDialog.Builder(context); 

     builder.setMessage("Continue?") 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // User confirms 

        } 
       }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // User cancelled 
        } 
       }); 
     // Create the AlertDialog object and return it 
     return builder.create(); 
    } 
} 

これは私がAlertDialogを表示私の活動です:

buttonConvert.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      ConversationAlertDialog.createDialog(ConversionActivity.this).show(); 
     } 
    }); 

次の私は、私はダイアログを書くことができるようにコールバックを作成したいですアクティビティの動作をキャンセル/確認します。このようにして、すでにActivityに書き込まれているコードを使用する方が簡単になります。私はまだコールバックについては分かりませんが、私はインターフェイスを実装すべきだと思います。

ヒントはすべて私を助けます。

答えて

1

彼は私が使用するコードです。わずかに異なるタスクのためにいくつかの同様のダイアログを作成できるので、非常に柔軟性があります。レイアウトファイルを作成する必要があります。これにより、機能とスタイルの柔軟性が大幅に向上します。

私のレイアウトファイルはfragment_ok_cancel_dialogです。

ダイアログを呼び出すアクティビティでは、リスナを実装する必要があります。

implements OkCancelDialogFragment.OkCancelDialogListener 

私のコードでは、タイトルとメッセージをアクティビティのニーズに合わせて変更することができます。

private void callMyDialog(){ 
    //Customize the title and message as needed 
    String title = "This is my dialog title"; 
    String mess = "This is my dialog message"; 
    OkCancelDialogFragment dialog = OkCancelDialogFragment.newInstance(title, mess); 
    dialog.show(getFragmentManager(), "OkCancelDialogFragment2"); 
} 

これで、DialogFragmentを呼び出すアクティビティでダイアログコールバックを実装する必要があります。

@Override 
public void onFinishOkCancelDialog(boolean submit) { 
    if(submit){ 
     // Do something positive 
    } 
    else{ 
     // Do something negative 
    } 
} 

は今DialogFragmentのためのコード:

public class OkCancelDialogFragment extends DialogFragment { 

    private static final String ARG_TITLE = "title"; 
    private static final String ARG_MESSAGE = "message"; 

    Context context = null; 

    private String title; 
    private String message; 
    private boolean submitData = false; 

    private OkCancelDialogListener mListener; 

    public OkCancelDialogFragment() { 
    } 

    public static OkCancelDialogFragment newInstance(String title, String message) { 
     OkCancelDialogFragment fragment = new OkCancelDialogFragment(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_TITLE, title); 
     args.putString(ARG_MESSAGE, message); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      title = getArguments().getString(ARG_TITLE); 
      message = getArguments().getString(ARG_MESSAGE); 
     } 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle saveIntsanceState){ 

     context = getActivity(); 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

     LayoutInflater inflater = getActivity().getLayoutInflater(); 

     View rootView = inflater.inflate(R.layout.fragment_ok_cancel_dialog, null, false); 
     final TextView titleView = (TextView)rootView.findViewById(R.id.tvTitle); 
     final TextView messView = (TextView)rootView.findViewById(R.id.tvMessage); 

     titleView.setText(title); 
     messView.setText(message); 

     builder.setView(rootView) 
//    .setTitle(title) 
       .setPositiveButton(R.string.ok_button_dialog_title, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 

         submitData = true; 
         if(mListener == null) mListener = (OkCancelDialogListener) context; 
         mListener.onFinishOkCancelDialog(submitData); 
        } 
       }) 
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         submitData = false; 
         if(mListener == null) mListener = (OkCancelDialogListener) context; 
         mListener.onFinishOkCancelDialog(submitData); 
        } 
       }); 
     return builder.create(); 
    } 


    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     try { 
      if(mListener == null) mListener = (OkCancelDialogListener) context; 
     } 
     catch (Exception ex){ 
      throw new RuntimeException(context.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 


    public interface OkCancelDialogListener { 
     void onFinishOkCancelDialog(boolean submit); 
    } 

} 

.setTitle(title)は、API 23以上のために有効であることに注意してください(または多分API 21以上?)。

1

ConversationAlertDialogクラスにインターフェイスを作成します。

public interface onActionSelect{ 
void onOkSelect(); 
void onCancelSelect(); 
} 

アクティビティで実装し、ConversationAlertDialogクラスへの参照を渡します。そのようなダイアログから呼び出します。

// For Positive Button Click 
    public void onClick(DialogInterface dialog, int id) { 
    // User confirms 
    if(mCallBack != null) mCallBack.onOkSelect() 
} 

    // For Negative Button Click 
    public void onClick(DialogInterface dialog, int id) { 
    // User confirms 
    if(mCallBack != null) mCallBack.onCancelSelect() 
} 

肯定または否定ボタンをクリックするとMainActivityで通知されます。

1
public class ConversationAlertDialog extends DialogFragment { 
    private onMyEvent event; 
    public void setMyEvent(onMyEvent e) 
    { 
     this.event=e; 
    } 
    public static Dialog createDialog(Context context) { 
     // Use the Builder class for convenient dialog construction 
     AlertDialog.Builder builder = new AlertDialog.Builder(context); 

     builder.setMessage("Continue?") 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // User confirms 
         if(null!=event) 
         { 
          event.ok(); 
         } 
        } 
       }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         if(null!=event) 
         { 
          event.cancel(); 
         } 
         // User cancelled 
        } 
       }); 
     // Create the AlertDialog object and return it 
     return builder.create(); 
    } 
    public interface onMyEvent{ 
     void ok(); 
     void cancel(); 
    } 
} 

buttonConvert.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     Dialog dialog=ConversationAlertDialog.createDialog(ConversionActivity.this); 
     dialog.setMyEvent(new onMyEvent{....}) 
     dialog.show(); 
    } 
}); 
関連する問題