我刚刚开始 Android 应用程序开发,在实现OnClickListener
. 我从 Android Studio 中的“空视图活动”开始。我在其他 UI 元素中添加了 a Button
、 anEditText
和ScrollView
包含 a 的 a TextView
。我OnClickListener
在按钮上设置了一个,以便发出 logcat 消息。
当我单击该按钮时,它会按预期发出 logcat 消息。但是,一旦我单击文本字段,该按钮就会在应用程序的剩余生命周期内变得无响应。奇怪的是,如果我删除ScrollView
.
这是我的 MRE:
活动主文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/startErrorText"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="160dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="160dp"
android:text="Start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textField" />
<EditText
android:id="@+id/textField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginTop="48dp"
android:layout_marginEnd="80dp"
android:ems="10"
android:hint="Enter some text"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:id="@+id/scrollView"
android:layout_width="409dp"
android:layout_height="585dp"
android:layout_marginStart="1dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/startButton"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/logView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.example.test
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button: Button = findViewById(R.id.startButton)
button.setOnClickListener(object : OnClickListener {
override fun onClick(v: View?) {
Log.i("MyApp", "Button pressed")
}
})
}
}
这是 logcat 输出:
ziparchive com.example.test W Unable to open '/data/data/com.example.test/code_cache/.overlay/base.apk/classes3.dm': No such file or directory
ziparchive com.example.test W Unable to open '/data/app/~~6ohH-jaL6kb5-3S5sLz1rw==/com.example.test-21xtpoSa2I8lnFAMirvZRA==/base.dm': No such file or directory
ziparchive com.example.test W Unable to open '/data/app/~~6ohH-jaL6kb5-3S5sLz1rw==/com.example.test-21xtpoSa2I8lnFAMirvZRA==/base.dm': No such file or directory
nativeloader com.example.test D Configuring clns-6 for other apk /data/app/~~6ohH-jaL6kb5-3S5sLz1rw==/com.example.test-21xtpoSa2I8lnFAMirvZRA==/base.apk. target_sdk_version=34, uses_libraries=, library_path=/data/app/~~6ohH-jaL6kb5-3S5sLz1rw==/com.example.test-21xtpoSa2I8lnFAMirvZRA==/lib/arm64, permitted_path=/data:/mnt/expand:/data/user/0/com.example.test
GraphicsEnvironment com.example.test V Currently set values for:
GraphicsEnvironment com.example.test V angle_gl_driver_selection_pkgs=[]
GraphicsEnvironment com.example.test V angle_gl_driver_selection_values=[]
GraphicsEnvironment com.example.test V ANGLE GameManagerService for com.example.test: false
GraphicsEnvironment com.example.test V com.example.test is not listed in per-application setting
GraphicsEnvironment com.example.test V Neither updatable production driver nor prerelease driver is supported.
libEGL com.example.test D loaded /vendor/lib64/egl/libEGL_emulation.so
AppCompatDelegate com.example.test D Checking for metadata for AppLocalesMetadataHolderService : Service not found
libEGL com.example.test D loaded /vendor/lib64/egl/libGLESv1_CM_emulation.so
libEGL com.example.test D loaded /vendor/lib64/egl/libGLESv2_emulation.so
om.example.test com.example.test W Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (unsupported, reflection, allowed)
om.example.test com.example.test W Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (unsupported, reflection, allowed)
Compatibil...geReporter com.example.test D Compat change id reported: 210923482; UID 10191; state: ENABLED
Compatibil...geReporter com.example.test D Compat change id reported: 171228096; UID 10191; state: ENABLED
Compatibil...geReporter com.example.test D Compat change id reported: 237531167; UID 10191; state: DISABLED
OpenGLRenderer com.example.test W Unknown dataspace 0
OpenGLRenderer com.example.test W Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
OpenGLRenderer com.example.test W Failed to initialize 101010-2 format, error = EGL_SUCCESS
Gralloc4 com.example.test I mapper 4.x is not supported
OpenGLRenderer com.example.test E Unable to match the desired swap behavior.
AutofillManager com.example.test D notifyViewEnteredForFillDialog:1073741824
EGL_emulation com.example.test D app_time_stats: avg=145.48ms min=0.87ms max=1433.47ms count=10
EGL_emulation com.example.test D app_time_stats: avg=181.58ms min=13.65ms max=2822.69ms count=17
MyApp com.example.test I Button pressed
ProfileInstaller com.example.test D Installing profile for com.example.test
EGL_emulation com.example.test D app_time_stats: avg=39.13ms min=0.77ms max=1255.29ms count=48
Compatibil...geReporter com.example.test D Compat change id reported: 163400105; UID 10191; state: ENABLED
ImeTracker com.example.test I com.example.test:39ecb7d6: onRequestShow at ORIGIN_CLIENT_SHOW_SOFT_INPUT reason SHOW_SOFT_INPUT
InputMethodManager com.example.test D showSoftInput() view=androidx.appcompat.widget.AppCompatEditText{4a6450d VFED..CL. .F.P..ID 250,132-830,256 #7f0801f9 app:id/textField aid=1073741824} flags=0 reason=SHOW_SOFT_INPUT
AssistStructure com.example.test I Flattened final assist data: 1492 bytes, containing 1 windows, 8 views
InsetsController com.example.test D show(ime(), fromIme=true)
InteractionJankMonitor com.example.test D Build configuration failed!
java.lang.IllegalArgumentException: Must pass in a valid surface control if only instrument surface;
at com.android.internal.jank.InteractionJankMonitor$Configuration.validate(InteractionJankMonitor.java:1259)
at com.android.internal.jank.InteractionJankMonitor$Configuration.<init>(InteractionJankMonitor.java:1217)
at com.android.internal.jank.InteractionJankMonitor$Configuration.<init>(Unknown Source:0)
at com.android.internal.jank.InteractionJankMonitor$Configuration$Builder.build(InteractionJankMonitor.java:1197)
at com.android.internal.jank.InteractionJankMonitor.begin(InteractionJankMonitor.java:611)
at android.view.inputmethod.ImeTracker$ImeJankTracker.onRequestAnimation(ImeTracker.java:717)
at android.view.InsetsController$InternalAnimationControlListener$2.onAnimationStart(InsetsController.java:448)
at android.animation.Animator$AnimatorListener.onAnimationStart(Animator.java:695)
at android.animation.Animator$AnimatorCaller$$ExternalSyntheticLambda0.call(Unknown Source:4)
at android.animation.Animator.callOnList(Animator.java:669)
at android.animation.Animator.notifyListeners(Animator.java:608)
at android.animation.Animator.notifyStartListeners(Animator.java:625)
at android.animation.ValueAnimator.startAnimation(ValueAnimator.java:1334)
at android.animation.ValueAnimator.start(ValueAnimator.java:1149)
at android.animation.ValueAnimator.start(ValueAnimator.java:1173)
at android.view.InsetsController$InternalAnimationControlListener.onReady(InsetsController.java:470)
at android.view.InsetsAnimationThreadControlRunner.lambda$new$0(InsetsAnimationThreadControlRunner.java:129)
at android.view.InsetsAnimationThreadControlRunner.$r8$lambda$3zGKYd3XPzPnvMO2hiF8a88M6T0(Unknown Source:0)
at android.view.InsetsAnimationThreadControlRunner$$ExternalSyntheticLambda2.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.os.HandlerThread.run(HandlerThread.java:67)
ImeTracker com.example.test I com.example.test:39ecb7d6: onShown
EGL_emulation com.example.test D app_time_stats: avg=213.12ms min=0.85ms max=500.33ms count=5
EGL_emulation com.example.test D app_time_stats: avg=500.68ms min=499.36ms max=502.83ms count=3
这种情况java.lang.IllegalArgumentException
只是偶尔发生,但总体错误每次都会发生。
我在我的 Android Studio 中运行了你的代码:
ScrollView
重叠了button
一半。如果我activity_main.xml
在该部分中替换<ScrollView
该行经过
一切正常。然而,一般来说,您可以
activity_main.xml
更简单、更清晰地进行编程。当您单击 时
EditText
,键盘会弹出,并且 的空间较少ScrollView
。因为你的ScrollView
尺寸是固定的,所以它会与按钮重叠。Layout Inspector
您可以在我为您制作的屏幕截图中看到它:解决方案:您需要将
ScrollView
高度设置为0dp
。ConstraintLayout
将其解释为“填充所有可用空间”。当键盘弹出时,它只会占用可用空间,不会与按钮重叠。只要复制这个 xml 就可以了:
这是你的 ScrollView 现在的样子(检查它使用的空间):
建议:如果您使用,
ConstraintLayout
请尽量不要对大小进行硬编码,并尽可能依赖约束。