Alertdialog.Builder setview:调用需要 API 级别 21
Alertdialog.Builder setview: Call requires API level 21
我试图在扩展 DialogPreference 的 class 中获得一个半径 NumberPicker 运行,但我在让 setView() 工作时遇到了很多麻烦。让我们从一些代码开始:
public class RadiusPickerPreference extends DialogPreference{
public RadiusPickerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onPrepareDialogBuilder(android.app.AlertDialog.Builder builder) {
builder.setTitle(R.string.set_radius_dialog_fragment_title);
builder.setView(R.layout.dialog_radius_picker);
builder.setPositiveButton(android.R.string.ok, null);
builder.setNegativeButton(android.R.string.cancel, null);
}
}
这让我在 builder.setView 上出现错误,说 "Call requires API 21 (current min is 15)." 我想支持 APIs 15+ 的设备,因此无法更改此选项。现在,如果我尝试覆盖
protected void onPrepareDialogBuilder(android.support.v7.app.AlertDialog.Builder builder)
而是 "Method does not override method from its superclass."
问题是,如何设置视图?不一定非要在onPrepareDialogBuilder()中,只要支持API15+即可。谢谢!
PS:如果您需要更多代码,请告诉我。要使其显示在 XML 中,只需将其添加到 <PreferenceScreen>
:
<com.example.project.RadiusPickerPreference
android:id="@+id/radPickerPref"
android:key="@string/pref_key_default_radius"
android:title="@string/pref_title_default_radius"/>
我在尝试自定义警报对话框时有过一些不太有趣的经历,建议您在确实需要详细的弹出窗口时绕过这个想法。如果您想改为尝试该路线,这里有一些对话片段代码...
public class AboutUs extends DialogFragment {
public interface DialogListener {
void onDialogFinish();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_about_us, container,
false);
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point(); display.getSize(size);
int width=size.x; int height=size.y; //change these to make your dialog the size you wish
WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
wmlp.height=height; wmlp.width=width;
getDialog().getWindow().setAttributes(wmlp);
WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();
lp.dimAmount=0.4f;
getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getDialog().setCanceledOnTouchOutside(true);
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_FRAME, android.support.v7.appcompat.R.style.Theme_AppCompat_Light);
}
public AboutUs()
{
}
}
\to call fragment from activity
AboutUs aboutUs = new AboutUs();
aboutUs.show(getSupportFragmentManager(), "Dialog Fragment");
您在这里要做的是调用 API 21 中添加的函数,而不是 API 1 中添加的函数。根据 the documentation, you want setView(View view)
instead of setView(int layoutResId)
. To get a View
from a layout, you need a LayoutInflater
. To get an instance of LayoutInflater
, you will need a context object. When you create your dialog, I would recommend storing your Context
as a variable in the class for future use. Then, in onPrepareDialogBuilder
, you can use (as per the docs):
LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE)
现在,您可以使用 inflater
从布局中获取 View
并按如下方式设置对话框的 View
:
View v = inflater.inflate(R.layout.dialog_radius_picker, null);
因此,您的代码可能如下所示:
@Override
protected void onPrepareDialogBuilder(android.app.AlertDialog.Builder builder) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
builder.setTitle(R.string.set_radius_dialog_fragment_title);
View v = inflater.inflate(R.layout.dialog_radius_picker, null);
builder.setView(v);
builder.setPositiveButton(android.R.string.ok, null);
builder.setNegativeButton(android.R.string.cancel, null);
}
希望对您有所帮助!
无需调用 setView(int resourceId)
,这需要 API21+,只需创建一个 View
对象,扩充资源并调用 setView(View view)
传递此视图。
我试图在扩展 DialogPreference 的 class 中获得一个半径 NumberPicker 运行,但我在让 setView() 工作时遇到了很多麻烦。让我们从一些代码开始:
public class RadiusPickerPreference extends DialogPreference{
public RadiusPickerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onPrepareDialogBuilder(android.app.AlertDialog.Builder builder) {
builder.setTitle(R.string.set_radius_dialog_fragment_title);
builder.setView(R.layout.dialog_radius_picker);
builder.setPositiveButton(android.R.string.ok, null);
builder.setNegativeButton(android.R.string.cancel, null);
}
}
这让我在 builder.setView 上出现错误,说 "Call requires API 21 (current min is 15)." 我想支持 APIs 15+ 的设备,因此无法更改此选项。现在,如果我尝试覆盖
protected void onPrepareDialogBuilder(android.support.v7.app.AlertDialog.Builder builder)
而是 "Method does not override method from its superclass."
问题是,如何设置视图?不一定非要在onPrepareDialogBuilder()中,只要支持API15+即可。谢谢!
PS:如果您需要更多代码,请告诉我。要使其显示在 XML 中,只需将其添加到 <PreferenceScreen>
:
<com.example.project.RadiusPickerPreference
android:id="@+id/radPickerPref"
android:key="@string/pref_key_default_radius"
android:title="@string/pref_title_default_radius"/>
我在尝试自定义警报对话框时有过一些不太有趣的经历,建议您在确实需要详细的弹出窗口时绕过这个想法。如果您想改为尝试该路线,这里有一些对话片段代码...
public class AboutUs extends DialogFragment {
public interface DialogListener {
void onDialogFinish();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_about_us, container,
false);
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point(); display.getSize(size);
int width=size.x; int height=size.y; //change these to make your dialog the size you wish
WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
wmlp.height=height; wmlp.width=width;
getDialog().getWindow().setAttributes(wmlp);
WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();
lp.dimAmount=0.4f;
getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getDialog().setCanceledOnTouchOutside(true);
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_FRAME, android.support.v7.appcompat.R.style.Theme_AppCompat_Light);
}
public AboutUs()
{
}
}
\to call fragment from activity
AboutUs aboutUs = new AboutUs();
aboutUs.show(getSupportFragmentManager(), "Dialog Fragment");
您在这里要做的是调用 API 21 中添加的函数,而不是 API 1 中添加的函数。根据 the documentation, you want setView(View view)
instead of setView(int layoutResId)
. To get a View
from a layout, you need a LayoutInflater
. To get an instance of LayoutInflater
, you will need a context object. When you create your dialog, I would recommend storing your Context
as a variable in the class for future use. Then, in onPrepareDialogBuilder
, you can use (as per the docs):
LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE)
现在,您可以使用 inflater
从布局中获取 View
并按如下方式设置对话框的 View
:
View v = inflater.inflate(R.layout.dialog_radius_picker, null);
因此,您的代码可能如下所示:
@Override
protected void onPrepareDialogBuilder(android.app.AlertDialog.Builder builder) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
builder.setTitle(R.string.set_radius_dialog_fragment_title);
View v = inflater.inflate(R.layout.dialog_radius_picker, null);
builder.setView(v);
builder.setPositiveButton(android.R.string.ok, null);
builder.setNegativeButton(android.R.string.cancel, null);
}
希望对您有所帮助!
无需调用 setView(int resourceId)
,这需要 API21+,只需创建一个 View
对象,扩充资源并调用 setView(View view)
传递此视图。