Android时间选择器未在点击时初始化

时间:2015-03-06 17:45:23

标签: android timepicker

我正在尝试制作一个允许用户安排约会的应用。我是一个相当陌生的Android,我正在通过Android Boot Camp预订。我可以使用日期选择器,但是当我点击时间选择器的按钮时,没有任何反应。我一直盯着这个,这句话开始模糊起来,所以我想我会寻求帮助。

这是我的.xml代码

<LinearLayout 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:gravity="center_horizontal"
android:padding="0dp"
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="com.example.massagescheduler.MainActivity" >

<ImageView
    android:id="@+id/imgMass"
    android:layout_width="246dp"
    android:layout_height="match_parent"
    android:contentDescription="@string/imgMass"
    android:src="@drawable/vmassage" />

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TableRow>

        <View android:layout_height="40dp" />

        <TextView
            android:id="@+id/txtTitle"
            android:layout_width="wrap_content"
            android:background="#B0C4DE"
            android:gravity="fill_horizontal"
            android:text="@string/txtTitle"
            android:textSize="30sp"
            android:typeface="serif" />
    </TableRow>

    <TableRow>

        <View android:layout_height="50dp" />

        <TextView
            android:id="@+id/txtDescription"
            android:layout_width="wrap_content"
            android:layout_gravity="left"
            android:padding="20dp"
            android:text="@string/txtDescription"
            android:textSize="20sp" />
    </TableRow>

    <TableRow>

        <View android:layout_height="25dp" />

        <Button
            android:id="@+id/btnDate"
            android:layout_width="wrap_content"
            android:gravity="fill_horizontal"
            android:padding="20dp"
            android:text="@string/btnDate"
            android:textSize="20sp" />
    </TableRow>

    <TableRow>

        <View android:layout_height="25dp" />

        <Button
            android:id="@+id/btnTime"
            android:layout_width="wrap_content"
            android:gravity="fill_horizontal"
            android:padding="20dp"
            android:text="@string/btnTime"
            android:textSize="20sp" />
    </TableRow>

    <TableRow>

        <View android:layout_height="50dp" />

        <TextView
            android:id="@+id/txtReservation"
            android:layout_gravity="center_vertical"
            android:padding="20dp"
            android:textSize="25sp" />
    </TableRow>
</TableLayout>

这是我的Java代码:

package com.example.massagescheduler;
import java.text.DateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends Activity {
private TextView reservation;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    reservation = (TextView) findViewById(R.id.txtReservation);
    Button btDate = (Button) findViewById(R.id.btnDate);
    Button btTime = (Button) findViewById(R.id.btnTime);
    btDate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new DatePickerDialog(MainActivity.this, d,
                    c.get(Calendar.YEAR), c.get(Calendar.MONTH), c
                            .get(Calendar.DAY_OF_MONTH)).show();

        }
    });

    btTime.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new TimePickerDialog(MainActivity.this, t, 
                    c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE),
                    false);

        }
    });
}

Calendar c = Calendar.getInstance();
DateFormat fmtDate = DateFormat.getDateInstance();
DateFormat fmtTime = DateFormat.getTimeInstance();
DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, monthOfYear);
        c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        reservation.setText("Your reservation is set for "
                + fmtDate.format(c.getTime()));

    }

};

TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int hour, int minute) {
        // TODO Auto-generated method stub
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        reservation.setText("Your reservation is set for "
                + fmtTime.format(c.getTime()));

    }

};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您忘了拨打TimePickerDialog#show()

new TimePickerDialog(MainActivity.this, t, 
    c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE),
          false).show(); // MISSING!