我想创建一个具有标题的DialogFragment,然后是一个消息“Select Wifi”,后跟一个微调器。在此之下,我想要另一条消息“输入昵称”,然后是下面的编辑文本字段。然后是两个按钮,它们将返回微调器和编辑文本的字符串。到目前为止我有这个代码,但我无法弄清楚为什么它不起作用......
public static class MyAlertDialogFragment extends DialogFragment {
public static MyAlertDialogFragment newInstance(String title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
final EditText input = new EditText(getActivity());
final Spinner sp = new Spinner(getActivity());
return new AlertDialog.Builder(getActivity())
.setTitle(title)
.setMessage("Select Wifi")
.setView(sp)
.setMessage("Enter nickname")
.setView(input)
.setPositiveButton("Save",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((MainActivity)getActivity()).doPositiveClick();
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((MainActivity)getActivity()).doNegativeClick();
}
}
)
.create();
}
}
它仅显示编辑文本文件和编辑文本的消息。那么我该如何添加一个微调器呢?或者是否有更好的方法来实现一个dialogFragment,它可以显示一个微调器和一个包含多个消息的editText?甚至可能在微调器中使用提示并编辑文本而不是消息?还有一种方法可以在我的主类中填充微调器,然后在我的对话框中使用该微调器吗?谢谢你的帮助。
答案 0 :(得分:2)
这里有一个非常简单的自定义对话框片段示例。此代码来自我的某个应用:
public class ClaimDialogFragment extends DialogFragment {
Button buttonClaim;
EditText editTextClaim;
String profileId;
String status;
public static ClaimDialogFragment newInstance(Bundle bundle) {
ClaimDialogFragment myFragment = new ClaimDialogFragment();
myFragment.setArguments(bundle);
return myFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_dialog_claim, container,
false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawableResource(
android.R.color.transparent);
editTextClaim = (EditText) view.findViewById(R.id.editTextClaim);
buttonClaim = (Button) view.findViewById(R.id.buttonClaim);
Log.d("point a", editTextClaim.getText().toString() + "");
return view;
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
if (getArguments() != null) {
profileId = getArguments().getString("profile_id");
}
buttonClaim.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),
"Please write your claim letter.",
Toast.LENGTH_LONG).show();
}
});
}
}
相关的xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/dialog_background"
android:orientation="vertical"
android:padding="8dp" >
<EditText
android:id="@+id/editTextClaim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:background="@drawable/edittext_border_background"
android:ems="10"
android:gravity="top"
android:hint="Cover letter for the claim"
android:inputType="textMultiLine"
android:minLines="5"
android:padding="4dp" >
<requestFocus />
</EditText>
<Button
android:id="@+id/buttonClaim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/button_black"
android:paddingTop="8dp"
android:text="/Claim"
android:textColor="@color/white"
android:textStyle="bold" />
这就是我称之为对话框片段的方式:
FragmentTransaction ft = getActivity()
.getSupportFragmentManager().beginTransaction();
DialogFragment fragment = new ClaimDialogFragment();
fragment.show(ft, null);
另请注意,您可以对片段使用newInstance()静态方法。 这是我的应用程序中此代码的快照:
答案 1 :(得分:2)
您可以使用setView()
方法为AlertDialog的内容设置自定义布局。
所以只需制作一个适合您需求的布局。在这种情况下,包含您的视图的LinearLayout(消息,EditText和Spinner)。我们假设您将其命名为my_dialog.xml
,然后您可以像这样使用它:
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
LinearLayout root = (LinearLayout) inflater.inflate(R.layout.my_dialog, null);
// get your Views with findViewById, and initialize them
//
// Spinner sp = (Spinner) root.findViewById()...
// ...
// ...
return new AlertDialog.Builder(getActivity())
.setTitle(title)
.setView(root)
.setPositiveButton("Save",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// you can access you Views here
// and gather the info that you need
// you can modify doPositiveClick to
// accept that info as parameters
// so the parent Activity can get it
((MainActivity)getActivity()).doPositiveClick(param1, param2);
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((MainActivity)getActivity()).doNegativeClick();
}
}
)
.create();
使用自定义视图,您仍然可以使用AlertDialog的内置功能(例如标题,正/负按钮等)。