findviewbyid在公共静态类android java中不起作用
我想将从用户处获得的值放入findviewbyid在公共静态类android java中不起作用,java,android,static-classes,Java,Android,Static Classes,我想将从用户处获得的值放入“OnDataSet”函数中的EditText,而findviewbyid中有一个错误,可能是因为您尚未将其键入EditText。 EditText-dob=(EditText)findViewById(R.id.dob) 可能是因为您尚未将其类型转换为EditText。 EditText-dob=(EditText)findViewById(R.id.dob) 如果您想在静态中findviewById,您应该调用rootView public static class
“OnDataSet”
函数中的EditText,而findviewbyid
中有一个错误,可能是因为您尚未将其键入EditText。
EditText-dob=(EditText)findViewById(R.id.dob) 可能是因为您尚未将其类型转换为EditText。
EditText-dob=(EditText)findViewById(R.id.dob) 如果您想在静态中
findviewById
,您应该调用rootView
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@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);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
EditText dob = findViewById(R.id.dob);
}
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
**
为了便于工作,您可以使用此库
有关更多信息:
如果您想在
static
中findviewById
,您应该调用rootView
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@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);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
EditText dob = findViewById(R.id.dob);
}
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
**
为了便于工作,您可以使用此库
有关更多信息:
这是我写的答案
rootView.findViewById
onDateSet中的更改
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@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);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
EditText dob = getActivity().findViewById(R.id.dob);
dob.setText(day+"/"+month+"/"+year);
}
}
这是我写的答案
rootView.findViewById
onDateSet中的更改
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@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);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
EditText dob = getActivity().findViewById(R.id.dob);
dob.setText(day+"/"+month+"/"+year);
}
}
欢迎光临,记住巴特霍夫是非常好的图书馆欢迎光临,记住巴特霍夫是非常好的图书馆