如何编程隐藏安卓软键盘
在本文中,我们将学习如何以编程方式隐藏软键盘。键盘通常会隐藏,但在某些情况下它不会隐藏。所以为了更好的用户体验,键盘是以编程方式隐藏的。
不使用下图方法,app 默认响应如下:-
[https://media.geeksforgeeks.org/wp-content/uploads/20200513200946/Record_2020-05-13-20-08-52_750d96ee45c2372059ea1c16be9c6b891.mp4](https://media.geeksforgeeks.org/wp-content/uploads/20200513200946/Record_2020-05-13-20-08-52_750d96ee45c2372059ea1c16be9c6b891.mp4)进场:
-
现在在 activity_main.xml 文件中添加以下代码。下面的代码在 activity_main 中添加了一个文本视图、编辑文本和一个按钮。点击该按钮将调用 MainActivity 类中的 setText 功能。
activity _ main . XML
```java <?xml version="1.0" encoding="utf-8"?>
<TextView android:textStyle="bold" android:textColor="#219806" android:id="@+id/text_view_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="GeeksForGeeks" android:textSize="22sp" />
<EditText android:layout_marginTop="20dp" android:id="@+id/edit_text_input" android:layout_width="match_parent" android:layout_height="wrap_content" />
```
-
现在在MainActivity.java文件中添加以下代码。这里我们定义设置文本和关闭键盘功能。当用户点击按钮时,setText 函数被调用。它从 edittext 获取输入,并在 textview 中替换它。然后它调用 closeKeyboard 函数并清除 edittext 的值。closeKeyboard 功能隐藏键盘。
MainActivity.java
```java package org.geeksforgeeks.gfgHideKey
import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.inputmethod .InputMethodManager; import android.widget.EditText; import android.widget.TextView;
public class MainActivity extends AppCompatActivity { private TextView textViewResult; private EditText editTextInput;
@Override protected void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
textViewResult = findViewById( R.id.text_view_result); editTextInput = findViewById( R.id.edit_text_input); }
public void setText(View v) { String newText = editTextInput .getText() .toString(); textViewResult.setText(newText);
closeKeyboard(); editTextInput.setText(""); }
private void closeKeyboard() { // this will give us the view // which is currently focus // in this layout View view = this.getCurrentFocus();
// if nothing is currently // focus then this will protect // the app from crash if (view != null) {
// now assign the system // service to InputMethodManager InputMethodManager manager = (InputMethodManager) getSystemService( Context.INPUT_METHOD_SERVICE); manager .hideSoftInputFromWindow( view.getWindowToken(), 0); } } } ```
输出:
[https://media.geeksforgeeks.org/wp-content/uploads/20200505213321/Record_2020-05-05-21-26-14_750d96ee45c2372059ea1c16be9c6b891.mp4](https://media.geeksforgeeks.org/wp-content/uploads/20200505213321/Record_2020-05-05-21-26-14_750d96ee45c2372059ea1c16be9c6b891.mp4)版权属于:月萌API www.moonapi.com,转载请注明出处