首页 > 解决方案 > 用户选择时如何制作列表视图项目的日期,开始时间和结束时间?

问题描述

我目前正在研究讲师任命系统。所以现在我面临的一个问题是如何制作一个简单的工作日,用户选择的开始时间和结束时间,并且可以将项目添加到列表中。据我了解,工作日可以使用微调器,开始时间和结束时间可以使用时间选择器,但我被困在添加列表项上。

这是我的代码: time_box.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        >
        <Spinner
            android:id="@+id/day_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:spinnerMode="dialog"
            android:entries="@array/days"
            />
        <TextView
            android:id="@+id/start_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text="10:00"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text=" - "
            />
        <TextView
            android:id="@+id/end_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text="13:30"
            />
    </LinearLayout>
</merge>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <Button
        android:id="@+id/btnAdd"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/lblBtnAdd"
        android:layout_toRightOf="@id/txtItem"
        />
    <include layout="@layout/time_box"
        android:id="@+id/txtItem"
        />
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtItem"
        />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtItem"
        android:text="@string/txtEmpty"
        android:gravity="center_horizontal"
        />

</RelativeLayout>

MainActivity.java

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;

import static android.view.View.*;

/** Note that here we are inheriting ListActivity class instead of Activity class **/
public class MainActivity extends ListActivity {

    /**
     * Items entered by the user is stored in this ArrayList variable
     */
    ArrayList<String> list = new ArrayList<String> ();

    /**
     * Declaring an ArrayAdapter to set items to ListView
     */
    ArrayAdapter<String> adapter;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);

        /** Setting a custom layout for the list activity */
        setContentView (R.layout.activity_main);

        /** Reference to the button of the layout main.xml */
        Button btn = (Button) findViewById (R.id.btnAdd);

        /** Defining the ArrayAdapter to set items to ListView */
        adapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, list);

        /** Defining a click event listener for the button "Add" */
        OnClickListener listener = new OnClickListener () {
            @Override
            public void onClick (View v) {
                EditText edit = (EditText) findViewById (R.id.txtItem);
                list.add (edit.getText ().toString ());
                edit.setText ("");
                adapter.notifyDataSetChanged ();
            }
        };

        /** Setting the event listener for the add button */
        btn.setOnClickListener (listener);

        /** Setting the adapter to the ListView */
        setListAdapter (adapter);
    }
}

https://i.stack.imgur.com/XlB9v.png

MainActivity 我将其保留为原始的可行代码(用于将项目添加到列表的用户输入文本)。

感谢那些回答我问题的人,谢谢。

标签: javaandroiddatetimelistview

解决方案


推荐阅读