0

我想创建一个自定义对话框,其中包含文本字段。但是,我无法查看单击按钮时创建的自定义对话框。有谁知道为什么会这样?

public class MainActivity extends Activity  {private Button mButton1;   
private Button mButton2;
private Button mButton3;
private Button mButton4;
private Button mButton5;

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

    mButton1 = (Button) findViewById(R.id.button1);
    mButton2 = (Button) findViewById(R.id.button2);
    mButton3 = (Button) findViewById(R.id.button3);
    mButton4 = (Button) findViewById(R.id.button4);
    mButton5 = (Button) findViewById(R.id.button5);

    mButton1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            show();
        }
    });

    mButton2.setOnClickListener(new View.OnClickListener() {
        //This is where my button is for the dialog box
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            onCreateDialog(null);

        }
    });

}

public void show() {
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
    builder.setTitle("CHECK IT out");
    builder.setMessage("Test");
    builder.setNegativeButton("Cancel", null);
    builder.setPositiveButton("cool", null);

    AlertDialog alert = builder.create();
    alert.show();
}
//This is where I'm having trouble
public class FireTheDialogFragment extends DialogFragment {

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

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

            }
        })
        .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

            }
        });      
        return builder.create();
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

这是xml文件

<EditText
    android:id="@+id/username"
    android:inputType="textEmailAddress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="4dp"
    android:hint="@string/username" />
<EditText
    android:id="@+id/password"
    android:inputType="textPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="16dp"
    android:fontFamily="sans-serif"
    android:hint="@string/password"/>
4

1 回答 1

0

首先,由于您DialogFragment用于创建自定义对话框,因此您必须将您的扩展ActivityFragmentActivity. 这样,您的活动将支持管理片段。如果您有 Android 支持库,请在导入FragmentActivityDialogFragment. 它们有 2 个类,一个用于原始版本(仅适用于 API 11 及更高版本),另一个用于支持旧版本(用于 API 4 及更高版本,带有android.support.v4前缀)

public class MainActivity extends FragmentActivity

其次,显示对话框,根据文档,用于DialogFragment显示对话框。

/* instead of calling the dialog's onCreateDialog(), call this */
public void showNoticeDialog() {
    // Create an instance of the dialog fragment and show it
    DialogFragment dialog = new FireTheDialogFragment();

    /* use getFragmentManager() instead if not using Android support library */
    dialog.show(getSupportFragmentManager(), "DialogFragment");
}
于 2013-11-11T00:52:17.143 回答