带有EditText和肯定的否定按钮的警告对话框

问题描述 投票:0回答:3

我正在尝试将DialogFragment创建为

    public class PlaceFragment extends DialogFragment {
      public PlaceFragment() {
      }

      public static PlaceFragment newInstance() {
        PlaceFragment placeFragment = new PlaceFragment();
        Bundle args = new Bundle();
        args.putDouble("Latitude", 12.3456);
        placeFragment.setArguments(args);
        return placeFragment;
      }
     @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      View v = inflater.inflate(R.layout.fragment_place, container, false);
    AlertDialog.Builder placePicker = new AlertDialog.Builder(getContext());
    placePicker.setView(R.layout.fragment_place)
        .setTitle("Enter Latitude and Longitude")
        .setPositiveButton("Ok", null);
//    return inflater.inflate(R.layout.fragment_place, container);
    return v;
  }
    }

应称为:

case R.id.action_geolocate:
        FragmentManager fm = getSupportFragmentManager();
        PlaceFragment placeFragment = PlaceFragment.newInstance();
        placeFragment.show(fm, "");
        return true;

但是,正如您所看到的,这是行不通的,因为这显然只显示了已夸大的布局。但是我试图用两行EditText的Ok和Dissmiss按钮来打开对话框。

如果我使用完全手动的方式,那么我将获得editText,但不知道如何使用OkButton获得那些值,例如:

 @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_place, container);
  }
android android-dialogfragment
3个回答
0
投票

您可以

  • 创建一个类扩展DialogFragment
  • 重写并实现onCreateDialog来配置对话框的显示方式。在这里,创建一个Dialog / AlertDialog,也可以在这里填充其布局。
  • 在某些情况下,您想实现onCreateView并在那里处理视图/操作。

希望这会有所帮助。


0
投票

我认为您应该替换此

我希望为您工作。


0
投票

如果我理解正确,类似这样的方法应该起作用:

 public class DialogEnterExit extends DialogFragment {

        public interface EnterExitDialogListener {
            void onFinishEditDialog(String event, String place, Location location);
        }

        EnterExitDialogListener mListener;

        private static final String TAG = DialogEnterExit.class.getSimpleName();

        AppCompatDialog mDialog;
        String mPlace;
        Location mLocation;



        public static DialogEnterExit newInstance(String place, Location location) {
            DialogEnterExit f = new DialogEnterExit();

            // Supply num input as an argument.
            Bundle args = new Bundle();
            args.putString("PLACE", place);
            args.putParcelable("LOCATION", location);
            f.setArguments(args);

            return f;
        }


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mPlace = getArguments().getString("PLACE");
            mLocation = getArguments().getParcelable("LOCATION");
        }

        @NonNull
        @Override
        public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            LayoutInflater layoutInflater = getActivity().getLayoutInflater();
            View view = layoutInflater.inflate(R.layout.layout_enter_exit_dialog,null);

            final RadioButton enter = view.findViewById(R.id.radio_enter);
            final RadioButton exit = view.findViewById(R.id.radio_exit);
            TextView place = view.findViewById(R.id.place_picked);

            place.setText(mPlace);

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

                    if (mListener != null) {

     mListener.onFinishEditDialog(getString(R.string.dialog_transition_entered), mPlace, 
      mLocation);
                    }
                    mDialog.dismiss();
                }
            });

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

                    if (mListener != null) {

     mListener.onFinishEditDialog(getString(R.string.dialog_transition_exited), mPlace, 
   mLocation);
                    }

                    mDialog.dismiss();
                }
            });


            builder
                    .setView(view)
                    .setTitle(getString(R.string.dialog_title_event))
                    .setCancelable(true);

            mDialog = builder.create();
            return mDialog;
        }


        @Override
        public void onAttach(@NonNull Context context) {
            super.onAttach(context);

            // Verify that the host activity implements the callback interface
            try {
                // Instantiate the EditNameDialogListener so we can send events to the 
           host
                mListener = (EnterExitDialogListener) context;
            } catch (ClassCastException e) {
                // The activity doesn't implement the interface, throw exception
                throw new ClassCastException(context.toString()
                        + " must implement EnterExitDialogListener"  );
            }


        }

        @Override
        public void onDetach() {
            super.onDetach();

            mListener = null;
        }
    }

和xml中的视图

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="place:"
                android:layout_weight="1"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/place_picked"
                android:hint="place name"
                android:layout_weight="3"/>
        </LinearLayout>

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radio_enter"
            android:text="@string/geofence_transition_entered"/>

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radio_exit"
            android:text="@string/geofence_transition_exited"/>
        </LinearLayout>
    </LinearLayout>
© www.soinside.com 2019 - 2024. All rights reserved.