简体   繁体   English

如何知道应用何时进入后台

[英]How To Know When App Goes To Background

Introduction 介绍

Note, the solution provided in the link below did not help me solve my problem: 请注意,以下链接中提供的解决方案无法帮助我解决问题:

How to detect when an Android app goes to the background and come back to the foreground 如何检测Android应用何时进入后台并回到前台

Hallo Stack Overflow Community 哈罗堆栈溢出社区

I am trying to create simple Android app that allows a user to select text and upper or lower case it, along with a function to remove extra spaces. 我正在尝试创建一个简单的Android应用,该应用允许用户选择文本以及使用大写或小写形式,以及删除多余空格的功能。 My app has only three objects, namely two of TButton and one of TMemo . 我的应用程序只有三个对象,即两个TButton和一个TMemo I have encountered a problem with the TMemo and the virtual keyboard. 我在TMemo和虚拟键盘上遇到了问题。 Whenever the virtual keyboard pops up, it displays over the bottom of the TMemo . 只要虚拟键盘弹出,它就会显示在TMemo的底部。 I have managed to solve this problem by working with the OnVirtualKeyboardHidden and OnVirtualKeybaordShown event handlers of the TForm . 我设法通过与合作,以解决这一问题OnVirtualKeyboardHiddenOnVirtualKeybaordShown的事件处理程序TForm Here's how I did it: 这是我的操作方式:

procedure TfrmEditor.FormVirtualKeyboardHidden(Sender: TObject;
  KeyboardVisible: Boolean; const Bounds: TRect);
begin
  memInput.Align := memInput.Align.alClient;
end;

procedure TfrmEditor.FormVirtualKeyboardShown(Sender: TObject;
  KeyboardVisible: Boolean; const Bounds: TRect);
begin
  if memInput.Align <> memInput.Align.alTop then
  begin
    memInput.Align := memInput.Align.alTop;
    memInput.Height := memInput.Height - Bounds.Height;
  end;
end;

Problem 问题

So here's my problem: whenever the virtual keyboard is shown and I switch to another app and switch back, the virtual keyboard is hidden but the TMemo TAlignLayout ins't restored back to alClient . 所以这是我的问题:每当显示虚拟键盘并且我切换到另一个应用并切换回时,虚拟键盘都被隐藏,但是TMemo TAlignLayout不会还原回alClient

If anyone can help me with this TMemo and virtual keyboard problem I would really appreciate it. 如果有人可以帮助我解决TMemo和虚拟键盘问题,我将不胜感激。

Thank you in advance! 先感谢您!

You can use ScrollView in your activity's layout file. 您可以在活动的布局文件中使用ScrollView By this, you can scroll up and down to view the things hidden by the virtual keybooard. 这样,您可以上下滚动查看虚拟按键框隐藏的内容。

Here's an example: 这是一个例子:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fillViewport="true">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1">

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="75dp"
                android:text="Welcome!"
                android:id="@+id/textView"
                android:gravity="center"
                android:textColor="#33b5e5"
                android:textSize="35sp"/>

        </LinearLayout>  
    </LinearLayout>
</ScrollView>

Also, if you you want to "How to know when app goes in backgrond?", you can try adding this function: 另外,如果您想“如何知道应用程序何时进入背景?”,则可以尝试添加以下功能:

public void onPause()
{
    super.oPause();
}

This function is called when app goes in the background. 当应用在后台运行时会调用此函数。 There are more similar functions like onStart(),onResume,onRestart(),onStop() and onDestroy(). 还有更多类似的函数,例如onStart(),onResume,onRestart(),onStop()和onDestroy()。 They work as their name suggests. 正如他们的名字所暗示的那样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序进入后台时如何停止runnable? - How to stop runnable when the app goes to background? 应用程序进入后台时的sockettimeOutexception - sockettimeOutexception when app goes in background 如何识别appk在Kitkat(4.4)中的背景? - How to identify app when it goes in background in Kitkat (4.4)? 当应用在Android 4.4.2中进入后台时如何禁用位置侦听器 - How to disable location listener when app goes background in android 4.4.2 如何检测特定的 Android 应用程序何时进入后台? - How to detect when a specific Android app goes to the background? 用户返回后台应用程序时如何触发功能? - How to trigger a function when a user goes back to a background app? 如何检测Android应用程序何时进入后台并返回前台 - How to detect when an Android app goes to the background and come back to the foreground 如何在Android应用程序进入后台时隐藏屏幕信息 - How to hide screen informations off Android app when it goes to the background Android:如何知道哪些应用程序向前台打开并在后台运行时做些什么 - Android: How to know what application is opened to foreground and do something when it goes to background Android,跟踪应用何时进入后台 - Android, track when app goes to background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM