Android进阶之路 - 软键盘中右下角的设置与监听
生活随笔
收集整理的這篇文章主要介紹了
Android进阶之路 - 软键盘中右下角的设置与监听
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在項目中,多多少少會遇到修改軟鍵盤右下角按鈕的需求,雖然已經寫過幾次,但是還是覺得在這里專心做個筆記比較放心 ~
我的那些軟鍵盤Blog ~
- Android進階之路 - 常見軟鍵盤操作行為
- Android進階之路 - 軟鍵盤中右下角的設置與監聽
- Android進階之路 - 軟鍵盤頂起解決方案
- Android進階之路 - 監聽軟鍵盤當前狀態,實現布局上移
Catalog
- xml - 部分實現
- 功能 - 部分實現
- 完整實現
Effect :
xml - 部分實現
- 確定右下角的功能展示鍵盤
補充:常見類型
| actionGo | Go |
| actionSearch | 放大鏡 |
| actionSend | Send |
| actionNext | Next |
| actionDone | 完成 |
- 確定當前的EditText條目(自我認為,它的字面是獨立一行)
- 完整的單個EditText代碼
功能 - 部分實現
- 設置動作監聽,如:
- 進行動作監聽處理
- 隱藏鍵盤方法
完整實現
MainActivity :
package com.example.yongliu.keyboard;import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.KeyEvent; import android.view.View; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener, TextView.OnEditorActionListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);EditText mSearch = findViewById(R.id.search);EditText mNext = findViewById(R.id.next);EditText mDone = findViewById(R.id.done);EditText mGo = findViewById(R.id.go);EditText mSend = findViewById(R.id.send);mSearch.setOnClickListener(this);mNext.setOnClickListener(this);mDone.setOnClickListener(this);mGo.setOnClickListener(this);mSend.setOnClickListener(this);mSearch.setOnEditorActionListener(this);mNext.setOnEditorActionListener(this);mDone.setOnEditorActionListener(this);mGo.setOnEditorActionListener(this);mSend.setOnEditorActionListener(this);}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.search:Toast.makeText(this, "搜索 - 點擊", Toast.LENGTH_LONG).show();break;case R.id.next:Toast.makeText(this, "下一步 - 點擊", Toast.LENGTH_LONG).show();break;case R.id.done:Toast.makeText(this, "完成 - 點擊", Toast.LENGTH_LONG).show();break;case R.id.go:Toast.makeText(this, "Go - 點擊", Toast.LENGTH_LONG).show();break;case R.id.send:Toast.makeText(this, "發送 - 點擊", Toast.LENGTH_LONG).show();break;}}@Overridepublic boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {boolean state = true;switch (actionId) {case EditorInfo.IME_ACTION_SEARCH:Toast.makeText(this, "軟鍵盤內部 - 搜索", Toast.LENGTH_LONG).show();hintKeyboard();break;case EditorInfo.IME_ACTION_NEXT:Toast.makeText(this, "軟鍵盤內部 - 下一步", Toast.LENGTH_LONG).show();hintKeyboard();break;case EditorInfo.IME_ACTION_DONE:Toast.makeText(this, "軟鍵盤內部 - 完成", Toast.LENGTH_LONG).show();hintKeyboard();break;case EditorInfo.IME_ACTION_GO:Toast.makeText(this, "軟鍵盤內部 - Go", Toast.LENGTH_LONG).show();hintKeyboard();break;case EditorInfo.IME_ACTION_SEND:Toast.makeText(this, "軟鍵盤內部 - 發送", Toast.LENGTH_LONG).show();hintKeyboard();break;default:state = false;break;}return state;}/*隱藏軟鍵盤*/void hintKeyboard() {InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);if (inputMethodManager.isActive()) {inputMethodManager.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), 0);}} }MainActivity Xml :
<?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.yongliu.keyboard.MainActivity"><TextViewandroid:layout_width="match_parent"android:layout_height="45dp"android:gravity="center"android:text="軟鍵盤監聽"/><EditTextandroid:id="@+id/search"android:layout_width="match_parent"android:layout_height="45dp"android:background="@null"android:gravity="center|start"android:hint="搜索"android:imeOptions="actionSearch"android:paddingLeft="15dp"android:singleLine="true"android:textSize="13sp"/><EditTextandroid:id="@+id/send"android:layout_width="match_parent"android:layout_height="45dp"android:background="@null"android:gravity="center|start"android:hint="發送"android:imeOptions="actionSend"android:paddingLeft="15dp"android:singleLine="true"android:textSize="13sp"/><EditTextandroid:id="@+id/go"android:layout_width="match_parent"android:layout_height="45dp"android:background="@null"android:gravity="center|start"android:hint="Go"android:imeOptions="actionGo"android:paddingLeft="15dp"android:singleLine="true"android:textSize="13sp"/><EditTextandroid:id="@+id/next"android:layout_width="match_parent"android:layout_height="45dp"android:background="@null"android:gravity="center|start"android:hint="下一步"android:imeOptions="actionNext"android:paddingLeft="15dp"android:singleLine="true"android:textSize="13sp"/><EditTextandroid:id="@+id/done"android:layout_width="match_parent"android:layout_height="45dp"android:background="@null"android:gravity="center|start"android:hint="完成"android:imeOptions="actionDone"android:paddingLeft="15dp"android:singleLine="true"android:textSize="13sp"/> </LinearLayout>總結
以上是生活随笔為你收集整理的Android进阶之路 - 软键盘中右下角的设置与监听的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【MATLAB教程案例29】基于Bake
- 下一篇: 急急急求微信公众号开发接口-php如何实