我在尝试使用 TextInputLayout 和 TextInputEditText 时遇到了问题。 这是我的代码
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SignUpActivity">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Enter Password"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="52dp"
android:inputType="textPassword"
android:lines="1"
android:hint="Enter Passwrod"/>
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
设计页面应该有显示的东西。我已经查看了几个 youtube 教程视频,但那些不能帮助我解决问题。
这是我的build.gradle(模块)
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.james.kfarm'
compileSdk 33
defaultConfig {
applicationId "com.james.kfarm"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
//Splash Screen API
implementation 'androidx.core:core-splashscreen:1.0.0-beta02'
}
看了几个关于textinputlayout和textinputedittext的使用问题。一些答案建议升级 "androidx.appcompat:appcompat" 和 "com.google.android.material:material" 的版本。但是我已经将它们升级到最新版本并且它不起作用。
我也重建了项目作为问题“添加视图后Android Studio无法显示设计布局”建议
如果有人能帮助解决问题,那将是对我很大的帮助和安慰。感谢您阅读这么长的问题。
经过多次尝试,我找到了解决问题的方法。 事实上,您在活动中使用的主题并不是所需要的。在我的例子中,消息表明该活动需要“AppCompat”来实例化 textInputLayout 和 textInputEditText。我添加了添加新样式
下面的代码是我在活动中使用的主题
<style name = "Theme.App.Fullscreen" parent= "Theme.AppCompat.DayNight.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
我已经导航到 AndroidManifest.xml 并更改相关活动的主题
<activity
android:name=".SignUpActivity"
android:exported="false"
android:theme="@style/Theme.App.Fullscreen">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
相关代码全部改完后,我关闭了Android Studio,重新打开。它最终显示了文本输入布局和文本输入编辑文本。
但是,当你想更改textinputlayout的样式时,比如outlined box,你可能需要将主题从AppCompat更改为MaterialComponent以避免实例化错误