我有一个工作正常的 Datepickerdialog 片段。当我单击按钮时,datepickerdialog 打开,左侧有微调器日期选择,右侧有日历视图。
对于我在一个地方的应用程序,我只想显示微调器日期选择部分(必须删除或隐藏日历视图部分)
在其他地方,我希望必须显示日历视图部分,并且必须隐藏或删除微调器日期选择。
但是我尝试了很多不同的东西,比如隐藏日历视图,比如
Method m = minDateSelector.getClass().getMethod("setCalendarViewShown", boolean.class);
m.invoke(dp, false);
但是当我添加这个时,我只会得到错误......
这个人也有同样的问题 Datepicker 创建的 DialopFragment 也显示日历
我的代码是
DatePickerActivity.java
import java.util.Calendar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class DatePickerActivity extends Activity {
DateDialogFragment frag;
Button button;
Calendar now;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
now = Calendar.getInstance();
button = (Button)findViewById(R.id.date_button);
button.setText(String.valueOf(now.get(Calendar.MONTH)+1)+"-"+String.valueOf(now.get(Calendar.DAY_OF_MONTH))+"-"+String.valueOf(now.get(Calendar.YEAR)));
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog();
}
});
}
public void showDialog() {
FragmentTransaction ft = getFragmentManager().beginTransaction(); //get the fragment
frag = DateDialogFragment.newInstance(this, new DateDialogFragmentListener(){
public void updateChangedDate(int year, int month, int day){
button.setText(String.valueOf(month+1)+"-"+String.valueOf(day)+"-"+String.valueOf(year));
now.set(year, month, day);
}
}, now);
frag.show(ft, "DateDialogFragment");
}
public interface DateDialogFragmentListener{
//this interface is a listener between the Date Dialog fragment and the activity to update the buttons date
public void updateChangedDate(int year, int month, int day);
}
}
DateDialogFragment.java
import java.util.Calendar;
import com.zeroe.DatePickerActivity.DateDialogFragmentListener;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.widget.DatePicker;
public class DateDialogFragment extends DialogFragment {
public static String TAG = "DateDialogFragment";
static Context mContext; //I guess hold the context that called it. Needed when making a DatePickerDialog. I guess its needed when conncting the fragment with the context
static int mYear;
static int mMonth;
static int mDay;
static DateDialogFragmentListener mListener;
public static DateDialogFragment newInstance(Context context, DateDialogFragmentListener listener, Calendar now) {
DateDialogFragment dialog = new DateDialogFragment();
mContext = context;
mListener = listener;
mYear = now.get(Calendar.YEAR);
mMonth = now.get(Calendar.MONTH);
mDay = now.get(Calendar.DAY_OF_MONTH);
/*I dont really see the purpose of the below*/
Bundle args = new Bundle();
args.putString("title", "Choose Date");
dialog.setArguments(args);//setArguments can only be called before fragment is attached to an activity, so right after the fragment is created
return dialog;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new DatePickerDialog(mContext, mDateSetListener, mYear, mMonth, mDay);
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
mListener.updateChangedDate(year, monthOfYear, dayOfMonth);
}
};
}
主要的.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/date_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="03-18-2012"
android:onClick="clickMe"/>
</LinearLayout>