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

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

//布局

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:id="@+id/relayout_recycleview"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#3f1a1a">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</RelativeLayout>

<RelativeLayout
    android:id="@+id/relayout_tool"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentBottom="true"
    android:visibility="gone">

    <EditText
        android:id="@+id/ed_text"
        android:layout_width="match_parent"
        android:layout_height="50dp" />

    <ImageView
        android:id="@+id/m"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_below="@id/ed_text"
        android:layout_marginTop="10dp"
        android:src="@mipmap/ic_launcher" />

    <ImageView
        android:id="@+id/mm"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_below="@id/ed_text"
        android:layout_marginStart="20dp"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@id/m"
        android:src="@mipmap/ic_launcher" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/relayout"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true">

    <EditText
        android:id="@+id/ed_text2"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
</RelativeLayout>

//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”

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值