如何制作一个对话框显示隐形元素android

时间:2014-07-21 21:53:17

标签: android android-dialog android-dialogfragment

我有一个对话框,我放了一些元素(EditText):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_add_electeur"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/e_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="17dp"
    android:ems="10"
    android:hint="@string/nom"
    android:inputType="textPersonName" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/lastname"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="26dp"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:ems="10"
    android:hint="@string/prenom"
    android:inputType="textPersonName" />

<EditText
    android:id="@+id/cin_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="37dp"
    android:ems="10"
    android:hint="@string/cin"
    android:inputType="textPersonName" />

<EditText
    android:id="@+id/tel_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="36dp"
    android:ems="10"
    android:hint="@string/tel"
    android:inputType="textPersonName" />

<EditText
    android:id="@+id/email_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="44dp"
    android:ems="10"
    android:hint="@string/email"
    android:inputType="textEmailAddress" />

<EditText
    android:id="@+id/adresse"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:ems="10"
    android:hint="@string/adresse"
    android:inputType="textPersonName" />

<EditText
    android:id="@+id/id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:visibility="invisible" />

<EditText
    android:id="@+id/bureau"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Bureau"
    android:inputType="textPersonName" />

这是我的对话框:

public class AddElecteur extends DialogFragment {
private AlertDialog.Builder builder;
public static String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
public static String CIN_PATTERN="^(\\d+)$";

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().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.add_electeur, null))
            // Add action buttons
            .setPositiveButton(R.string.add_electeur_btn,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {

                        }
                    })
            .setNegativeButton(R.string.annuler_btn,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            AddElecteur.this.getDialog().cancel();
                        }
                    });
    return builder.create();

}

@Override
public void onStart() {
    super.onStart(); // super.onStart() is where dialog.show() is actually
                        // called on the underlying dialog, so we have to do
                        // it after this point
    AlertDialog d = (AlertDialog) getDialog();
    if (d != null) {
        Button positiveButton = (Button) d
                .getButton(Dialog.BUTTON_POSITIVE);
        positiveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Boolean wantToCloseDialog = false;
                AddElecteur e = AddElecteur.this;
                Dialog d = e.getDialog();
                EditText nomText = (EditText) d.findViewById(R.id.e_name);
                EditText prenomText = (EditText) d
                        .findViewById(R.id.lastname);
                String nom = nomText.getText().toString();
                String prenom = prenomText.getText().toString();
                EditText cinText = (EditText) d.findViewById(R.id.cin_text);
                String cin=cinText.getText().toString();
                if (nom.isEmpty()) {
                    Toast toast = Toast.makeText(d.getContext(),
                            "Vérifier le Nom", Toast.LENGTH_SHORT);
                    int[] location = new int[2];
                    nomText.getLocationOnScreen(location);
                    toast.setGravity(Gravity.TOP, location[0], location[1]);
                    toast.show();
                    nomText.setFocusableInTouchMode(true);
                    nomText.requestFocus();
                } else if (prenom.isEmpty()) {
                    Toast toast = Toast.makeText(d.getContext(),
                            "Vérifier le Prenom", Toast.LENGTH_SHORT);
                    int[] location = new int[2];
                    prenomText.getLocationOnScreen(location);
                    toast.setGravity(Gravity.TOP, location[0], location[1]);
                    toast.show();
                    prenomText.setFocusableInTouchMode(true);
                    prenomText.requestFocus();

                } else if(!cin.matches(CIN_PATTERN)){
                    Toast toast = Toast.makeText(d.getContext(),
                            "Vérifier le CIN", Toast.LENGTH_SHORT);
                    int[] location = new int[2];
                    cinText.getLocationOnScreen(location);
                    toast.setGravity(Gravity.TOP, location[0], location[1]);
                    toast.show();
                    cinText.setFocusableInTouchMode(true);
                    cinText.requestFocus();
                }


                if (wantToCloseDialog)
                    dismiss();
                // else dialog stays open. Make sure you have an obvious way
                // to close the dialog especially if you set cancellable to
                // false.
            }
        });
    }
}

}

在这种情况下,我的对话框显示了6个第一个元素,其余的它们是不可见的,如下所示: enter image description here 现在我想留下两个按钮&#34; Annuler&#34;和&#34; Enregistrer&#34;修复,但包含所有表单的正文将滚动:)谢谢。

2 个答案:

答案 0 :(得分:2)

将您的LinearLayout包装在ScrollView中。请记住,ScrollViews只能包含一个元素,因此您仍然需要使用LinearLayout。

答案 1 :(得分:1)

使用以下代码环绕编辑视图。

<ScrollView 
        android:layout_width="match_parent"
        android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

// your editviews

</LinearLayout>
</ScrollView>