我尝试创建带有布局的自定义对话框。我尚未向对话框添加任何功能。目前,我正在尝试通过MainActivity中的FAB单击来启动它。
这是我的自定义对话框:
public class GenerateDialog extends Fragment implements DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_layout, container, false);
}
...
}
这是我的MainActivity
。我不知道如何在onClick
中启动对话框。我尝试使用一种意图,但是将对话框强制转换为活动时抛出了错误:
public class MainActivity extends AppCompatActivity {
...
GenerateDialog generateDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
generateDialog = new GenerateDialog();
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Show GenerateDialog
}
});
}
}
答案 0 :(得分:1)
做了类似的事情,您的对话框类应该扩展Dialog而不是Fragment。
应该是:public class GenerateDialog extends Dialog
然后在构造函数上给它充气:
public class GenerateDialog extends Dialog {
public GenerateDialog(@NonNull Context context) {
super(context, android.R.style.Theme_NoTitleBar_Fullscreen);
setContentView(R.layout.dialog_layout);
}
…
}
在FAB侦听器上,启动对象后,您可以调用show()(如果对象名为generateDialog,则在实例化后执行generateDialog.show();
,这是对话框所必需的)出现)。
F.i。:
public class MainActivity extends AppCompatActivity {
...
GenerateDialog generateDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
generateDialog = new GenerateDialog(this);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
generateDialog.show();
}
});
}
}
答案 1 :(得分:1)
您可以这样显示对话框
public class CustomDialog extends Activity {
private static Context context;
private static Dialog dialog;
public static void dialog(final Context ctxt, final String calledBy,
String title, String message, String buttonText) {
context = ctxt;
if( dialog != null && dialog.isShowing() ) return;
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(R.layout.custom_dialog_single_button);
TextView txtTitle = dialog.findViewById(R.id.txtTitle);
TextView txtMessage = dialog.findViewById(R.id.txtMessage);
TextView tvOK = dialog.findViewById(R.id.tvOK);
txtTitle.setText(title);
txtMessage.setText(message);
tvOK.setText(buttonText);
dialog.show();
}
并这样称呼
CustomDialogSingleButton.dialog(context,
Constants.SYNC_FILES
, context.getResources().getString(R.string.sync_files_title),
context.getResources().getString(R.string.sync_files_msg), "OK");
只需尝试此代码,您的对话框就会出现
答案 2 :(得分:1)
对话框片段的代码
public class MyCustomDialog extends DialogFragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(android.app.DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialog_view, container, false);
return v;
}
}
并从如下所示的活动中调用此MyCustomDialog,
MyCustomDialog dialog = new MyCustomDialog();
Bundle args = new Bundle();
FragmentTransaction ft = getFragmentManager().beginTransaction();
dialog.show(ft, MyCustomDialog.TAG);
答案 3 :(得分:0)
自定义视图
<!-- fragment_edit_name.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edit_name"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:orientation="vertical" >
<TextView
android:id="@+id/lbl_your_name" android:text="Your name"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<EditText
android:id="@+id/txt_your_name"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:inputType="text"
android:imeOptions="actionDone" />
</LinearLayout>
,并从对话框片段的支持版本开始定义片段本身:
import android.support.v4.app.DialogFragment;
// ...
public class EditNameDialogFragment extends DialogFragment {
private EditText mEditText;
public EditNameDialogFragment() {
// Empty constructor is required for DialogFragment
// Make sure not to add arguments to the constructor
// Use `newInstance` instead as shown below
}
public static EditNameDialogFragment newInstance(String title) {
EditNameDialogFragment frag = new EditNameDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
frag.setArguments(args);
return frag;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_edit_name, container);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Get field from view
mEditText = (EditText) view.findViewById(R.id.txt_your_name);
// Fetch arguments from bundle and set title
String title = getArguments().getString("title", "Enter Name");
getDialog().setTitle(title);
// Show soft keyboard automatically and request focus to field
mEditText.requestFocus();
getDialog().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
}
,并在扩展AppCompatActivity的活动中显示对话框:
从 FAB 的onclick方法中调用 showEditDialog()
方法
private void showEditDialog() {
FragmentManager fm = getSupportFragmentManager();
EditNameDialogFragment editNameDialogFragment = EditNameDialogFragment.newInstance("Some Title");
editNameDialogFragment.show(fm, "fragment_edit_name");
}
检查info