android聊天界面软键盘和工具页切换(类似微信)

//本文只针对聊天页面的软键盘和工具页面的切换(类似微信),可直接粘贴

//布局



    




    

    

    




    

//MainActivity
public class MainActivity extends AppCompatActivity {
private EditText editText, editText2;
private Button button, btnn, button2, btnn2;
private RelativeLayout relayout_tool, relayout, relayout_recycleview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = findViewById(R.id.ed_text);
    relayout_recycleview = findViewById(R.id.relayout_recycleview);
    editText2 = findViewById(R.id.ed_text2);
    relayout = findViewById(R.id.relayout);
    btnn = findViewById(R.id.btnn);
    button = findViewById(R.id.btn);
    btnn2 = findViewById(R.id.btnn2);
    button2 = findViewById(R.id.btn2);
    relayout_tool = findViewById(R.id.relayout_tool);

    relayout_recycleview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        //点击外部,取消软键盘
            relayout_tool.setVisibility(View.GONE);
            relayout.setVisibility(View.VISIBLE);
            InputMethodManager imm = (InputMethodManager)         getSystemService(Context.INPUT_METHOD_SERVICE);
            boolean isOpen = imm.isActive();
            if (isOpen) {
                // imm.toggleSoftInput(0,
                // InputMethodManager.HIDE_NOT_ALWAYS);//没有显示则显示
                imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
     //edittext 监听是否获取焦点
    editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                relayout.setVisibility(View.GONE);
                editText.requestFocus();
                InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
                try {
                    Thread.sleep(100);
                    relayout_tool.setVisibility(View.VISIBLE);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        }
    });
}

}
//最后在清单文件下的该Activity下加入 android:windowSoftInputMode=“adjustPan|stateHidden”

//代码量不多,只当是记录

你可能感兴趣的:(android聊天界面软键盘和工具页切换(类似微信))