开发者

EditText inside a alertDialogue is not showing softkeyboard in Android

I have a edit text inside an alert Dialog for some user input. But when I click on the edit text it does not show the soft keyboard.

Whereas if the same edit text is placed inside a dialog it show the keyboard when the edit text is focused.

public class LaunchingActivity extends Activity {

    Button addSubList = null;
    TableLayout tabLayToDoList;
    int textViewId = StaticContents.textViewId;
    int edtTxtViewId = StaticContents.edtTxtViewId;
    int ImgViewId = StaticContents.ImgViewId;
    int relLayViewId = StaticContents.relLayViewId;
    int count = StaticContents.count;
    CustomDialog share;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        System.out.println(textViewId + " " + edtTxtViewId + " " + ImgViewId
                           + " " + relLayViewId);
        addSubList = (Button) findViewById(R.id.btnAddSubItem);
        tabLayToDoList = (TableLayout) findViewById(R.id.tabLayToDoList);

    }

    public void showSublistDialog() {
        Dialog subList = new Dialog(this);
        Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
            .getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();
        System.out.println("Display Width and Height :" + "width " + width
                           + " height " + height);
        subList.show();

        // rel.setLayoutParams(layoutPrams);
    }
    @Override
    protected void onPostResume() {
 开发者_JAVA技巧       addSubList.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    count = 0;
                    share = new CustomDialog(LaunchingActivity.this);
                    share.show();

                }
            });

        super.onPostResume();
    }
    private class CustomDialog extends Dialog {

        public CustomDialog(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }

        RelativeLayout relLayParent;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.addsubitem);

            Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();
            int width = display.getWidth();
            int height = display.getHeight();

            LayoutParams params = getWindow().getAttributes();
            if(height>width){
                //potrait mode
                params.height = height - 150;
                params.width = width-60;
            }else{
                params.height = height - 100;
                params.width = width-100;   
            }

            getWindow().setAttributes(
                                      (android.view.WindowManager.LayoutParams) params);

            relLayParent = (RelativeLayout) findViewById(R.id.relLayParent);
            addDesScription(relLayParent, R.id.relLaySubListContent);
            addDesScription(relLayParent, R.id.relLaySubListContent);

        }

        public void addDesScription(RelativeLayout relParent, int relLayoutId) {
            TextView txtDescription = new TextView(LaunchingActivity.this);
            EditText edtDescription = new EditText(LaunchingActivity.this);
            ImageView imgaddDescription = new ImageView(LaunchingActivity.this);
            edtDescription.setInputType(262144);
            RelativeLayout relLay = new RelativeLayout(LaunchingActivity.this);

            textViewParams(txtDescription);
            edttxtDescriptionParams(edtDescription);
            imageViewParams(imgaddDescription);

            relLaySubListDescription(relLay, relLayoutId);
            relLay.addView(txtDescription);
            relLay.addView(edtDescription);

            relParent.addView(relLay);
            if (count != 0) {
                relLay.addView(imgaddDescription);
            } else {
                relLay.setVisibility(View.GONE);
            }
            incrementIds();

        }

        public void textViewParams(TextView txtDescription) {
            txtDescription.setText("Description:");
            txtDescription.setTextColor(getResources().getColor(R.color.Blue));
            txtDescription.setId(textViewId);
            txtDescription.setMinLines(1);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                                                                                 android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
                                                                                 android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            txtDescription.setLayoutParams(params);
        }

        public void imageViewParams(ImageView addSubListDescription) {
            addSubListDescription
                .setImageResource(R.drawable.add_description_icon);
            addSubListDescription.setId(ImgViewId);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                                                                                 android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
                                                                                 android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            addSubListDescription.setOnTouchListener(new OnTouchListener() {

                    public boolean onTouch(View arg0, MotionEvent arg1) {

                        addDesScription(relLayParent, relLayViewId - 1);

                        return false;
                    }
                });
            addSubListDescription.setLayoutParams(params);
        }

        public void edttxtDescriptionParams(EditText edtDescription) {


            edtDescription.setId(edtTxtViewId);
            edtDescription.setMinLines(1);
            edtDescription.setHint("Description");
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                                                                                 android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
                                                                                 android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.LEFT_OF, ImgViewId);
            params.addRule(RelativeLayout.RIGHT_OF, textViewId);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            edtDescription.setLayoutParams(params);
        }

        public void incrementIds() {
            ImgViewId++;
            textViewId++;
            edtTxtViewId++;
            relLayViewId++;
            count++;

        }

        public void relLaySubListDescription(
                                             RelativeLayout relLaySubListDescription, int relLayoutId) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                                                                                 android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
                                                                                 android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);

            params.width = android.widget.RelativeLayout.LayoutParams.FILL_PARENT;
            relLaySubListDescription.setId(relLayViewId);
            params.addRule(RelativeLayout.BELOW, relLayoutId);
            relLaySubListDescription.setLayoutParams(params);
        }
    }
}


public class LaunchingActivity extends Activity {

    Button addSubList = null;
    TableLayout tabLayToDoList;
    int textViewId = StaticContents.textViewId;
    int edtTxtViewId = StaticContents.edtTxtViewId;
    int ImgViewId = StaticContents.ImgViewId;
    int relLayViewId = StaticContents.relLayViewId;
    int count = StaticContents.count;
    CustomDialog share;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        System.out.println(textViewId + " " + edtTxtViewId + " " + ImgViewId
                + " " + relLayViewId);
        addSubList = (Button) findViewById(R.id.btnAddSubItem);
        tabLayToDoList = (TableLayout) findViewById(R.id.tabLayToDoList);

    }

    public void showSublistDialog() {
        Dialog subList = new Dialog(this);
        Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();
        System.out.println("Display Width and Height :" + "width " + width
                + " height " + height);
        subList.show();

        // rel.setLayoutParams(layoutPrams);
    }
@Override
protected void onPostResume() {
        addSubList.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            count = 0;
            share = new CustomDialog(LaunchingActivity.this);
            share.show();

        }
    });

    super.onPostResume();
}
    private class CustomDialog extends Dialog {

        public CustomDialog(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }

        RelativeLayout relLayParent;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.addsubitem);

            Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
                    .getDefaultDisplay();
            int width = display.getWidth();
            int height = display.getHeight();

            LayoutParams params = getWindow().getAttributes();
            if(height>width){
            //potrait mode
                params.height = height - 150;
                params.width = width-60;
            }else{
                params.height = height - 100;
                params.width = width-100;   
            }

            getWindow().setAttributes(
                    (android.view.WindowManager.LayoutParams) params);

            relLayParent = (RelativeLayout) findViewById(R.id.relLayParent);
            addDesScription(relLayParent, R.id.relLaySubListContent);
            addDesScription(relLayParent, R.id.relLaySubListContent);

        }

        public void addDesScription(RelativeLayout relParent, int relLayoutId) {
            TextView txtDescription = new TextView(LaunchingActivity.this);
            EditText edtDescription = new EditText(LaunchingActivity.this);
            ImageView imgaddDescription = new ImageView(LaunchingActivity.this);
            edtDescription.setInputType(262144);
            RelativeLayout relLay = new RelativeLayout(LaunchingActivity.this);

            textViewParams(txtDescription);
            edttxtDescriptionParams(edtDescription);
            imageViewParams(imgaddDescription);

            relLaySubListDescription(relLay, relLayoutId);
            relLay.addView(txtDescription);
            relLay.addView(edtDescription);

            relParent.addView(relLay);
            if (count != 0) {
                relLay.addView(imgaddDescription);
            } else {
                relLay.setVisibility(View.GONE);
            }
            incrementIds();

        }

        public void textViewParams(TextView txtDescription) {
            txtDescription.setText("Description:");
            txtDescription.setTextColor(getResources().getColor(R.color.Blue));
            txtDescription.setId(textViewId);
            txtDescription.setMinLines(1);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
                    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            txtDescription.setLayoutParams(params);
        }

        public void imageViewParams(ImageView addSubListDescription) {
            addSubListDescription
                    .setImageResource(R.drawable.add_description_icon);
            addSubListDescription.setId(ImgViewId);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
                    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            addSubListDescription.setOnTouchListener(new OnTouchListener() {

                public boolean onTouch(View arg0, MotionEvent arg1) {

                    addDesScription(relLayParent, relLayViewId - 1);

                    return false;
                }
            });
            addSubListDescription.setLayoutParams(params);
        }

        public void edttxtDescriptionParams(EditText edtDescription) {


            edtDescription.setId(edtTxtViewId);
            edtDescription.setMinLines(1);
            edtDescription.setHint("Description");
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
                    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.LEFT_OF, ImgViewId);
            params.addRule(RelativeLayout.RIGHT_OF, textViewId);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            edtDescription.setLayoutParams(params);
        }

        public void incrementIds() {
            ImgViewId++;
            textViewId++;
            edtTxtViewId++;
            relLayViewId++;
            count++;

        }

        public void relLaySubListDescription(
                RelativeLayout relLaySubListDescription, int relLayoutId) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
                    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);

            params.width = android.widget.RelativeLayout.LayoutParams.FILL_PARENT;
            relLaySubListDescription.setId(relLayViewId);
            params.addRule(RelativeLayout.BELOW, relLayoutId);
            relLaySubListDescription.setLayoutParams(params);
        }
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜