2015-09-14 24 views
2

我創建了一個編程的佈局,並將其設置爲oncreate內的secontentview我創建使用簡單的編輯文本和發送圖像按鈕編輯文本的權利,每當用戶點擊edittext突然隱藏softkeyboard我試過所有選項需要在menifest像adjustPan或screenresize等,但沒有效果提的EditText仍然鍵盤Softkey隱藏的Edittext

藏在這裏是包含的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); 
    } 
+1

我想更多的細節儘量把佈局中的滾動型用戶內可以滾動,如果edittext是隱藏的。 – Biswajit

+0

@ user3471185:請發佈包含'edittext'的佈局。 – AndiGeeky

+0

發表文章您的xml –

回答

0

嘗試在這種方式隱藏鍵盤

我的佈局代碼
// Check if no view has focus: 
View view = this.getCurrentFocus(); 
if (view != null) { 
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 

}

+0

因爲我需要在軟鍵盤上方顯示edittext,當用戶點擊edittext時edittext放置在佈局的底部以及其他視圖 – user3471185

+0

是的,它的完成工作就像簡單的工作一樣。 –

+0

仍然沒有效果,但當我使用相同的動態佈局與一個單獨的應用程序,工作正常任何幫助我會感謝 – user3471185

0

在你Manifest.xml它添加到你的活動

<activity 
     android:windowSoftInputMode="adjustResize|stateHidden" ... > 
     ... 
</activity> 

Click here有關處理輸入法

0
i have just remove getWindow().setFlags(
       WindowManager.LayoutParams.FLAG_FULLSCREEN 
         | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN 
         | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS 
         | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); after that edittext comming below the edittext whenever soft keyboard appear