为动态创建的视图提供id

时间:2017-07-15 10:15:05

标签: android android-layout android-fragments android-custom-view

我是编码的初学者,我希望得到一些帮助。我想做一个报警应用程序。在我的主页片段上,我添加了一个按钮,它将警报添加到ScrollView内的LinearLayout中。警报中将包含三个TextView,以及一个用于激活/取消激活的按钮。

以下是我希望我的闹钟看起来的样子(目前我的编码中没有使用它;我创建它只是为了获得我想要制作的视觉资料):

<?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。

在我看来,我需要为每个按钮和TextView分别操作它们的唯一ID。

现在我的问题是:

  1. 如果我想在点击按钮时以编程方式添加视图,这是正确的方法吗?或者有更好,更简单的方法吗?

  2. 我的闹钟最终需要成为对象。像User这样的非活动类,或者在这种情况下是Alarm,是否可以为它自己设置一个布局?

  3. 如何通过点击按钮创建每个视图的唯一ID?

  4. 当我现在测试运行我的应用程序时,我添加到alarm_ll中的布局将不会被保存,因此如果我转移到另一个活动并返回,则会重置alarm_ll。当它们不是原始数据类型时,如何将它们保存在片段中?

  5. 我很抱歉一次提出这么多问题,但我真的很想得到一些答案或建议。谢谢。

1 个答案:

答案 0 :(得分:0)

  1. 我假设您希望能够设置多个警报。这是对ListViewRecyclerView的完美使用,它允许您对同一视图的多个副本进行充气,并根据某些基础数据显示列表。

  2. 我建议你学习创建&#34;模型&#34;哪些是存储数据的对象。这些&#34;模型&#34;对象应与显示数据的视图对象分开。有几种设计模式通常用于这种分离:模型 - 视图 - 控制器,模型 - 视图 - 展示器和模型 - 视图 - 模型视图。

  3. XML中的
  4. android:id通常用于在Java代码中获取视图的对象。当您动态创建视图时,无论是在问题中显示的方式还是通过膨胀XML,您已经拥有此对象,因此我认为不需要为这些动态创建的视图分配ID。

  5. 使用我在#2中建议的其中一种设计模式时,您还将创建一种存储数据的方法。 Android提供了一个API,用于在数据库中存储信息。这可以通过使用&#34;适配器&#34;轻松显示在ListViewRecyclerView中。

相关问题