通过在 dateonset () 方法中执行此操作,我以多种方式尝试了此 button.set(),尽管 editText4 包含设置按钮的有效文本,但我在那里得到了一个空指针异常。我尝试了共享首选项的路线,因为我怀疑 edittext4 是空的。最后,我尝试使用此方法从视图中初始化 Onclick 方法中的 Datebtn。在这里,我没有得到空指针异常,但是 switch case 内的 Datebtn.setText() 没有 settext 选定的日期值 & Datebtn 文本继续仅显示在 onCreateView () 方法中设置的当前日期。我想我错过了一些微不足道的东西。
public class AFragment extends Fragment implements OnClickListener {
private DataManipulator dh;
public Button Datebtn;
private Calendar mCalen;
private int day;
private int month;
private int year;
public String myEditText4;
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
View view = inflater.inflate( R.layout.afragment, container, false );
Button b1 = (Button)view.findViewById( R.id.Button01 );
b1.setOnClickListener( this );
Button Datebtn = (Button)view.findViewById( R.id.datepickerbutton );
mCalen = Calendar.getInstance();
day = mCalen.get( Calendar.DAY_OF_MONTH );
month = mCalen.get( Calendar.MONTH );
year = mCalen.get( Calendar.YEAR );
Datebtn.setText( day + " / " + (month + 1) + " / " + year );
myEditText4 = (String)Datebtn.getText();
Datebtn.setOnClickListener( this );
return view;
}
@SuppressLint( "CutPasteId" )
@Override
public void onClick( View view ) {
Context context = view.getContext();
Button Datebtn = (Button)view.findViewById( R.id.datepickerbutton );
switch( view.getId() ) {
case (R.id.datepickerbutton):
DatePickerFragment newDateFragment = new DatePickerFragment();
newDateFragment.setStyle( 1, 1 );
newDateFragment.show( getFragmentManager(), "DatePicker" );
Datebtn.setText( myEditText4 );
break;
case (R.id.Button01):
View editText1 = (EditText)getView().findViewById( R.id.ppl );
View editText2 = (EditText)getView().findViewById( R.id.litres );
View editText3 = (EditText)getView().findViewById( R.id.odo );
String myEditText1 = ((TextView)editText1).getText().toString();
String myEditText2 = ((TextView)editText2).getText().toString();
String myEditText3 = ((TextView)editText3).getText().toString();
dh = new DataManipulator( context );
if( myEditText1 != "" & myEditText2 != "" & myEditText3 != "" & myEditText4 != "" ) {
dh.insert( myEditText1, myEditText2, myEditText3, myEditText4 );
Toast.makeText( context.getApplicationContext(), "Submit Successful!!!", Toast.LENGTH_LONG ).show();
EditText text1 = (EditText)getView().findViewById( R.id.ppl );
text1.setText( "" );
EditText text2 = (EditText)getView().findViewById( R.id.litres );
text2.setText( "" );
EditText text3 = (EditText)getView().findViewById( R.id.odo );
text3.setText( "" );
} else {
Toast.makeText( context.getApplicationContext(), "Values can not be empty!!!", Toast.LENGTH_LONG ).show();
}
break;
}
}
@SuppressLint( "ValidFragment" )
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog( Bundle savedInstanceState ) {
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 );
return new DatePickerDialog( getActivity(), this, year, month, day );
}
public void onDateSet( DatePicker view, int selectedYear, int selectedMonth, int selectedDay ) {
int year = selectedYear;
int month = selectedMonth;
int day = selectedDay;
myEditText4 = (day + " / " + (month + 1) + " / " + year);
return;
}
}
}