[英]How To Know When App Goes To Background
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
. 我设法通过与合作,以解决这一问题OnVirtualKeyboardHidden
和OnVirtualKeybaordShown
的事件处理程序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;
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.