【问题标题】:Unable to maintain the state of a custom dialog button when screen orientation changes屏幕方向更改时无法保持自定义对话框按钮的状态
【发布时间】:2015-12-08 06:49:15
【问题描述】:

我有一个使用 DialogFragment 创建的自定义对话框。它使用一个有一个按钮和一个编辑文本的视图。 在我的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();
}

}

问题

当我将屏幕方向更改为(比如说)横向时,如果我在更改方向之前在编辑文本中输入了任何内容,我输入的内容在横向中仍然可见,我的按钮可见 但它变得无法点击.我不能点击它。我必须通过按下对话框窗口或后退按钮之外的某个位置来删除对话框并重新创建它。

即使改变屏幕方向,我如何才能让我的按钮保持可点击状态?

注意:在改变方向之前是可以点击的。

编辑

我的对话框由片段中的按钮激活,而不是活动。 这个问题不是This one 的重复,因为后者是对活动的实施,它的实现对我的观点不可靠,而且它的状态有问题(按钮)

为我的自定义对话框布局编辑 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>

【问题讨论】:

  • @MrsEd 如何恢复按钮的已保存实例状态?我的意思是我发现很难在您的评论链接中实现答案。
  • @MrsEd 我还没有找到答案,兄弟。您链接中的那个对我来说很复杂和困惑。我在这里找不到与我的按钮的良好关系。
  • @MrsEd 我已经发布了带有按钮的自定义对话框的 xml。是的,即使屏幕方向改变,我也会从用户输入中获取文本。只是按钮变得不可点击

标签: android android-dialogfragment android-dialog android-textwatcher


【解决方案1】:

按钮被禁用的问题可能是由于调用视图在方向更改中被破坏而导致对话框不再起作用。所以它可能仍然可见,但实际上是之前视图被破坏之前的产物。

您需要使用在创建对话框片段实例时保存其状态的方法。例如,您可以调用

setRetainInstanceState(true);

在您的 onCreateDialog 方法中,以在屏幕方向更改时保留 DialogFragment 实例的状态。

您可能需要覆盖onDestroyView() 以防止在屏幕方向更改时对话框被破坏。像这样

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

关于这个和其他片段方法的信息可以在here找到。

【讨论】:

    【解决方案2】:

    如果您在 Dialog 中为按钮添加点击监听器,您需要为在 onConfigurationChanged 之后创建的新按钮对象添加 onclick。

    在 xml 中设置点击监听器是消除此类问题的好习惯:

    像这样在 xml 中点击设置:

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

    现在在活动中调用 onclick 你想要的地方

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

    在xml中使用on click后无需调用setOnClickListener

    【讨论】:

    • 如果我的方法有一些参数怎么办?顺便说一下,这个对话框是在一个片段中实现的。我在 xml 中收到警告 Cannot resolve symbol xxx check if method specified in onclick xml attribute is declared in related activity
    • 出现错误! could not find method showBal in the activity 这个方法是在一个片段而不是活动中。有什么办法可以在 xml onClick 属性中使用它?
    • sry 但是..我听不懂你在说什么..稍微清楚一点
    • 我不认为您的答案适用于我的情况,因为您无法在 Fragment 类中为按钮(来自对话框的布局)注册 onClick 回调(使用 android:onClick)因为Android不会找到它,因为它只会在 Activity 中搜索与该名称匹配的方法(showBal),如果找不到metnod,它将抛出该异常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多