应用程序关闭,同时保留编辑文本

时间:2018-09-11 11:47:32

标签: android android-fragments android-edittext

我有一个应用程序在一个活动中包含多个片段。
其中一个片段是使用带有基于相机更改侦听器的用户地址自动填充的编辑文本的实现文本实现的地图,而当我从该地图片断中按回时,我设法返回到先前的片段,但是一旦单击该地图片断中的编辑文本,便按回按钮关闭整个应用程序,而无需强行关闭。  我不知道确切的问题。  对不起,我的英语不好。

public class Maps extends Fragment implements OnMapReadyCallback {

    Unbinder unbinder;

    @BindView(R.id.address_submit)
    Button addressSubmit;

    private GoogleMap mMap;

    IDU idu;
    User user;

    EditText addressDoor, addressAddressline, addressStreet, addressCity, addressPincode, addressState;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_maps, container, false);
        unbinder = ButterKnife.bind(this, rootView);

        Activity_Dashboard.navigation.hideBottomNavigation();

        idu = new IDU_Presenter(this, getActivity());

        user = SharedPreferenceManager.getInstance(getActivity()).getUser();

        addressDoor = (EditText)rootView.findViewById(R.id.address_door);
        addressAddressline = (EditText)rootView.findViewById(R.id.address_addressline);
        addressStreet = (EditText)rootView.findViewById(R.id.address_street);
        addressCity = (EditText)rootView.findViewById(R.id.address_city);
        addressState = (EditText)rootView.findViewById(R.id.address_state);
        addressPincode = (EditText)rootView.findViewById(R.id.address_pincode);

        final RippleBackground rippleBackground = (RippleBackground) rootView.findViewById(R.id.content);
        ImageView b = (ImageView) rootView.findViewById(R.id.confirm_address_map_custom_marker);

        rippleBackground.startRippleAnimation();
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.

        if (mMap == null) {
            SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
        }

        rootView.setFocusableInTouchMode(true);
        rootView.requestFocus();
        rootView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // Log.i(tag, "keyCode: " + keyCode);
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {

                    Fragment_Address_list fragment = new Fragment_Address_list();
                    FragmentActivity activitys = (FragmentActivity) getActivity();
                    FragmentManager fm = activitys.getSupportFragmentManager();
                    FragmentTransaction ft = fm.beginTransaction();
                    ft.replace(R.id.frame_layout, fragment);
                    ft.commit();

                    return true;

                }
                return false;
            }
        });

        return rootView;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;

        final Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());

        mMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
            @Override
            public void onCameraIdle() {
                double lat = mMap.getCameraPosition().target.latitude;
                double lng = mMap.getCameraPosition().target.longitude;

                Location startPoint = new Location("locationA");
                startPoint.setLatitude(11.010519);
                startPoint.setLongitude(76.986481);
                Location startPoint1 = new Location("locationb");
                startPoint1.setLatitude(lat);
                startPoint1.setLongitude(lng);

                List<Address> addresses = null;

                try {

                    addresses = geocoder.getFromLocation(lat, lng, 1);

                    if (addresses.size() == 0) {

                        Toast.makeText(getActivity(), "Invalid location", Toast.LENGTH_SHORT).show();

                    } else {

                        if (addresses.get(0).getThoroughfare() == null) {

                            addressAddressline.setText(addresses.get(0).getSubLocality());

                        } else {

                            addressAddressline.setText(addresses.get(0).getThoroughfare());

                        }

                        addressStreet.setText(addresses.get(0).getSubLocality());

                        addressCity.setText(addresses.get(0).getLocality());
                        addressPincode.setText(addresses.get(0).getPostalCode());
                        addressState.setText(addresses.get(0).getAdminArea());

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        LatLng TutorialsPoint = new LatLng(11.010519, 76.986481);

        MarkerOptions markerOptions = new MarkerOptions().position(TutorialsPoint).title("Your location");
        Marker mMarker = mMap.addMarker(markerOptions);
        mMarker.showInfoWindow();
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(TutorialsPoint, 18f));

    }

Log details

1 个答案:

答案 0 :(得分:0)

您可以覆盖onBackPressed()方法,该方法在用户单击“后退”按钮时触发。

@Override
public void onBackPressed() {
    //DO YOUR STUFF LIKE SHOWING DIALOG
    new AlertDialog.Builder(this)
            .setMessage("Are you sure you want to exit?")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    MyActivity.super.onBackPressed();
                }
            })
            .setNegativeButton("No", null)
            .show();
}

您可以在此处将片段更改为上一个片段(这是使用fragmentManager时代码的默认行为),可以显示Toast或其他内容。

希望有帮助。