Android 軟鍵盤狀態(tài)并隱藏輸入法的實(shí)例
Android 軟鍵盤狀態(tài)并隱藏輸入法的實(shí)例
1 軟鍵盤狀態(tài)的切換
2 強(qiáng)制隱藏輸入法鍵盤
MainActivity如下:
package cc.c; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.app.Activity; import android.content.Context; /** * Demo描述: * 1 軟鍵盤狀態(tài)的切換 * 2 強(qiáng)制隱藏輸入法鍵盤 */ public class MainActivity extends Activity { private EditText mEditText; private Button mButton; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mContext=this; mEditText=(EditText) findViewById(R.id.editText); mButton=(Button) findViewById(R.id.button); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //toggleInput(mContext); hideInput(mContext,mEditText); } }); } /** * 切換軟鍵盤的狀態(tài) * 如當(dāng)前為收起變?yōu)閺棾?若當(dāng)前為彈出變?yōu)槭掌? */ private void toggleInput(Context context){ InputMethodManager inputMethodManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); } /** * 強(qiáng)制隱藏輸入法鍵盤 */ private void hideInput(Context context,View view){ InputMethodManager inputMethodManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); } }
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:hint="Input here" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/editText" android:layout_centerHorizontal="true" android:layout_marginBottom="15dp" android:hint="Button" /> </RelativeLayout>
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
android 處理配置變更的實(shí)現(xiàn)方法
這篇文章主要介紹了android 處理配置變更的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07SwipeLayout框架實(shí)現(xiàn)側(cè)拉刪除編輯功能
這篇文章主要為大家詳細(xì)介紹了SwipeLayout框架實(shí)現(xiàn)側(cè)拉刪除編輯功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Android 通過webservice上傳多張圖片到指定服務(wù)器詳解
這篇文章主要介紹了Android 通過webservice上傳多張圖片到指定服務(wù)器詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02Android使用MulticastSocket實(shí)現(xiàn)多點(diǎn)廣播圖片
這篇文章主要為大家詳細(xì)介紹了Android使用MulticastSocket實(shí)現(xiàn)多點(diǎn)廣播圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Android中使用SeekBar拖動(dòng)條實(shí)現(xiàn)改變圖片透明度(代碼實(shí)現(xiàn))
這篇文章主要介紹了Android中使用SeekBar拖動(dòng)條實(shí)現(xiàn)改變圖片透明度,需要的朋友可以參考下2020-01-01Android開發(fā)基于ViewPager+GridView實(shí)現(xiàn)仿大眾點(diǎn)評(píng)橫向滑動(dòng)功能
這篇文章主要介紹了Android開發(fā)基于ViewPager+GridView實(shí)現(xiàn)仿大眾點(diǎn)評(píng)橫向滑動(dòng)功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09