由软键盘隐藏的Edittext

时间:2015-09-14 04:44:29

标签: android

我已经以编程方式创建了一个布局并将其设置为onconate中的secontentview我使用简单的edittext创建聊天布局,并且只要用户点击edittext突然通过软键盘隐藏,就可以使用sendtext发送图像按钮我尝试了所有需要提及的选项在换句包括adjustPan或screenresize等但不影响仍然被键盘隐藏的编辑文本

这是我的布局代码,其中包含edittext

private LinearLayout getTextChatViewTest() {
//      MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//  LibraryMainActivity.this.getActionBar().show();

    LinearLayout linWrapperLayout;
    LinearLayout.LayoutParams linWrapperLayoutParams;

    LinearLayout linHeaderLayout;
    LinearLayout.LayoutParams linHeaderParams;
    TextView txtHeaderText;

    linWrapperLayout = new LinearLayout(MainActivity.this);
    linWrapperLayout.setOrientation(LinearLayout.VERTICAL);
    linWrapperLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                       LinearLayout.LayoutParams.MATCH_PARENT);
    linWrapperLayout.setLayoutParams(linWrapperLayoutParams);
    linWrapperLayout.setBackgroundColor(Color.parseColor("#ffffff"));

    linHeaderLayout = new LinearLayout(MainActivity.this);
    linHeaderLayout.setBackgroundColor(Color.parseColor("#E3E9F3"));
    linHeaderLayout.setPadding(pixToDp(10), pixToDp(5), pixToDp(10), pixToDp(5));
    linHeaderParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    linHeaderLayout.setLayoutParams(linHeaderParams);

    txtHeaderText = new TextView(MainActivity.this);
    txtHeaderText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
    txtHeaderText.setPadding(pixToDp(5), pixToDp(10), pixToDp(10), pixToDp(10));
    txtHeaderText.setTextColor(Color.parseColor("#004A8F"));
    linHeaderLayout.addView(txtHeaderText);
    txtHeaderText.setText("Text Chat");

    RelativeLayout rel = new RelativeLayout(MainActivity.this);
    rel.setBackgroundColor(Color.parseColor("#ffffff"));
    RelativeLayout.LayoutParams relPArams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                                            RelativeLayout.LayoutParams.MATCH_PARENT);
    rel.setLayoutParams(relPArams);
    linWrapperLayout.addView(rel);
    rel.addView(linHeaderLayout);

    txtChatArea = new TextView(MainActivity.this);


    txtChatArea.setId(011);
    txtChatArea.setBackgroundColor(Color.parseColor("#ffffff"));
    txtChatArea.setGravity(Gravity.BOTTOM);
    int scroll_amount = (int) (txtChatArea.getLineCount() * txtChatArea.getLineHeight()) - (txtChatArea.getBottom() - txtChatArea.getTop());

    txtChatArea.setLinksClickable(true);
    txtChatArea.setAutoLinkMask(Linkify.WEB_URLS);
    txtChatArea.setText("Anything");
    txtChatArea.scrollTo(0, scroll_amount);
    txtChatArea.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
    txtChatArea.setScrollY(0);
    txtChatArea.setMovementMethod(new ScrollingMovementMethod());

    /** for allowing copy from text chat area */
    txtChatArea.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            txtChatArea.setCursorVisible(true);
            return true;
        }
    });


//  Button btn = new Button (this);
    android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
//  btn.setHeight((int)(display.getHeight()*0.68));

    int height = display.getHeight(); 
    RelativeLayout.LayoutParams relImageParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                                                 (MainActivity.getPixelHeight(MainActivity.this))/2+60);
    relImageParams.setMargins(pixToDp(15), pixToDp(100),  pixToDp(15), 0);
    relImageParams.addRule(RelativeLayout.BELOW, 111);
    txtChatArea.setLayoutParams(relImageParams);
    rel.addView(txtChatArea);




    /* The following section was copied from incoming call view */
    relIncomingBottomLayout = new RelativeLayout(MainActivity.this);
    relIncomingBottomLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                                    RelativeLayout.LayoutParams.WRAP_CONTENT);
    relIncomingBottomLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    //relIncomingBottomLayoutParams.addRule(RelativeLayout.BELOW, 011);
    relIncomingBottomLayoutParams.setMargins(pixToDp(10), 0, pixToDp(10), pixToDp(10));
    relIncomingBottomLayout.setLayoutParams(relIncomingBottomLayoutParams);
    rel.addView(relIncomingBottomLayout);


    /* Now add a linearlayout with vertical orientation, which will contain two linearlayouts with horizontal orientation */
    LinearLayout linBottomHolder = new LinearLayout(MainActivity.this);
    linBottomHolder.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams linHolderParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                                LinearLayout.LayoutParams.WRAP_CONTENT);
    linBottomHolder.setLayoutParams(linHolderParams);
    relIncomingBottomLayout.addView(linBottomHolder);

    /* an warning view on characters left for end users */
    txtWarning = new TextView(MainActivity.this);
    txtWarning.setBackgroundColor(Color.parseColor("#ffffff"));
    txtWarning.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
    txtWarning.setText("150 characters left");
    txtWarning.setTextColor(Color.GRAY);
    linBottomHolder.addView(txtWarning);

    /* Now add the first linear layout to contain an edittext to get message and an imageview */
    LinearLayout linWriteHolder = new LinearLayout(MainActivity.this);
    linWriteHolder.setOrientation(LinearLayout.HORIZONTAL);
    LinearLayout.LayoutParams linWriteHolderPArams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                                     LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
    linWriteHolder.setLayoutParams(linWriteHolderPArams);
    linBottomHolder.addView(linWriteHolder);

    /* Now add an edittext and an imageview inside this linearlayout */
    GradientDrawable gd = new GradientDrawable();
    gd.setStroke(2, Color.parseColor("#E3E9F3"));
    gd.setShape(GradientDrawable.RECTANGLE);
    gd.setColor(Color.WHITE);
    gd.setCornerRadius(5);
    edtWriteMesage = new EditText(MainActivity.this);
    edtWriteMesage.setHint("Type text here");
    edtWriteMesage.setBackgroundColor(Color.parseColor("#ffffff"));
    edtWriteMesage.setBackground(gd);
    edtWriteMesage.setSingleLine(true);

    /** filtering inputs */
    InputFilter[] filters = new InputFilter[1];
    filters[0] = new InputFilter.LengthFilter(150);
    edtWriteMesage.setFilters(filters); /** now a maximum of 150 characters can be taken as input */

    edtWriteMesage.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            int currentLength = edtWriteMesage.getText().toString().length();
            int remainingLength = 150 - currentLength;
            if(remainingLength <= 15){
                txtWarning.setTextColor(Color.RED);
            }else{
                txtWarning.setTextColor(Color.GRAY);
            }
            txtWarning.setText(""+remainingLength + " characters left");
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    });

    LinearLayout.LayoutParams linWriteParams = new LinearLayout.LayoutParams(0,
                                               ViewGroup.LayoutParams.WRAP_CONTENT, 0.9f);
    linWriteParams.gravity = Gravity.CENTER_VERTICAL;
    edtWriteMesage.setLayoutParams(linWriteParams);
    linWriteHolder.addView(edtWriteMesage);

    /* Now add an imageview with an arrow in it by the edittext's side */
    imgSendArrow = new ImageView(MainActivity.this);
    byte[] sendArrow = Base64.decode(SecondImageStore.send, Base64.DEFAULT);
    //byte[] sendArrow = Base64.decode(SecondImageStore.papersend, Base64.DEFAULT);

    imgSendArrow.setImageBitmap(BitmapFactory.decodeByteArray(sendArrow, 0, sendArrow.length));
    LinearLayout.LayoutParams linSendParams = new LinearLayout.LayoutParams(0,
                                              ViewGroup.LayoutParams.WRAP_CONTENT, 0.1f);
    linSendParams.gravity = Gravity.CENTER;
    linSendParams.setMargins(pxToDp(3), 0, pxToDp(2), 0);

    imgSendArrow.setLayoutParams(linSendParams);
    linWriteHolder.addView(imgSendArrow);


    /* Now add the Second relative layout to contain two buttons */
    LinearLayout linTextChatButtonHolder = new LinearLayout(MainActivity.this);
    LinearLayout.LayoutParams linTextChatButtonHolderParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                                                LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
    linTextChatButtonHolderParams.setMargins(0, pixToDp(10), 0, 0);
    linTextChatButtonHolder.setLayoutParams(linTextChatButtonHolderParams);
    linBottomHolder.addView(linTextChatButtonHolder);






    /* Button to end ongoing conference */
    btnTextChatEndCall = new Button(MainActivity.this);
    btnTextChatEndCall.setText("Call Held");
    btnTextChatEndCall.setBackgroundColor(Color.parseColor("#C11E0F"));
    btnTextChatEndCall.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
    btnTextChatEndCall.setGravity(Gravity.CENTER);
    btnTextChatEndCall.setTextColor(Color.parseColor("#ffffff"));

    LinearLayout.LayoutParams linTextChatEndCallParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                         ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f);
    linTextChatEndCallParams.setMargins(0, pixToDp(10), 0, 0);
    btnTextChatEndCall.setLayoutParams(linTextChatEndCallParams);
    linTextChatButtonHolder.addView(btnTextChatEndCall);

    /* Button to display menu in chat mode */
    btnTextChatMenu = new Button(MainActivity.this);
    btnTextChatMenu.setText("Menu");
    btnTextChatMenu.setGravity(Gravity.CENTER);
    btnTextChatMenu.setBackgroundColor(Color.parseColor("#014A8E"));
    btnTextChatMenu.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
    btnTextChatMenu.setTextColor(Color.parseColor("#ffffff"));

    LinearLayout.LayoutParams linTextChatMenuParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                         ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f);
    linTextChatMenuParams.setMargins(pixToDp(10), pixToDp(10), 0, 0);
    btnTextChatMenu.setLayoutParams(linTextChatMenuParams);
    linTextChatButtonHolder.addView(btnTextChatMenu);


    btnTextChatEndCall.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            try{

            }catch(Exception e){
                e.printStackTrace();
            }
        }
    });




    edtWriteMesage.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            int result = actionId & EditorInfo.IME_MASK_ACTION;
            switch (result) {
            case EditorInfo.IME_ACTION_DONE:

                break;
            case EditorInfo.IME_ACTION_NEXT:
                break;
            }
            return false;
        }
    });

    return linWrapperLayout;
//   setContentView(linWrapperLayout);
    }

0 个答案:

没有答案