我有一个关于 Android 应用程序架构的一般性问题。我正在实现一个应用程序(用Java),它有一个活动和20个片段(相似但不相同)。到目前为止,我在片段中实现了所有内容(UI、逻辑、数据库查询)。现在我正在考虑使用 ViewModel,但我不确定它是否值得付出努力。所以我的问题是我的 20 个片段中的每一个片段是否都应该有一个自己的 ViewModel,或者我是否可以为所有 20 个片段实现一个 ViewModel?为所有 Fragment 类实现一个 ViewModel 会大大增加工作量,所以我想知道是否可以为所有 Fragment 只使用一个 ViewModel ?
我使用单个活动并将多个片段嵌入其中。现在我想知道在单个活动类中,当前正在显示哪个片段。我怎样才能做到这一点?我从这里查看了解决方案How to Know if a Fragment is Visible?
MyFragmentClass test = (MyFragmentClass) getSupportFragmentManager().findFragmentByTag("testID");
if (test != null && test.isVisible()) {
//DO STUFF
}
else {
//Whatever
}
Run Code Online (Sandbox Code Playgroud)
但我不知道你的参数是什么"testID"
。此外,我尝试从获取当前片段对象中实现解决方案
Fragment currentFragment = getActivity().getFragmentManager().findFragmentById(R.id.fragment_container);
Run Code Online (Sandbox Code Playgroud)
但在这里我收到错误消息:“无法解析符号‘fragment_container’”
有谁知道如何获取使用单个活动和多个片段方法时显示的当前片段的名称?
我想将一个包含当前时间和日期的字符串存储到 Android 应用程序的数据库(SQLite)中。为此,我使用 SimpleDateFormat。不幸的是它没有显示正确的时间。我尝试了两种选择。
第一个选项(来自SimpleDateFormat 和 TimeZone)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", Locale.getDefault());
sdf.format(new Date());
Run Code Online (Sandbox Code Playgroud)
第二个选项(来自Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") 给出时区为 IST)
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss'Z'");
sdf2.setTimeZone(TimeZone.getTimeZone("CEST"));
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,时间都是错误的。我的笔记本电脑或手机显示的不是当地时间,但输出时间早了 2 小时。我怎样才能改变这一点?我希望我的计算机上也显示柏林的当前时间(CEST)。我很欣赏每一条评论。
我有一个滚动视图,其中有 4 个 ImageButtons。但是,当我在约束布局中定义约束时,我在 XML 文件中收到错误消息:“无法解析符号 @id/imageButton ”。我不明白这个错误消息,因为我已经定义了图像按钮的 id。有人能帮我吗?这是代码的一部分:
<ScrollView
app:layout_constraintTop_toBottomOf="@id/toolbar_mainActivity"
app:layout_constraintBottom_toTopOf="@id/bottom_layout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="0dp"
android:layout_height="0dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity"
tools:ignore="ExtraText">
<ImageButton
android:id="@+id/imageButton_1"
android:layout_width="0dp"
android:layout_height="128dp"
android:layout_marginTop="12dp"
android:background="#00000000"
android:scaleType="fitCenter"
app:layout_constraintEnd_toStartOf="@id/imageButton_2"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/test_dish_1" />
<ImageButton
android:id="@+id/imageButton_2"
android:layout_width="0dp"
android:layout_height="128dp"
android:layout_marginTop="12dp"
android:background="#00000000"
android:scaleType="fitCenter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toEndOf="@id/imageButton_1"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/test_dish_1" />
<ImageButton
android:id="@+id/imageButton_4"
android:layout_width="0dp"
android:layout_height="128dp"
android:layout_marginTop="12dp"
android:background="#00000000"
android:scaleType="fitCenter"
app:layout_constraintEnd_toStartOf="@id/imageButton_3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/imageButton_1"
app:layout_constraintVertical_chainStyle="packed"
app:srcCompat="@drawable/test_dish_1" />
<ImageButton
android:id="@+id/imageButton_3"
android:layout_width="0dp"
android:layout_height="128dp"
android:layout_marginTop="12dp"
android:background="#00000000"
android:scaleType="fitCenter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/imageButton_4"
app:layout_constraintTop_toBottomOf="@id/imageButton_1"
app:srcCompat="@drawable/test_dish_1" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
例如,错误显示在以下行中: 'app:layout_constraintEnd_toStartOf="@id/imageButton_2"'
这是 Android Studio …
我有一个我不明白的错误,我不知道如何解决它,尽管已经尝试了很多。错误是:
Could not find com.android.tools.build:gradle:7.6.
Searched in the following locations:
- https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/7.6/gradle-7.6.pom
- https://repo.maven.apache.org/maven2/com/android/tools/build/gradle/7.6/gradle-7.6.pom
Required by:
project :
Add google Maven repository and sync project
Open File
Run Code Online (Sandbox Code Playgroud)
所以我的 Java 版本或与 build.grandle 文件的连接有问题。我有Java JDK 19。这是build.gradle文件的代码
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
def nav_version = "2.3.0"
classpath 'com.android.tools.build:gradle:7.6'
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
classpath 'com.google.gms:google-services:4.3.10'
// NOTE: Do not place your application dependencies here; they belong
// in …
Run Code Online (Sandbox Code Playgroud) 我希望在 Android 中为我的布局添加一个加号/减号按钮(使用 Java),就像您在此处看到的那样https://ux.stackexchange.com/questions/99253/plus-minus-button-position-for -选择乘客数量。Android 中有原生元素可以使用它吗?我搜索了它,但找不到一个令我惊讶的元素,因为我认为这是应用程序中经常使用的元素(至少比您在 Android Studio 布局编辑器中看到的几个项目更常用)。
对于本机组件有一个简单的解决方案吗?我会对每一条评论感到高兴。