什么可以用作上下文的第一个参数?
What can use as the first argument for the context?
这是我创建的 TimePicker class 的代码
picker.java
package com.example.abhijeet.todo;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;
import java.util.Calendar;
/**
* Created by Abhijeet on 9/6/2017.
*/
public class Picker extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
//Creating a View object because this class do not extend from activity class so no findviewbyid() method is present
View v =View.inflate(* ? *,R.layout.activity_editor,null); //HERE
//Displaying the user selected time on the Edit Text view
EditText edittime = (EditText) v.findViewById(R.id.time_editext_field);
}
}
我正在尝试在编辑文本框中显示通过选择器选择的时间。
我无法找到论点的正确上下文。
我已经阅读了答案 here,其中解释了上下文的不同用法,但这似乎对我没有帮助。
感谢您提前的帮助。
直接打电话getContext()
作为DialogFragment is child of Fragment。所以所有的Fragmentclass方法都可以在DialogFragment中调用。
像这样
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
//Creating a View object because this class do not extend from activity class so no findviewbyid() method is present
View v =View.inflate(getContext() ,R.layout.activity_editor,null); //HERE
//Displaying the user selected time on the Edit Text view
EditText edittime = (EditText) v.findViewById(R.id.time_editext_field);
}
您也可以调用getActivity()方法。如果您定位到年龄大于 API 的 23 岁:
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
//Creating a View object because this class do not extend from activity class so no findviewbyid() method is present
View v =View.inflate(getActivity() ,R.layout.activity_editor,null); //HERE
//Displaying the user selected time on the Edit Text view
EditText edittime = (EditText) v.findViewById(R.id.time_editext_field);
}
无论何时你都被利用。 Activity
-> getApplicationContext()
, Fragment
-> getActivity()
, Adapter
-> getContext()
这是我创建的 TimePicker class 的代码
picker.java
package com.example.abhijeet.todo;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;
import java.util.Calendar;
/**
* Created by Abhijeet on 9/6/2017.
*/
public class Picker extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
//Creating a View object because this class do not extend from activity class so no findviewbyid() method is present
View v =View.inflate(* ? *,R.layout.activity_editor,null); //HERE
//Displaying the user selected time on the Edit Text view
EditText edittime = (EditText) v.findViewById(R.id.time_editext_field);
}
}
我正在尝试在编辑文本框中显示通过选择器选择的时间。 我无法找到论点的正确上下文。
我已经阅读了答案 here,其中解释了上下文的不同用法,但这似乎对我没有帮助。
感谢您提前的帮助。
直接打电话getContext()
作为DialogFragment is child of Fragment。所以所有的Fragmentclass方法都可以在DialogFragment中调用。
像这样
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
//Creating a View object because this class do not extend from activity class so no findviewbyid() method is present
View v =View.inflate(getContext() ,R.layout.activity_editor,null); //HERE
//Displaying the user selected time on the Edit Text view
EditText edittime = (EditText) v.findViewById(R.id.time_editext_field);
}
您也可以调用getActivity()方法。如果您定位到年龄大于 API 的 23 岁:
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
//Creating a View object because this class do not extend from activity class so no findviewbyid() method is present
View v =View.inflate(getActivity() ,R.layout.activity_editor,null); //HERE
//Displaying the user selected time on the Edit Text view
EditText edittime = (EditText) v.findViewById(R.id.time_editext_field);
}
无论何时你都被利用。 Activity
-> getApplicationContext()
, Fragment
-> getActivity()
, Adapter
-> getContext()