将 id 赋予动态创建的视图
Giving id's to dynamically created views
我是编码初学者,希望得到一些帮助。我想做一个闹钟应用。在我的主页片段上,我添加了一个按钮,用于将警报添加到 ScrollView 内的 LinearLayout 中。警报将包含三个 TextView,以及一个用于 activation/deactivation.
的按钮
我希望我的闹钟看起来像这样(目前我的编码中没有任何地方使用它;我创建它只是为了让我的目标得到视觉帮助):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:id="@+id/alarm_fl"
android:background="@mipmap/white_box">
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="9.5dp"
android:id="@+id/alarm_activation_button"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="5dp"
android:textSize="10pt"
android:id="@+id/alarm_time"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="165dp"
android:layout_marginTop="11.5dp"
android:textSize="7pt"
android:id="@+id/alarm_ampm"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="30dp"
android:textSize="5pt"
android:id="@+id/alarm_day"/>
</FrameLayout>
这就是我目前在片段中测试警报的方式:
addAlarm.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
LayoutInflater alarm_inflater = (LayoutInflater) getContext().getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup parent = (ViewGroup) getActivity().findViewById(R.id.alarm_ll);
View alarm_view = alarm_inflater.inflate(R.layout.alarm_layout, parent);
TextView alarm_time = (TextView) alarm_view.findViewById(R.id.alarm_time);
alarm_time.setText("9시 45분");
TextView alarm_ampm = (TextView) alarm_view.findViewById(R.id.alarm_ampm);
alarm_ampm.setText("오후");
TextView alarm_day = (TextView) alarm_view.findViewById(R.id.alarm_day);
alarm_day.setText("월,화,수,목,금");
Button activation_button = (Button) alarm_view.findViewById(R.id.alarm_activation_button);
activation_button.setBackgroundResource(R.mipmap.checkbox_deactivated);
}
});
其中 alarm_ll 是我要用新创建的警报填充的 LinearLayout。
在我看来,我需要为每个 Buttons 和 TextViews 设置唯一的 ID,以便分别操作它们。
下面是我的问题:
如果我想在单击按钮时以编程方式添加视图,这是正确的方法吗?或者有更好更简单的方法吗?
我的闹钟最终需要成为对象。像用户这样的非 activity class 是否有可能拥有自己的布局?
如何在通过单击按钮创建时为每个视图指定唯一 ID?
当我现在测试-运行我的应用程序时,我添加到alarm_ll的布局不会被保存,所以如果我转移到另一个activity然后回来,alarm_ll 将被重置。当它们不是原始数据类型时,如何将它们保存在片段中?
很抱歉一次问这么多问题,但我真的很想得到一些答案或建议。谢谢。
我假设您希望能够设置多个闹钟。这是 ListView
或 RecyclerView
的完美用法,它允许您扩充同一视图的多个副本并显示基于某些基础数据的列表。
我建议您了解如何创建 "model" 存储数据的对象。这些 "model" 对象应该与显示数据的视图对象分开。有几种常用的设计模式用于这种分离:Model-View-Controller、Model-View-Presenter 和 Model-View-ModelView。
android:id
in XML 通常用于获取 Java 代码中视图的对象。当您动态创建视图时,无论是您在问题中显示的方式还是通过膨胀 XML,您已经拥有此对象,因此我认为不需要为这些动态创建的视图分配 ID。
当使用我在 #2 中建议的设计模式之一时,您还将创建一种存储数据的方法。 Android 提供了一个 API 来将信息存储在数据库中。这可以通过使用 "adapter".
轻松显示在 ListView
或 RecyclerView
中
我是编码初学者,希望得到一些帮助。我想做一个闹钟应用。在我的主页片段上,我添加了一个按钮,用于将警报添加到 ScrollView 内的 LinearLayout 中。警报将包含三个 TextView,以及一个用于 activation/deactivation.
的按钮我希望我的闹钟看起来像这样(目前我的编码中没有任何地方使用它;我创建它只是为了让我的目标得到视觉帮助):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:id="@+id/alarm_fl"
android:background="@mipmap/white_box">
<Button
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="9.5dp"
android:id="@+id/alarm_activation_button"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="5dp"
android:textSize="10pt"
android:id="@+id/alarm_time"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="165dp"
android:layout_marginTop="11.5dp"
android:textSize="7pt"
android:id="@+id/alarm_ampm"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="30dp"
android:textSize="5pt"
android:id="@+id/alarm_day"/>
</FrameLayout>
这就是我目前在片段中测试警报的方式:
addAlarm.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
LayoutInflater alarm_inflater = (LayoutInflater) getContext().getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup parent = (ViewGroup) getActivity().findViewById(R.id.alarm_ll);
View alarm_view = alarm_inflater.inflate(R.layout.alarm_layout, parent);
TextView alarm_time = (TextView) alarm_view.findViewById(R.id.alarm_time);
alarm_time.setText("9시 45분");
TextView alarm_ampm = (TextView) alarm_view.findViewById(R.id.alarm_ampm);
alarm_ampm.setText("오후");
TextView alarm_day = (TextView) alarm_view.findViewById(R.id.alarm_day);
alarm_day.setText("월,화,수,목,금");
Button activation_button = (Button) alarm_view.findViewById(R.id.alarm_activation_button);
activation_button.setBackgroundResource(R.mipmap.checkbox_deactivated);
}
});
其中 alarm_ll 是我要用新创建的警报填充的 LinearLayout。
在我看来,我需要为每个 Buttons 和 TextViews 设置唯一的 ID,以便分别操作它们。
下面是我的问题:
如果我想在单击按钮时以编程方式添加视图,这是正确的方法吗?或者有更好更简单的方法吗?
我的闹钟最终需要成为对象。像用户这样的非 activity class 是否有可能拥有自己的布局?
如何在通过单击按钮创建时为每个视图指定唯一 ID?
当我现在测试-运行我的应用程序时,我添加到alarm_ll的布局不会被保存,所以如果我转移到另一个activity然后回来,alarm_ll 将被重置。当它们不是原始数据类型时,如何将它们保存在片段中?
很抱歉一次问这么多问题,但我真的很想得到一些答案或建议。谢谢。
我假设您希望能够设置多个闹钟。这是
ListView
或RecyclerView
的完美用法,它允许您扩充同一视图的多个副本并显示基于某些基础数据的列表。我建议您了解如何创建 "model" 存储数据的对象。这些 "model" 对象应该与显示数据的视图对象分开。有几种常用的设计模式用于这种分离:Model-View-Controller、Model-View-Presenter 和 Model-View-ModelView。
android:id
in XML 通常用于获取 Java 代码中视图的对象。当您动态创建视图时,无论是您在问题中显示的方式还是通过膨胀 XML,您已经拥有此对象,因此我认为不需要为这些动态创建的视图分配 ID。当使用我在 #2 中建议的设计模式之一时,您还将创建一种存储数据的方法。 Android 提供了一个 API 来将信息存储在数据库中。这可以通过使用 "adapter".
轻松显示在
ListView
或 RecyclerView
中