我正在测试新的BetterPickers库(日期和时间选择器)。在我向xml文件添加多个按钮之前,每件事都可以。通过拣选器拾取按钮文本后显示时间停止。 这是我的布局xml文件:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/lieu_depart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lieu de prise en charge"
style="" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="false"
android:layout_below="@+id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/activity_vertical_margin" >
<Button
android:id="@+id/button2_date_depart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Date" />
<Button
android:id="@+id/time_depart5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="settime" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/linearLayout2"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="@dimen/activity_vertical_margin"
android:text="Lieu de restitution" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/linearLayout3"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:orientation="horizontal" >
<Button
android:id="@+id/button5_date_arrive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="Date" />
<Button
android:id="@+id/button6_time_arrive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="Heure" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/linearLayout4"
android:orientation="horizontal" >
<Button
android:id="@+id/Button03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Button" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
这是我的代码:Reservation.java
package younga.ines.yourcar;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragment;
import com.doomonafireball.betterpickers.datepicker.DatePickerBuilder;
import com.doomonafireball.betterpickers.datepicker.DatePickerDialogFragment;
import com.doomonafireball.betterpickers.timepicker.TimePickerBuilder;
import com.doomonafireball.betterpickers.timepicker.TimePickerDialogFragment;
public class Reservation extends SherlockFragment implements OnClickListener,
DatePickerDialogFragment.DatePickerDialogHandler,
TimePickerDialogFragment.TimePickerDialogHandler {
private Button ti_depart;
private Button da_depart;
private Button ti_arrive;
private Button da_arrive;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.fragment_reservation, container, false);
//date_depart
da_depart=(Button)rootView.findViewById(R.id.button2_date_depart);
da_depart.setOnClickListener(this);
//time_depart
ti_depart = (Button) rootView.findViewById(R.id.time_depart5);
ti_depart.setOnClickListener(this);
//date_arrive
da_arrive=(Button)rootView.findViewById(R.id.button5_date_arrive);
da_arrive.setOnClickListener(this);
//time_arrive
ti_arrive=(Button)rootView.findViewById(R.id.button6_time_arrive);
ti_arrive.setOnClickListener(this);
return rootView; }
private void calltoDatedepartPicker() {
// TODO Auto-generated method stub
DatePickerBuilder dpb = new DatePickerBuilder()
.setFragmentManager(getChildFragmentManager())
.setStyleResId(R.style.BetterPickersDialogFragment)
.setTargetFragment(Reservation.this);
dpb.show();
}
private void calltoTimedepartPicker() {
// TODO Auto-generated method stub
TimePickerBuilder tpb = new TimePickerBuilder()
.setFragmentManager(getChildFragmentManager())
.setStyleResId(R.style.BetterPickersDialogFragment)
.setTargetFragment(Reservation.this);
tpb.show();
}
private void calltoTimearrivePicker() {
// TODO Auto-generated method stub
TimePickerBuilder tpb = new TimePickerBuilder()
.setFragmentManager(getChildFragmentManager())
.setStyleResId(R.style.BetterPickersDialogFragment)
.setTargetFragment(Reservation.this);
tpb.show();
}
private void calltoDatearrivePicker() {
// TODO Auto-generated method stub
DatePickerBuilder dpb = new DatePickerBuilder()
.setFragmentManager(getChildFragmentManager())
.setStyleResId(R.style.BetterPickersDialogFragment)
.setTargetFragment(Reservation.this);
dpb.show();
}
@Override
public void onDialogTimeSet(int reference, int hourOfDay, int minute) {
// TODO Auto-generated method stub
// reference argument show -1 as value
Toast.makeText(getSherlockActivity(), ""+reference, Toast.LENGTH_SHORT).show();
if(reference==R.id.time_depart5) ti_depart.setText("" + hourOfDay + ":" + minute);
if(reference==R.id.button6_time_arrive) ti_arrive.setText("" + hourOfDay + ":" + minute);
}
@Override
public void onDialogDateSet(int reference, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
if(reference==R.id.button2_date_depart) da_depart.setText("" + dayOfMonth + "" + monthOfYear + "" + year);
if(reference==R.id.button5_date_arrive) da_arrive.setText("" + dayOfMonth + "" + monthOfYear + "" + year);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.button2_date_depart){
calltoDatedepartPicker();
}
if(v.getId()==R.id.time_depart5){
calltoTimedepartPicker();
}
if(v.getId()==R.id.button5_date_arrive){
calltoDatearrivePicker();
}
if(v.getId()==R.id.button6_time_arrive){
calltoTimearrivePicker();
}
}
}
所以我的问题是我如何使用 onDialogDataSet和onDialogTimeSet方法来显示在 4个按钮上选择的时间或日期?
PS:当我只使用一个按钮时效果很好!! 提前谢谢。
答案 0 :(得分:0)
@Override public void onDialogTimeSet ( int reference, int hourOfDay, int minute){
if (flag_time_depart == true) {
ti_depart.setText("" + hourOfDay + ":" + minute);
flag_time_depart = false;
}
if (flag_time_arrive == true) {
ti_arrive.setText("" + hourOfDay + ":" + minute);
flag_time_arrive = false;
}
}
and without forgetting the Flags declaration as globals vars:
private boolean flag_time_depart = false;
private boolean flag_time_arrive = false;
DatePickerBuilder dpb = new DatePickerBuilder()
.setFragmentManager(getChildFragmentManager())
.setStyleResId(R.style.BetterPickersDialogFragment_Light)
.setTargetFragment(Reserva.this);
dpb.show();