如何在片段中添加日期选择器。?
我在SO上找到了一些答案,展示了如何使用DialogFragment来设置DatePicker。但是当我将getSupportFragmentManager()添加到我的fragmnet时,它会给出错误。
请帮帮我......
public class Fragment_addStudentDetails extends Fragment implements View.OnClickListener {
EditText fName,mName, Address, sName, hNo, mNo, presentSpo;
Button save;
DatabaseAdapter databaseHelper;
private ImageButton ib;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_add_student_details,container,false);
fName = (EditText) view.findViewById(R.id.enterFname);
mName = (EditText) view.findViewById(R.id.enterMname);
sName = (EditText) view.findViewById(R.id.enterSureName);
Address =(EditText) view.findViewById(R.id.enterAddress);
hNo = (EditText) view.findViewById(R.id.entertph);
mNo = (EditText) view.findViewById(R.id.entertpm);
presentSpo = (EditText) view.findViewById(R.id.enterpres_sport);
save = (Button) view.findViewById(R.id.btnsavefrag);
save.setOnClickListener(this);
databaseHelper = new DatabaseAdapter(getActivity());
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnsavefrag:
addStudent(v);
break;
case 1:
break;
}
}
public void showDatePickerDialog(View v) {
new DatePickerFragment((EditText) v).show(getSupportFragmentManager(), "datePicker");
}
public void addStudent(View view){
String fname = fName.getText().toString();
String mname = mName.getText().toString();
String sname = sName.getText().toString();
String address = Address.getText().toString();
String TPhome = hNo.getText().toString();
String TPmobile = mNo.getText().toString();
String presentsport = presentSpo.getText().toString();
long id= databaseHelper.insertData(fname, mname, sname, address, TPhome, TPmobile, presentsport);
if(id<0){
Message.message(getActivity(),"Error inInsertiong Data");
}else
{
Message.message(getActivity(),"Succecsfuly inserted to data");
}
}
public void viewDetails(View view){
String data = databaseHelper.getAllData();
Message.message(getActivity(),data);
}
}
我的日期选择器Fragmnet Class
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
public EditText activity_edittext;
public DatePickerFragment(EditText edit_text) {
activity_edittext = edit_text;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
activity_edittext.setText(String.valueOf(month + 1 ) + "/" + String.valueOf(day) + "/" + String.valueOf(year));
}
}
活动类
public class Group_Add_Details_menu extends Activity{
Fragment fragment;
Button btnFragment1, btnFragment2, btnFragment3, btnFragment4;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_detal_menu);
btnFragment1 = (Button)findViewById(R.id.btn1);
btnFragment2 = (Button)findViewById(R.id.btn2);
btnFragment3 = (Button)findViewById(R.id.btn3);
btnFragment4 = (Button)findViewById(R.id.btn4);
btnFragment1.setBackgroundResource(R.drawable.add_sbutton_pressed);
btnFragment2.setBackgroundResource(R.drawable.add_tbutton_pressed);
btnFragment3.setBackgroundResource(R.drawable.add_pbutton_pressed);
btnFragment4.setBackgroundResource(R.drawable.view_button_pressed);
// get an instance of FragmentTransaction from your Activity
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//add a fragment
Fragment_addStudentDetails myFragment = new Fragment_addStudentDetails();
fragmentTransaction.add(R.id.myfragment, myFragment);
fragmentTransaction.commit();
btnFragment1.setOnClickListener(btnFragmentOnClickListener);
btnFragment2.setOnClickListener(btnFragmentOnClickListener);
btnFragment3.setOnClickListener(btnFragmentOnClickListener);
btnFragment4.setOnClickListener(btnFragmentOnClickListener);
}
Button.OnClickListener btnFragmentOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View v) {
Fragment newFragment;
// Create new fragment
if(v == btnFragment1){
newFragment = new Fragment_addStudentDetails();
}else{
newFragment = new Group_Add_test();
}
// Create new transaction
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.myfragment, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}};
}
答案 0 :(得分:2)
cannot resolve method 'getSupportFragmentManager()'
这意味着你使用的是另一种类型的片段,它是android的default Fragment
而不是支持库中的support fragment
。
<强>溶液强>
确保您的导入如下所示:
import android.support.v4.app.Fragment;
修改强>
由于您使用Activity
这是默认的Android活动,而不是您需要使用getFragmentManager()
代替getSupportFragmentManager()