【问题标题】:Creating Custom Dialog box (android example error?)创建自定义对话框(android 示例错误?)
【发布时间】:2013-12-16 00:07:05
【问题描述】:

一直在尝试创建客户对话框,我从 android 网站复制了源代码。我已经完成了一些事情,现在我真的被困住了。我不知道代码中的LoginFragment是什么:

...
public class start extends Activity{
Button buttonx;
final Context context = this;
LayoutInflater mInflater;

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);
}

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

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(mInflater.inflate(R.layout.login, null))
            // Add action buttons
            .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // sign in the user ...
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    LoginDialogFragment.this.getDialog().cancel();
                }
            });
    return builder.create();
}

当我//将行注释掉时,它说@Override 是不必要的,这不能解决任何问题。 对不起菜鸟问题

【问题讨论】:

  • LoginDialogFragment 必须是示例中使用的类名。你可以用你的活动名称来改变它。

标签: android android-fragments customdialog


【解决方案1】:

看看这篇关于android开发者的帖子,对我有用很多次。

http://developer.android.com/guide/topics/ui/dialogs.html

这里还有一个简单的例子:

http://www.mkyong.com/android/android-custom-dialog-example/

【讨论】:

    【解决方案2】:

    我猜你可能已经知道LoginFragment 是一个片段,所以你必须创建一个新类并用Fragment 类扩展它。你可以在那里输入你的代码onCreateDialog()。然后按照如何显示它的步骤进行操作。

    您将使用此代码来显示它-

    DialogFragment newFragment = new LoginFragment();
    newFragment.show(getSupportFragmentManager(), "login");
    

    参考link rochasdv 分享的内容,你会有更好的想法。问我是否遗漏了什么。

    【讨论】:

      【解决方案3】:

      为要在对话框中显示的视图创建布局文件。 假设你想要一个文本视图和一个对话框中的按钮:

      dialog_box.xml

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
      
      <TextView
          android:id="@+id/textview"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:textSize="17sp"
          android:layout_margin="20dp"/>
      
      <Button
          android:id="@+id/button"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:textSize="17sp"
          android:layout_margin="20dp"/>
      

      在你的 java 文件中:

          Dialog d = new Dialog(this);
          d.setTitle("No Places found");
          d.setContentView(R.layout.dialog_box);
          d.getWindow().getAttributes().width = WindowManager.LayoutParams.FILL_PARENT;
          d.show();
      

      祝你好运

      【讨论】:

        猜你喜欢
        • 2011-05-28
        • 2011-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-19
        • 1970-01-01
        • 1970-01-01
        • 2019-11-15
        相关资源
        最近更新 更多