簡體   English   中英

自定義對話框

[英]Custom dialog box

我想創建一個自定義對話框,其中包含文本字段。 但是,我在單擊按鈕時無法查看自定義對話框。 有人知道為什么會這樣嗎?

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

首先,由於要使用DialogFragment創建自定義對話框,因此必須將Activity擴展為FragmentActivity 這樣,您的活動將具有管理片段的支持。 如果您具有Android支持庫,則在導入FragmentActivityDialogFragment時要小心。 它們有2個類,一個用於原始類(僅適用於API 11及更高版本),另一個用於支持較舊版本(用於android.support.v4前綴的API 4及更高版本)

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");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM