使用EditText自定义AlertDialog

时间:2017-11-07 10:20:14

标签: android android-layout android-fragments

我尝试开发一个小的Android应用程序并显示一个对话框警报,只有一个edittext来填充自定义 作为下一个链接

Creating a custom layout 我有这个代码,但它没有工作

public class TraceDialogFragment extends DialogFragment {


TraceDialogListener mListener;

public interface TraceDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog);
    public void onDialogNegativeClick(DialogFragment dialog);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();


    builder.setView(inflater.inflate(R.layout.activity_trace, null))
            .setIcon(R.drawable.ic_action_locate)
            .setTitle(R.string.trace_title)
            // Add action buttons
            .setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    mListener.onDialogPositiveClick(TraceDialogFragment.this);
                }
            })
            .setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    mListener.onDialogNegativeClick(TraceDialogFragment.this);
                }
            });
    return builder.create();
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    try{
        mListener = (TraceDialogListener) context;
    }catch (ClassCastException e){
        throw new ClassCastException(context.toString() + " must implement TraceDialogListener");
    }
}

}

但代码在

循环中运行
LayoutInflater inflater = getActivity().getLayoutInflater();

我不明白

在我班主要活动中

我有这段代码

class MainActivity extends AppCompatActivity implements TraceDialogFragment.TraceDialogListener

DialogFragment traceDialog = new TraceDialogFragment();
traceDialog.show(getSupportFragmentManager(),"TraceDialogFragment");

这里是我的styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

我的logcat是下一个

11-07 11:10:50.015 23570-23570/E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                            Process: , PID: 23570
                                                                                            java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                                                at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:359)

感谢您的帮助,

2 个答案:

答案 0 :(得分:0)

使用editText

尝试使用AlertDialogue
  • 只需在method的{​​{1}}中拨打此Activity or Fragment

    Oncreate or OnCreateView
  • public void inflateDialogue() { LayoutInflater layoutInflater=SelectUsers.this.getLayoutInflater(); final View view = layoutInflater.inflate(R.layout.dialog_group_name, null); AlertDialog alertDialog = new AlertDialog.Builder(this).create(); alertDialog.setTitle("Group_name"); alertDialog.setCancelable(false); edt_groupName = (EditText) view.findViewById(R.id.edt_groupName); alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, getResources().getString(R.string.ok), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { str_getTextFrom=edt_groupName.getText().toString(); //here we have to call Database firebase dialog.dismiss(); } }); alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alertDialog.setView(view); alertDialog.show(); }

    中创建dialog_group_name
    xml

答案 1 :(得分:0)

我通过更改命名空间来解决我的问题

import android.support.v7.app.AlertDialog; 至 import android.app.AlertDialog;

我不明白一切......但它有效