我已经成功地为我的 Android 应用程序创建了一个可用的 AlertDialog:
public class MyClass extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
ArrayList selectedItems = new ArrayList();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_toppings)
builder.setMultiChoiceItems(R.array.my_array, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
selectedItems.add(which);
} else if (selectedItems.contains(which)) {
selectedItems.remove(Integer.valueOf(which));
}
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// do stuff here ...
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// do stuff here ...
}
});
return builder.create();
}
}
此 MultiChoiceItems 列表由 /res/values/array.xml
中的数组支持
<resources>
<array name="my_array">
<item>item 01</item>
<item>item 02</item>
<item>item 03</item>
<item>item 04</item>
<item>item 05</item>
</array>
</resources>
在我的 Activity 中,我这样调用 AlertDialog:
MyClass myClass = new MyClass();
myClass.show(getSupportFragmentManager(), "My Dialog");
我现在想做的是使用带有 AlertDialog 的自定义布局,这样我就可以执行诸如交替行阴影、自定义按钮之类的操作,并添加一个 EditText,这样我就可以有一个具有填充功能的“其他”选项在“其他”中。
谷歌搜索后,我似乎需要创建一个新布局,并将 AlertDialog 的 View 设置为此布局。所以,我创建了一个布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="other"
android:textSize="18sp"/>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
然后我将其添加到我的 DialogFragment 类中:
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
View view = layoutInflater.inflate(R.layout.my_new_layout, null);
然后
builder.setView(view);
如您所料,这没有用。新的 CheckBox 和 EditText 插入在我的数组中填充的其他复选框之后,但它看起来很糟糕,而且我似乎无法控制从数组创建的复选框的外观。
就像我说的,我希望能够添加这个新的 CheckBox/EditText 组合,以及能够自定义整个 AlertDialog 的外观。
我真的很想使用 /res/values/array.xml
中的数组,这样如果我想向列表中添加新项目,就不必硬编码新选项。
我想做的事可行吗?如果是这样,一些建议会很好。
谢谢
这就是我希望我的 AlertDialog 看起来/表现的样子:
最佳答案
好吧,我终于自己弄明白了。这是我的决定:
新布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right">
</LinearLayout>
新类:
public class MyClass extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String[] theOptions = getResources().getStringArray(R.array.options);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_toppings)
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
LinearLayout view = (LinearLayout) layoutInflater.inflate(R.layout.my_layout, null);
for(String option : theOptions){
CheckBox checkbox = new CheckBox(getContext());
checkbox.setText(option);
view.addView(checkbox);
}
LinearLayout otherLinearLayout = new LinearLayout(getContext());
otherLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
otherLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
otherLinearLayout.setId(R.id.otherLinearLayout);
CheckBox otherCheckBox = new CheckBox(getContext());
otherCheckBox.setText("other");
otherCheckBox.setId(R.id.otherCheckBox);
EditText otherEditText = new EditText(getContext());
otherEditText.setId(R.id.otherEditText);
otherLinearLayout.addView(otherCheckBox);
otherLinearLayout.addView(otherEditText);
view.addView(otherLinearLayout);
builder.setView(view);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// do stuff here ...
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// do stuff here ...
}
});
return builder.create();
}
}
关于android - 需要帮助获得自定义布局以使用我的 Android AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40266053/