相关疑难解决方法(0)

Android:按下后退按钮时提示用户保存更改

我有一个活动,其中包含几个用户可编辑的项目(EditText字段,RatingBar等).如果按下后退/主页按钮并且已经进行了尚未保存的更改,我想提示用户.通过阅读android文档后,似乎这段代码应该放在onPause方法中.我已经尝试在onPause中放置一个AlertDialog,但是对话框会显示出来,然后立即关闭,因为没有任何东西阻止暂停完成.

这是我到目前为止所提出的:

@Override
protected void onPause() {
    super.onPause();

    AlertDialog ad = new AlertDialog.Builder(this).setMessage(
            R.string.rating_exit_message).setTitle(
            R.string.rating_exit_title).setCancelable(false)
            .setPositiveButton(android.R.string.ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            // User selects OK, save changes to db
                        }
                    }).setNeutralButton(android.R.string.cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            // User selects Cancel, discard all changes
                        }
                    }).show();
}
Run Code Online (Sandbox Code Playgroud)

我是在正确的轨道还是有另一种方式来完成我在这里要做的事情?任何帮助都会很棒!

java android

34
推荐指数
2
解决办法
4万
查看次数

如何使用InputConnectionWrapper?

我有一个EditText.现在我想让用户对此进行所有更改EditText并使用它们,然后手动将它们插入到EditText.我不希望用户直接更改文本EditText.这应该只通过我的代码完成(例如使用replace()setText()).

我搜索了一下,发现了一个有趣的类名InputConnectionWrapper.根据javadoc,它应作为给定的代理InputConnection.所以我将其子类化为:

private class EditTextInputConnection extends InputConnectionWrapper {

    public EditTextInputConnection(InputConnection target, boolean mutable) {
        super(target, mutable);
    }

    @Override
    public boolean commitText(CharSequence text, int newCursorPosition) {
                    // some code which takes the input and manipulates it and calls editText.getText().replace() afterwards
        return true;
    }

}
Run Code Online (Sandbox Code Playgroud)

为了初始化包装器,我在EditText-subclass中覆盖了以下方法:

public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    InputConnection con = super.onCreateInputConnection(outAttrs);
    EditTextInputConnection connectionWrapper = new EditTextInputConnection(con, true);
    return connectionWrapper;
} …
Run Code Online (Sandbox Code Playgroud)

android user-input input android-edittext

9
推荐指数
1
解决办法
6734
查看次数

标签 统计

android ×2

android-edittext ×1

input ×1

java ×1

user-input ×1