【问题标题】:Want to clear EditText content before activity goes in background想要在活动进入后台之前清除 EditText 内容
【发布时间】:2016-10-15 04:55:06
【问题描述】:

用例是当用户在edittext中输入它的信息并且有意或无意地用户在后台发送应用程序。在这种情况下,我不想在最近的应用程序列表屏幕截图中显示 edittext 信息,当用户再次恢复应用程序时,我想在 edittext 中填充相同的信息。

【问题讨论】:

标签: android android-activity android-edittext


【解决方案1】:

另一种选择是:

FLAG_SECURE - 将窗口内容视为安全,防止其出现在屏幕截图中或在非安全显示器上查看。

更多详情here

但是这也不允许截图(不确定你是否想要)

要使用它,将以下行添加到您的 onCreate()

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

----------------------编辑------- ------

如果您想在“最近的应用程序”列表中显示应用程序,但没有 editText,那么您可能想做这样的事情:

private string mySecretText;

@Override
public void onPause() {
    super.onPause();  // Always call the superclass method first

    //Now we remember the text
    mySecretText = myEditText.getText().toString();

    //Optional save it in your Shared Preferences
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("secretText", mySecretText);
    editor.apply();

    //Remove the text from the editText
    myEditText.setText("");
}

@Override
public void onResume() {
    super.onResume();  // Always call the superclass method first

    //Optional load it from your Shared Preferences
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    mySecretText = preferences.getString("secretText", "Default");  //U can remove default if u want

    myEditText.setText(mySecretText);
}

----------------------编辑------- ------

或者您可以更改完整的缩略图:

onCreateThumbnail - 为这个活动生成一个新的缩略图。此方法在暂停活动之前调用,并且应该在该位图的尺寸中将所需缩略图的图像绘制到 outBitmap 中。如果需要,它可以使用给定的画布进行渲染,该画布配置为绘制到位图中。

重要! 默认实现返回失败,不绘制缩略图;如果需要,这将导致平台创建自己的缩略图。

所以创建你自己的缩略图

@Override
public boolean onCreateThumbnail (Bitmap outBitmap, Canvas canvas) {
    Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.myBitmap);
    canvas.drawBitmap(myBitmap, 0, 0, null);

    return true;
}

祝你好运!

【讨论】:

  • 感谢您的回答,它确实帮助了我,但在某些情况下,我希望屏幕出现在最近的应用程序列表中,但在 edittext 视图中没有文本。
  • 添加了另一个答案!
【解决方案2】:

在执行的异步任务中添加这些行 1.如果你想隐藏编辑文本框然后editext.setVisibility(View.INVISIBLE); 要么 2.如果要清除内容则editext.clear();

【讨论】:

    【解决方案3】:

    您可以使用活动生命周期回调方法来清除(.clear())或填充(.setText("some text"))EditText。 p>

    onResume :用户看到并可以与活动交互。

    onPause : 活动部分或全部在后台。

    您可以将信息保存为 MODE_PRIVATE 中当前活动的共享首选项。

    SharedPreferences sharedPreferences;
    
    sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    

    编写共享首选项

    SharedPreferences.Editor editor = sharedPreferences.edit();
    
    editor.putString("INFO", "some text");
    
    editor.commit();
    

    阅读共享首选项

    String info = sharedPreferences.getString("INFO", "default value");
    

    所以你可以在 onResume 中读取 SP,然后在 onPause 中写入,就在你清除 EditText 内容之前。

    【讨论】:

    • 感谢您的回答!使用相同的方法,清除 onPause 中的 edittext 并在 onResume 中更新它。但问题是android在我清除onPause中的edittext之前记录了屏幕状态。
    • 您可以将此标签添加到任何不应显示在“最近的应用程序”中的活动。 : android:excludeFromRecents="true"
    • 否则,还有另一种选择。您可以在调用超类之前清除 onPause 中的内容。
    • android:excludeFromRecents="true" 不会帮助我,会尝试其他选项。谢谢!
    【解决方案4】:

    您可以尝试属性,android:noHistory="true" 用于清单文件中的活动标记。

    它会破坏痕迹。

    或者,如果您想在最近列表中显示空白视图,那么:

    覆盖onStop() 方法并尝试在调用之前将活动的内容视图重置为一些空白xml 文件 super.onStop()

    当调用activity.onStop() 时,Android 框架可能会对您的应用进行截图,以便在最近的应用中显示。

    希望它能解决你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-19
      • 2011-12-09
      • 2019-07-21
      • 2019-04-02
      • 2016-09-06
      • 2012-07-17
      • 1970-01-01
      相关资源
      最近更新 更多