-1
我有一個TextView
,一旦我點擊它,將打開一個對話框,它使用日期和時間選擇器作爲它的視圖。Android對話框沒有顯示
datetimepicker.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</DatePicker>
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TimePicker>
<LinearLayout
android:id="@+id/setcancel"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="SET" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CANCEL" />
</LinearLayout>
</LinearLayout>
這對於TextView.OnClickListener
final TextView startTime = (TextView) getActivity().findViewById(R.id.textView3);
startTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.datetimepicker);
dialog.setTitle("Set Date and Time");
Button set = (Button) dialog.findViewById(R.id.button1);
Button cancel = (Button) dialog.findViewById(R.id.button2);
final TimePicker tp = (TimePicker)dialog.findViewById(R.id.timePicker1);
tp.setCurrentHour(Calendar.HOUR_OF_DAY);
tp.setCurrentMinute(Calendar.MINUTE);
tp.setOnTimeChangedListener(new OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
hour = hourOfDay;
min = minute;
}
});
set.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startTime.setText(Integer.toString(hour)+ ";" + Integer.toString(min));
}
});
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
}
});
該問題的代碼是,一旦我點擊它什麼也不做TextView
,我的意思是什麼也沒有發生,我想設置在對話框上點擊確定按鈕後,用戶設置的時間爲TextView
的文本。
你有什麼建議嗎? TIA。
最後一個問題時,我的設置按鈕,如何設置上按鈕TextView的文本單擊一下? – RieJack
我認爲你已經這樣做了:「startTime.setText(Integer.toString(hour)+」;「+ Integer.toString(min));」? –
謝謝菲爾..我明白了! – RieJack