简体   繁体   English

屏幕方向更改时无法维护自定义对话框按钮的状态

[英]Unable to maintain the state of a custom dialog button when screen orientation changes

I have a customized dialog which i have created using DialogFragment. 我有一个使用DialogFragment创建的自定义对话框。 it uses a view which got one button and an edittext. 它使用一个视图,它有一个按钮和一个edittext。 in my edittext, i have implemented a textwatcher where if there are values in the edittext, the button becomes visible else it remains invisible. 在我的edittext中,我实现了一个textwatcher,如果edittext中有值,则按钮变为可见,否则它将保持不可见。

public  class ShowDialog extends DialogFragment {

private Button btnShowBalance;
private EditText balanceInquiryPinInput;

public ShowDialog(){

}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View myView = inflater.inflate(R.layout.fragment_balance_inquiry,null);

    btnShowBalance= (Button) myView.findViewById(R.id.btnShowBalance);

    balanceInquiryPinInput = (EditText) myView.findViewById(R.id.et_balance_inquiry_pin);

    balanceInquiryPinInput.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            if(s.length() > 0){
                btnShowBalance.setVisibility(View.VISIBLE);
            }else{
                btnShowBalance.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

     btnShowBalance.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showBalance(balanceInquiryPinInput,nameview,ShowDialog.this);
        }
    });


    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(myView);

    // Create the AlertDialog object and return it
    return builder.create();
}

}

Problem 问题

When i change my screen orientation to (let say) landscape, if i had typed anything to my edittext before changing my orientation, whatever i had typed is still visible in landscape, my button is visible But it becomes unclickable. 当我将我的屏幕方向改为(比如说)风景时,如果我在更改我的方向之前输入了任何内容,那么无论我输入的是什么,在风景中仍然可见,我的按钮是可见的但它变得无法点击。 i cannot click it. 我无法点击它。 i have to remove the dialog by pressing somewhere outside the dialog window or back button and create it again. 我必须通过按对话框窗口或后退按钮之外的某处删除对话框并再次创建它。

How can i make my button remain clickable even when one change the screen orientation? 即使更改屏幕方向,如何使我的按钮保持可点击状态?

Note: it is clickable before changing orientation. 注意:在更改方向之前可以单击它。

EDIT 编辑

My dialog is activated by a button which is in a fragment and not activity. 我的对话框由一个片段激活,该片段处于片段而非活动状态。 this question is not a duplicate of This one because the latter is an implentation on an activity and it's implementation is unreliable to my view and it's state in question(button) 这个问题不是一个重复的这一个 ,因为后者是一个活动的implentation和它的实现是不可靠的,以我的观点,它是有问题的状态(按钮)

EDIT xml for my custom dialog layout 编辑我的自定义对话框布局的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#044848"
>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:background="@drawable/check_balance"
    >

    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/et_balance_inquiry_pin"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:background="@drawable/enter_pin_textview"
        android:inputType="numberPassword"/>

    <Button
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="OK"
        android:id="@+id/btnShowBalance"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="10dp"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:background="@drawable/show_balance_ok_button"
        android:visibility="invisible" />

</LinearLayout>

The problem of the button being disabled may be due to the dialog no longer being functional due to the calling view being destroyed in the orientation change. 按钮被禁用的问题可能是由于对话视图在方向改变时被破坏而不再起作用。 So it may still be visible, but is really an artifact of the previous view before it was destroyed. 因此它可能仍然是可见的,但在它被销毁之前它实际上是前一个视图的工件。

You need to use methods that saves the state of your dialog fragment instance when it is created. 您需要使用在创建对话框片段实例时保存对话框片段实例状态的方法。 for example you can call 例如你可以打电话

setRetainInstanceState(true);

in your onCreateDialog method to retain the state of your DialogFragment instance when screen orientation changes. onCreateDialog方法中,在屏幕方向更改时保留DialogFragment实例的状态。

You may need to overide onDestroyView() to prevent dialog from been destroyed when screen orientation changes. 您可能需要覆盖onDestroyView()以防止在屏幕方向更改时销毁对话框。 like this 像这样

@Override
public void onDestroyView() {
    if (getDialog() != null && getRetainInstance())
        getDialog().setDismissMessage(null);
    super.onDestroyView();
}

Information about this and other fragment methods can been found here . 可以在此处找到有关此方法和其他片段方法的信息。

If you are adding click listeners in Dialog to your buttons, you need to add onclick for your new buttons objects create after onConfigurationChanged. 如果要将Dialog中的单击侦听器添加到按钮,则需要在onConfigurationChanged之后为新按钮对象添加onclick。

setting on click listener in xml is good practice for removing problem like this: 在xml中单击侦听器设置是删除这样的问题的好习惯:

so set on click in xml like this : 所以设置点击xml如下:

<Button
android:id="@+id/addnote"
android:onClick="closeDialog"
/>

now in activity call onclick where you want 现在在活动中调用onclick你想要的地方

public void closeDialog(View view) {
dialogObject.dismiss();
} 

after using on click in xml no need to call setOnClickListener 在xml上单击使用后无需调用setOnClickListener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android gridview会在更改方向时保持状态,但需要更新视图 - Android gridview maintain the state when changes orientation but need to update views 方向更改时更改自定义对话框的尺寸 - Change the dimension of a custom dialog when orientation changes 对话框打开时如何处理屏幕方向变化? - How do I handle screen orientation changes when a dialog is open? 如何通过方向更改来维护已过滤的CursorAdapter的状态? - How to maintain state of filtered CursorAdapter through orientation changes? 如何防止自定义视图在屏幕方向更改时丢失状态 - How to prevent custom views from losing state across screen orientation changes 如何防止自定义视图在“ Mono for Android”的屏幕方向更改中丢失状态 - How to prevent Custom Views from losing state across screen orientation changes for “ Mono for Android” 每次方向改变,按钮状态都会改变 - Every time orientation change, button state changes 屏幕方向改变时,不得忽略对话框 - Dialog must not be dismiss when screen orientation change 屏幕方向更改时,默认情况下,系统将销毁当前活动并创建一个新活动,同时保留其状态 - When the screen orientation changes the system will, by default, destroy the current activity and create a new one while preserving its state 片段方向改变时处理textview状态 - Handle textview state when fragment orientation changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM