Android-帮助使用EditText修复“自定义警报"对话框 [英] Android- Help fixing Custom Alert Dialog with EditText

查看:117
本文介绍了Android-帮助使用EditText修复“自定义警报"对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:有一个自定义的Listview,每个列表项中都有一个按钮.当您单击按钮时,将显示一个带有编辑文本和提交按钮的alertDialog.这仅在第一次单击时发生,在随后的单击中,Toast将仅显示其到目前为止被单击的次数.

Context: There is a custom Listview and each list item has a button in it. When you click the button an alertDialog appears with an edit text and submit button. This only happens on the first click, on subsequent clicks a Toast will simply appear with the number of times it has been clicked thus far.

当您单击提交按钮时,将出现一个祝酒词,显示输入到editText中的文本以及他们单击它的次数,该次数通常为1,因为只有在第一次单击时才会发生这种情况.

When you click the submit button a toast will appear displaying the text that was entered into the editText and the number of times they have clicked on it which will presumably always be 1 since this can only happen on the first click.

问题:如果用户在单击提交"之前点击了"editText",那么timesClicked计数器无法正常工作.我猜是0.但是,如果用户 not 单击editText,则程序将正常运行. 0_o我很茫然.

Problem: The timesClicked counter is not working properly if the user so much as clicks on the editText before clicking submit. It is restting to 0 I guess. However if the user does not click on the editText then the program works normally. 0_o I'm at a loss.

尝试解决的问题:我将代码简化了很多,以尝试找出问题所在,这就是我遇到的问题.最初,我只是夸大一个只有编辑文本的视图,然后才使用builder.setPositiveButtton.我认为直接在视图中实现按钮将解决此问题,但事实并非如此.我已经坚持了一段时间.任何帮助都很好

Attempts at solving: I simplified the code down quite a bit to try and pinpoint the problem and this is where I am stuck. Originally I was inflating a view that only had an edit text and then I was just using builder.setPositiveButtton. I thought implementing the buttons directly in the view would fix it but that doesn't seem to be the case. I have been stuck on this for awhile. Any help would be great

此处是发生错误的视频

 private class OnSubtractClickListener implements View.OnClickListener  {

    final int id;  //id of list item that was clicked
    int timesClicked;
    Toast toast;

    public OnSubtractClickListener(int id, View view) {
        super();
        this.id = id;
        timesClicked = 0;
    }

@Override
    public void onClick(View view) {
        if (timesClicked != 0) {
             toast.setText(Integer.toString(timesClicked));
             toast.show();
        }
        else{
            toast = Toast.makeText(view.getContext(), "", Toast.LENGTH_SHORT);
            final View dialogView =  LayoutInflater.from(view.getContext()).inflate(R.layout.dialog_add_notes, null);

            AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
            builder.setView(dialogView);
            builder.setTitle("Subtract cigar?");
            builder.setIcon(R.mipmap.monkey_launcher);
            final AlertDialog dialog = builder.create();

            Button yesButton = (Button)dialogView.findViewById(R.id.dialog_notes_yes_button);
            yesButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    EditText editText = (EditText)dialogView.findViewById(R.id.dialog_editText);
                    String userInput = editText.getText().toString();
                    String timesClickedString = Integer.toString(++timesClicked);
                    toast.setText(timesClickedString + ": " + userInput);
                    toast.show();
                    dialog.dismiss();
                }
            });

            dialog.show(); //new
        }
    }
}

推荐答案

我找到了解决方案.基本上发生的事情是,当键盘出现时,它将导致列表视图调整大小,从而使用对话框出现之前的列表项的回收/旧版本重新创建整个列表视图-有效地撤消了对话框对列表视图项所做的任何更改

I found the solution. Basically what was happening was that when the Keyboard appeared it would cause the listview to adjust the size recreating the whole listview with recycled/old versions of the list items from before the dialog appeared -effectively undoing any changes made to the ListView items by the dialog.

在您的列表视图XML中添加以下内容:

In your listview XML add this:

android:descendantFocusability ="beforeDescendants"

android:descendantFocusability="beforeDescendants"

在Mainfest.xml中:

In Mainfest.xml:

<activity android:name= ".yourActivity"      
android:windowSoftInputMode="adjustPan"/>

这篇关于Android-帮助使用EditText修复“自定义警报"对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆