質問

I have a class that extends LinearLayout. It contains simply two EditBox. On some button click, i want to load this class inside the alert Dialog. When i click on button, the alert dialog is displayed but the view class that extends LinearLayout is not displayed.The code is as follows. I am stucked into this. Can I get some solution??

public class StudentDialog extends LinearLayout {
    Context context;
    LinearLayout layout;

    public StudentDialog(Context context) {
        super(context);
        this.context = context;
        createStudentDialog();
    }

    private void createStudentDialog() {

        layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        layout.setPadding(10, 10, 10, 10);
        layout.setId(200);

        EditText studentName = new EditText(context);
        studentName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        studentName
                .setTextAppearance(getContext(), TEXT_DIRECTION_FIRST_STRONG);

        EditText address = new EditText(context);
        address.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        address.setTextAppearance(getContext(), TEXT_DIRECTION_FIRST_STRONG);

        layout.addView(studentName);
        layout.addView(address);

    }

}

//Now i am calling this on some button click listener as follows. The alert dialog is displayed but not the StudentDialog. 

StudentDialog dialog = new StudentDialog(this);



        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setView(dialog);

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
役に立ちましたか?

解決

It should work if you change

alertDialogBuilder.setView(dialog);

as

alertDialogBuilder.setView(dialog.layout);

他のヒント

Do you want to show two edit text in a alert view just like a login dialog with button, if yes then just create a layout which you want to show in dialog view. Create a class which extends Dialog and set this layout in setContentView of onCreate of this class

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top