如何在 android 中以全视图的线性布局显示两个时间选择器


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_gradient"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <TextView
        android:id="@+id/tile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="34dp"
        android:layout_marginTop="15dp"
        android:text="Class" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/tile"
        android:layout_alignParentRight="true"
        android:layout_marginRight="83dp"
        android:text="Course" />
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tile"
        android:layout_below="@+id/tile"
        android:layout_marginLeft="14dp"
        android:layout_marginTop="14dp"
        android:orientation="horizontal" >
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="From time" />
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="22dp"
            android:text="To time" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout1"
        android:layout_marginTop="19dp"
        android:layout_toLeftOf="@+id/textView1"
        android:orientation="horizontal" >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >
            <TimePicker
                android:id="@+id/timePicker1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleX="0.5"
                android:scaleY="0.5" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >
            <TimePicker
                android:id="@+id/timePicker2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleX="0.5"
                android:scaleY="0.5" />
        </LinearLayout>
    </LinearLayout>
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/linearLayout2" >
    </ListView>
    <TextView
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/listView1"
        android:text="Submit"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
给定 XML:我正在准备布局,但在线性布局中,我显示 2 个线性布局,内部显示时间选择器,但在这种情况下,它只显示时间选择

器和时间选择器的小时,而我必须显示小时,分钟上午和下午,但我无法做到这一点,请帮助如何修复它

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == R.id.imageView1) {
            Calendar mcurrentTime = Calendar.getInstance();
            int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
            int minute = mcurrentTime.get(Calendar.MINUTE);
            TimePickerDialog mTimePicker;
            mTimePicker = new TimePickerDialog(MainActivity.this,
                    new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker timePicker,
                                int selectedHour, int selectedMinute) {
                            Toast.makeText(MainActivity.this,
                                    selectedHour + ":" + selectedHour, 1000)
                                    .show();
                        }
                    }, hour, minute, true);// Yes 24 hour time
            mTimePicker.setTitle("Select Time");
            mTimePicker.show();
        }
    }

像这样使用,它将正常工作,并且很容易.

您可以使用它将其设置为 12h 格式

timePicker = (TimePicker) findViewById(R.id.timePicker1);
timePicker.setIs24HourView(false);
timePicker.setCurrentHour(Calendar.get(Calendar.HOUR_OF_DAY));

最新更新