android聊天介面軟鍵盤和工具頁切換(類似微信)

2020-10-11 12:00:23

//本文只針對聊天頁面的軟鍵盤和工具頁面的切換(類似微信),可直接貼上

//佈局

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

//程式碼量不多,只當是記錄