欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-04-15 21:28:54
//本文只针对聊天页面的软键盘和工具页面的切换(类似微信),可直接粘贴//布局

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

//布局

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

本文地址:https://blog.csdn.net/weixin_45189491/article/details/108982385

相关标签: android 软键盘