自定义对话框

时间:2013-11-11 00:01:28

标签: java android

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

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"/>

1 个答案:

答案 0 :(得分:0)

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

public class MainActivity extends FragmentActivity

其次,要显示基于the documentation的对话框,请使用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");
}