我开始在Andrioid中尝试时间和日期选择器。
我的课程如下:
Main.java
package com.valgriz.testapplication2;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class Main extends FragmentActivity implements OnClickListener,
OnItemSelectedListener {
Button bPickTime;
Button bPickDate;
Spinner sTime;
TimePickerFragment tpf;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bPickTime = (Button) findViewById(R.id.bPickTime);
bPickDate = (Button) findViewById(R.id.bPickDate);
sTime = (Spinner) findViewById(R.id.sTime);
bPickTime.setOnClickListener(this);
bPickDate.setOnClickListener(this);
sTime.setOnItemSelectedListener(this);
ArrayAdapter sAdapter = ArrayAdapter.createFromResource(this,
R.array.sChoices1, android.R.layout.simple_spinner_item);
sAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sTime.setAdapter(sAdapter);
}
public void onClick(View v) {
if (v.getId() == R.id.bPickTime) {
DialogFragment df = new TimePickerFragment();
df.show(getSupportFragmentManager(), "timePickerFragment");
} else if (v.getId() == R.id.bPickDate) {
DialogFragment df = new DatePickerFragment();
df.show(getSupportFragmentManager(), "datePickerFragment");
}
}
public void onItemSelected(AdapterView<?> arg0, View v, int selection,
long identification) {
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
TimePickerFragment.java
package com.valgriz.testapplication2;
import java.util.Calendar;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;
public class TimePickerFragment extends DialogFragment implements
OnTimeSetListener {
int hourOfDay;
int minute;
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
hourOfDay = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hourOfDay, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
}
如何将bPickTime的文本设置为当天的小时和分钟? 所以按钮看起来像这个“hourOfDay +”:“+ minute”
谢谢,
-Steve
答案 0 :(得分:1)
我认为按钮位于某个布局文件中,例如。 main.xml
。要访问未使用setContentView()
设置的任何布局文件的视图,您需要膨胀该特定布局,然后访问它的视图。
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.main, null);
Button button = (Button) view.findViewById(R.id.button);
现在您可以根据需要使用button
。
答案 1 :(得分:0)
在Button
中声明并初始化TimePickerFragment.java
,就像在Main.java
中一样,然后使用
bPickTime.setText(hourOfDay + ":" + minute);