日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android进阶之路 - 软键盘中右下角的设置与监听

發布時間:2023/12/20 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android进阶之路 - 软键盘中右下角的设置与监听 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在項目中,多多少少會遇到修改軟鍵盤右下角按鈕的需求,雖然已經寫過幾次,但是還是覺得在這里專心做個筆記比較放心 ~

我的那些軟鍵盤Blog ~

  • Android進階之路 - 常見軟鍵盤操作行為
  • Android進階之路 - 軟鍵盤中右下角的設置與監聽
  • Android進階之路 - 軟鍵盤頂起解決方案
  • Android進階之路 - 監聽軟鍵盤當前狀態,實現布局上移

Catalog

        • xml - 部分實現
        • 功能 - 部分實現
        • 完整實現

Effect :


xml - 部分實現

  • 確定右下角的功能展示鍵盤
android:imeOptions="actionSearch"

補充:常見類型

類型含義
actionGoGo
actionSearch放大鏡
actionSendSend
actionNextNext
actionDone完成
  • 確定當前的EditText條目(自我認為,它的字面是獨立一行)
android:singleLine="true"
  • 完整的單個EditText代碼
<EditTextandroid:layout_width="match_parent"android:layout_height="45dp"android:background="@null"android:hint="搜索"android:imeOptions="actionSearch"android:singleLine="true"/>

功能 - 部分實現

  • 設置動作監聽,如:
mSearch.setOnEditorActionListener(this);
  • 進行動作監聽處理
@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;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 :

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进阶之路 - 软键盘中右下角的设置与监听的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。