美文网首页
如何让返回键具有删除编辑内容功能?

如何让返回键具有删除编辑内容功能?

作者: RTThread小师弟 | 来源:发表于2017-11-06 17:24 被阅读9次

    [DESCRIPTION]

    在编辑界面,如果正在编辑,就删除编辑的内容。如果编辑的内容为空,按下返回键就返回上一界面

    请问该如何实现?

    [SOLUTION]

    修改Activity.java中onKeyUp 为如下:

    public boolean onKeyUp(int keyCode, KeyEvent event) {

    if (getApplicationInfo().targetSdkVersion

    >= Build.VERSION_CODES.ECLAIR) {

    if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()

    && !event.isCanceled()) {

    /// M: Fix ALPS00247686, ALPS00253881 Free test JE issue @{

    if (isResumed()) {

    View decorView = this.getWindow().getDecorView();//mtkmodifyBegin

    if(decorView!=null){

    View focus = decorView.findFocus();

    if(focus!=null&&focus instanceof EditText){

    (EditText) editText = (EditText)focus;

    CharSequence text = editText.getText();

    if(text!=null && text.length()>0){

    editText.clear();

    return true;

    }

    }

    }

    onBackPressed();//MTKmodify END

    return true;

    } else {

    Log.v(TAG, "Tracking Key Up, activity is resumed: " + isResumed());

    // Fix sub activity of tab activity which isn't in resumed state

    // Return false means didn't handle this key event

    return false;

    }

    /// @}

    }

    }

    return false;

    }

    来源:一牛网论坛

    相关文章

      网友评论

          本文标题:如何让返回键具有删除编辑内容功能?

          本文链接:https://www.haomeiwen.com/subject/bknlmxtx.html