使用 dialog/Alertdialog 中的搜索栏更改 activity 中的设置

Use seekbars in dialog/Alertdialog to change settings in activity

我正在尝试创建一个应用程序来显示我在 googlemaps 中的位置。然后我有一些搜索栏可以更改我想显示我去过的地方的日期和时间。当我在屏幕上共享 space 的同一个活动中有 googlemaps 片段和搜索栏(使用 crystal 搜索栏)时,我得到了一个代码。但现在我想增加 googlemap 的大小,而不是在点击一个名为设置的按钮时通过对话框显示其他布局中的搜索栏。我无法让它工作,因为它抱怨视图,我不想删除视图,因为那样我的所有参数和东西都会变回 sarting 值并停止工作。下面我展示了 class 的代码和错误消息。

这里是mainactivity中seekbar的声明: @覆盖 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    layout_settings = (LinearLayout)inflater.inflate(R.layout.dialog_settings, null);

    //Initialize
    InitializeUI();

    //Makes buttons work
    onClickListener();

    //UpdateGuiStart
    updateStartGUI();

    //Seekbars
    SeekBarCheckBoxModeController();

}

这是我初始化所有搜索栏的地方:

 public void InitializeUI() {
    GPSButton = (Button) findViewById(R.id.GPSButton);
    mapsButton = (Button) findViewById(R.id.mapsButton);
    TextSpeed = (TextView) findViewById(R.id.TextSpeed);
    check_view = (TextView) findViewById(R.id.textView_check);
    deleteData = (Button) findViewById(R.id.button_delete);
    settings = (Button) findViewById(R.id.button_settings);
    TextSpeed.setText("Speed : " + speed);
    mModeSpinner = (Spinner) layout_settings.findViewById(R.id.spinner);
    mSeekBarTime = (CrystalSeekbar) layout_settings.findViewById(R.id.rangeSeekbarTime);
    mSeekBarDate = (CrystalRangeSeekbar) layout_settings.findViewById(R.id.rangeSeekbarDate);
    mSeekBarTimeLength = (CrystalSeekbar) layout_settings.findViewById(R.id.rangeSeekbarTimeLength);
    mSeekBarTransparency = (CrystalSeekbar) layout_settings.findViewById(R.id.rangeSeekbarTransparency);
    tvMinTime = (TextView) layout_settings.findViewById(R.id.textViewMinTime);
    tvMaxTime = (TextView) layout_settings.findViewById(R.id.textViewMaxTime);
    tvMinDate = (TextView) layout_settings.findViewById(R.id.textViewMinDate);
    tvMaxDate = (TextView) layout_settings.findViewById(R.id.textViewMaxDate);
    tvMaxDates = (TextView) layout_settings.findViewById(R.id.textViewMaxDates);
    tvTransparency = (TextView) layout_settings.findViewById(R.id.textViewTransparency);


    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.fragment_map);
    mapFragment.getMapAsync(this);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    //Initialize database helper
    mDbHelper = new LocationDbHelper(this);
}

这是我的警报对话框,用于显示我想在地图上进行更改的搜索栏,当我再次单击那里时,我希望我的搜索栏保持上次更改时离开的方式某事...

 settings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            minday = current_minday;
            maxday = current_maxday;
            mintime = current_time;
            maxtime = maxTime;
            trans = current_trans;
            mode = mModeSpinner.getSelectedItemPosition();

            final AlertDialog.Builder settings_alert = new AlertDialog.Builder(MainActivity.this);
            settings_alert.setView(layout_settings);
            settings_alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    show();
                    Toast.makeText(MainActivity.this, "Settings changed", Toast.LENGTH_LONG).show();
                    dialogInterface.dismiss();
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    mSeekBarDate.setMaxStartValue(maxday);
                    mSeekBarDate.setMinStartValue(minday);
                    mSeekBarTime.setMinStartValue(mintime);
                    mSeekBarTimeLength.setMinStartValue(maxtime);
                    mModeSpinner.setSelection(mode);
                    dialogInterface.dismiss();

                }
            }).show();

        }
    });

错误信息:

FATAL EXCEPTION: main
                                                                                        Process: com.example.smartcities.smartcitiesproject, PID: 30988
                                                                                        java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                                            at android.view.ViewGroup.addViewInner(ViewGroup.java:4654)
                                                                                            at android.view.ViewGroup.addView(ViewGroup.java:4490)
                                                                                            at android.view.ViewGroup.addView(ViewGroup.java:4462)
                                                                                            at android.support.v7.app.AlertController.setupCustomContent(AlertController.java:613)
                                                                                            at android.support.v7.app.AlertController.setupView(AlertController.java:452)
                                                                                            at android.support.v7.app.AlertController.installContent(AlertController.java:215)
                                                                                            at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257)
                                                                                            at android.app.Dialog.dispatchOnCreate(Dialog.java:578)
                                                                                            at android.app.Dialog.show(Dialog.java:314)
                                                                                            at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:953)
                                                                                            at com.example.smartcities.smartcitiesprojects.MainActivity.onClick(MainActivity.java:313)
                                                                                            at android.view.View.performClick(View.java:5697)
                                                                                            at android.widget.TextView.performClick(TextView.java:10814)
                                                                                            at android.view.View$PerformClick.run(View.java:22526)
                                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                            at android.os.Looper.loop(Looper.java:158)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

picture of seekbar dialog

    LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    layout_settings = (LinearLayout)inflater.inflate(R.layout.dialog_settings, null);

以上代码必须是 onClick 事件的本地代码。

应该是这样的:

  settings.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
        layout_settings = (LinearLayout)inflater.inflate(R.layout.dialog_settings, null);

        minday = current_minday;
        maxday = current_maxday;
        mintime = current_time;
        maxtime = maxTime;
        trans = current_trans;
        mode = mModeSpinner.getSelectedItemPosition();

        final AlertDialog.Builder settings_alert = new AlertDialog.Builder(MainActivity.this);
        settings_alert.setView(layout_settings);
        settings_alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                show();
                Toast.makeText(MainActivity.this, "Settings changed", Toast.LENGTH_LONG).show();
                dialogInterface.dismiss();
            }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                mSeekBarDate.setMaxStartValue(maxday);
                mSeekBarDate.setMinStartValue(minday);
                mSeekBarTime.setMinStartValue(mintime);
                mSeekBarTimeLength.setMinStartValue(maxtime);
                mModeSpinner.setSelection(mode);
                dialogInterface.dismiss();
            }
        }).show();
    }
});