android - 使用警报对话框摇动动画(如果无效信息摇动编辑文本并且不要关闭)

标签 android android-layout

所以我有一个对话框,用户可以在其中输入数据,这里是用户的年龄。所以我希望当/如果用户将 edittext 留空时,edittext 将像 api 演示中那样做摇动动画。到目前为止,当输入无效信息时,我无法让对话框不被关闭。谢谢。

mInflater = (LayoutInflater) Information.this.getSystemService(LAYOUT_INFLATER_SERVICE);
        mLayout = mInflater.inflate(R.layout.agedialog, null);

        mAgeEditText = (EditText) mLayout.findViewById(R.id.AgeEditText);
        mAgeResultTextView = (TextView) findViewById(R.id.AgeResultTextView);
        final Animation shake = AnimationUtils.loadAnimation(Information.this, R.anim.shake);

        mInputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mInputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

        new AlertDialog.Builder(Information.this).setView(mLayout).setTitle(R.string.EnterYourAge)
                .setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        mInputManager.hideSoftInputFromWindow(mAgeEditText.getWindowToken(), 0);
                        if (TextUtils.isEmpty(mAgeEditText.getText())) {
                            mAgeEditText.startAnimation(shake);
                            // here the dialog dismisses even if I call startAnimation
                        }
                        else {
                            HelperClass.getInstance().setAge(Integer.parseInt(mAgeEditText.getText().toString()));
                            mAgeResultTextView.setText(HelperClass.getInstance().getAge());
                        }
                    }
                }).setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        mInputManager.hideSoftInputFromWindow(mAgeEditText.getWindowToken(), 0);
                        dialog.cancel();
                    }
                }).show();

最佳答案

一个额外的夏日爱情小故事:

如果你想摇动整个AlertDialog,而不仅仅是EditText,你可以在你的对话框 fragment 中做这样的事情:

(注意!为了清楚起见,我省略了任何健全性检查,例如示例代码中上下文和窗口的空验证)

public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Create the alert dialog as usual.
    AlertDialog shakyAlertDialog = new AlertDialog.Builder(getContext())
            .setTitle(R.string.title)
            .setMessage(R.string.message)    // Optional
            .setView(R.layout.input)         // This one holds your EditText
            .setNegativeButton(R.string.cancel, null)
            .setPositiveButton(R.string.ok, null)
            .show();

    // Get hold of the edit text we want to validate on.
    EditText input = shakyAlertDialog.findViewById(R.id.age_input);

    // Replace the "OK" button's click listener. As discussed in previous
    // answers this will replace the auto-dismiss behavior.
    shakyAlertDialog
            .getButton(DialogInterface.BUTTON_POSITIVE)
            .setOnClickListener(view -> {
                if (input.getText().isEmpty()) {
                    // This is the gold. This is how we shake the entire
                    // AlertDialog, not only the EditText.
                    shakyAlertDialog
                            .getWindow()
                            .getDecorView()
                            .animate()
                            .translationX(16f)
                            .setInterpolator(new CycleInterpolator(7f))
                } else {
                    // Do something with the input, but don't
                    // forget to manually dismiss the dialog.
                    shakyAlertDialog.dismiss()
                }
            });

    return shakyAlertDialog;
}

关于android - 使用警报对话框摇动动画(如果无效信息摇动编辑文本并且不要关闭),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5577608/

相关文章:

android - 如何将textview对齐到屏幕的右上角

android - 表布局分离

java - 在 Android 上为 Mandelbrot 集着色时出现问题

Android 风格不起作用

java - 如何将循环中的值放入数组列表中?

java - 闹钟响起后如何终止调用 setAlarm。

android - CoordinatorLayout中如何让ViewStub在AppBarLayout下面

java - 填充 Spinner 而不使用 Strings.xml 文件

android - 如何使用安卓模拟器测试蓝牙应用?

android - Android 10 (api 29) 中没有这样的文件或目录