1 Change the style of TextView in the layout layout
1 First, modify its style in the input of EditText, and change the original simple EditText
。。。。。。。。。。。。。。。。。。。。。。。。。。。
<EditText
android:id="@+id/input"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="@color/grey"
/>
。。。。。。。。。。。。。。。。。。。。。。。。。。。
Add FragmentLayout to wrap, and add a LinearLayout to listen to the event when the EditText is clicked. The reason why the following LinearLayout is set is that when the input is clicked, it can trigger the pop-up and recycling keyboard bound by the button below at the same time!
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<EditText
android:id="@+id/input"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="@color/grey"
/>
<LinearLayout
android:id="@+id/layout_edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
</FrameLayout>
2Set the main program
class ChatActivity : AppCompatActivity() {
private lateinit var input: EditText
private lateinit var layout_edit: LinearLayout
private lateinit var weChatAdapter: WeChatAdapter
private lateinit var messageList: RecyclerView
。。。。。。。自定义其他的组件信息。。。。。。。。。。
//增加回调来处理点击显示键盘上时RecyclerView上弹的问题
private var handler: Handler = object : Handler(Looper.getMainLooper()) {
override fun handleMessage(msg: Message) {
super.handleMessage(msg)
when (msg.what) {
0 -> {
messageList.scrollToPosition(messages.size - 1)
}
}
}
}
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.chat_layout)
initValus()
//RecyclerView的部分
messageList = findViewById(R.id.weChatMessage)
//准备好数据
//为其榜上Adapter布局
messageList.layoutManager = LinearLayoutManager(this)
weChatAdapter = WeChatAdapter(messages)
messageList.adapter = weChatAdapter
。。。。。。。。。。。。其他代码。。。。。。。。。。。。
//下面两个方法用来绑定点击事件,当点击输入时进行键盘的弹出和回收
layout_edit.setOnClickListener(View.OnClickListener {
input.requestFocus()
showSoftInput(this, input)
handler.sendEmptyMessageDelayed(0, 250)
})
//点击RecyclerView就回收键盘
messageList.setOnTouchListener {
_, _ ->
hideSoftInput(this@ChatActivity, input)
false
}
。。。。。。。。。。。。其他代码。。。。。。。。。。。。
}
//加入下面两个方法,用来强制弹出键盘的服务
fun showSoftInput(context: Context, view: View) {
var imm =
context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(view, InputMethodManager.SHOW_FORCED)
}
fun hideSoftInput(context: Context, view: View) {
var imm =
context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
fun initValus() {
//其他要初始化的界面组件
。。。。。。。。。。。。。
input = findViewById(R.id.input)
layout_edit = findViewById(R.id.layout_edit)
//其他要初始化的界面组件
。。。。。。。。。。。
}
}