Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Android to implement an input field suspended on a soft keyboard

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use Android to achieve an input field suspended on a soft keyboard". In daily operation, I believe many people have doubts about how to use Android to achieve an input field suspended on a soft keyboard. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "how to use Android to achieve an input field suspended on a soft keyboard". Next, please follow the editor to study!

Suspension column

It is easy to implement the suspension bar.

ChatInputPanel.setVisibility (View.VISIBLE); chatInputEt.setFocusable (true); chatInputEt.setFocusableInTouchMode (true); chatInputEt.requestFocus (); InputMethodManager inputManager = (InputMethodManager) chatInputEt.getContext (). GetSystemService (Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput (chatInputEt, 0)

ChatInputPanel is the suspension bar the whole layout,mChatPanelContent is the actual part of the suspension column, chatInputEt is the EditText of it, and you can levitate the chatInputPanel on the software disk by setting it up.

Here chatInputPanel is full screen (click outside mChatPanelContent to hide the keyboard), mChatPanelContent is at the bottom of its bottom, hidden by default (INVISIBLE).

Full screen of soft keyboard in horizontal screen

When crossing the screen, Android defaults to displaying the soft keyboard full screen, so that the suspension bar cannot be realized. So you need to cancel the full screen display.

Using android:imeOptinos in EditText can make some interface settings for the soft keyboard that comes with Android.

Android:imeOptions= "flagNoExtractUi" / / makes the soft keyboard not full screen and occupies only part of the screen

There is no prompt on the right side of the android:imeOptions= "actionNone" / / input box

Android:imeOptions= "actionGo" / / the button in the lower right corner reads "start"

Android:imeOptions= "actionSearch" / / the button in the lower right corner is a magnifying glass image, search

Android:imeOptions= "actionSend" / / the button in the lower right corner reads "send"

Android:imeOptions= "actionNext" / / the button in the lower right corner reads "next"

Android:imeOptions= "actionDone" / / the button in the lower right corner reads "done"

So we can set the android:imeOptions= "flagNoExtractUi" for the EditText to realize the incomplete display in the horizontal screen. At the same time, it is possible that EditText adds a corresponding listener to capture the listening event when the user clicks the button in the lower right corner of the soft keyboard for processing.

EditText.setOnEditorActionListener (new OnEditorActionListener () {@ Override public boolean onEditorAction (TextView v, int actionId, KeyEvent event) {Toast.makeText (MainActivity.this, "text2", Toast.LENGTH_SHORT). Show (); return false;}}); listening soft keyboard (this method is unreliable, obsolete, and reliable below)

Note: this is a wrong method on the Internet, so take it out and say it. If you are not interested, just go to see (3).

The display is fine, but when the soft keyboard is hidden, the suspension bar is required to be hidden synchronously.

The system does not provide api for monitoring soft keyboards, so we have to implement it ourselves.

ChatInputPanel.getViewTreeObserver () .addOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener () {@ Override public void onGlobalLayout () {if (chatInputPanel.getBottom () > container.getRootView () .getHeight () / 2) {chatInputPanel.setVisibility (View.INVISIBLE);} else {chatInputPanel.setVisibility (View.VISIBLE);})

Listen for changes in the layout of the chatInputPanel (overall layout of the suspension column) and hide when the bottom is more than half the height of the rootview, otherwise it will be displayed.

Because our function is horizontal, when the keyboard pops up, the chatInputPanel is suspended on the keyboard, so the bottom must be less than half the height (screen width) of the rootview.

When you put the keyboard away, chatInputPanel goes back to the bottom (the setting is at the bottom of the parent layout), so the bottom must be greater than half.

This method is unreliable, and redrawing will lead to frequent execution of onGlobalLayout, although you can add a time to control, but it is not recommended to use this way to listen to the soft keyboard, let's take a look at another way.

A reliable way to monitor a soft keyboard

The reason why the above method is not considered is that the full-screen display of FLAG_FULLSCREEN (hidden notification bar) causes problems.

The FLAG_FULLSCREEN property is used when we need to show the hidden notification bar full screen

GetActivity (). GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)

However, it will affect the suspension bar above, because it is found that the chatInputPanel.getBottom () has not changed, but we judge that the hiding depends on this change.

No change is due to the way android handles full-screen FLAG_FULLSCREEN. There are a lot of problems with soft keyboards on full screen, and there are a lot of problems on this web.

How to solve?

Let's just listen to the soft keyboard in a different way.

GetActivity (). GetWindow (). GetDecorView (). GetViewTreeObserver (). AddOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener () {@ Override public void onGlobalLayout () {Rect rect = new Rect (); rootView.getWindowVisibleDisplayFrame (rect); int rootHeight=rootView.getRootView (). GetHeight (); int displayHeight=rect.height (); int diffHeight=rootHeight-displayHeight; if (diffHeight==0) {/ / Keyboard chatInputPanel.setVisibility (View.INVISIBLE) } else {/ / keyboard pop-up chatInputPanel.setVisibility (View.VISIBLE);})

By listening for changes in the content layout of the root layout species, this method is currently the most reliable.

But there is also a small problem, that is, the keyboard will cover a small part of the bottom of the suspension bar in full-screen state.

Ultimate suspension mode

The above solves the monitoring problem of the soft keyboard, but the suspension bar will always be partially covered in the full-screen state, what should I do?

In fact, there is another problem here, when the keyboard is displayed, the overall layout in the app is pushed up, which leads to the reduction of some components and so on.

We need to solve this problem first, leaving the overall layout of the app unchanged, with the keyboard overlaid on it, which needs to be manually set before popping up the keyboard, as follows:

MChatInput.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {DisplayMetrics metric = new DisplayMetrics (); getActivity (). GetWindowManager (). GetDefaultDisplay (). GetMetrics (metric); chatInputPanel.setY (- metric.heightPixels); / / solve the problem that may be pushed up for the first time chatInputEt.setFocusable (true); chatInputEt.setFocusableInTouchMode (true); chatInputEt.requestFocus () InputMethodManager inputManager = (InputMethodManager) chatInputEt.getContext () .getSystemService (Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput (chatInputEt, 0);}})

This will move the suspension bar to the top, there will be no push (guess with the keyboard mechanism, because the keyboard pop-up if the focus of the input components to re-adjust the window, we put the suspension window on top, the keyboard will not block the focus of the EditText, so will not re-adjust the window).

But the suspension bar is invisible all the time, and we can see that the chatInputPanel.setVisibility (View.VISIBLE); code is removed here, so how do you display it?

We mentioned above that we use OnGlobalLayoutListener to monitor the keyboard, so we can display it here. At the same time, we can optimize the position of the display. Here, we can calculate how much the window display area is moved up, and let chatInputPanel move up the corresponding position, such as:

Private int mLastHeight = 0getActivity (). GetWindow (). GetDecorView (). GetViewTreeObserver (). AddOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener () {@ Override public void onGlobalLayout () {Rect rect = new Rect (); getActivity (). GetWindow (). GetDecorView (). GetWindowVisibleDisplayFrame (rect); int height = rect.height (); int rawHeight = getResources (). GetDisplayMetrics (). HeightPixels-rect.top; if (height = mLastHeight) return If (height < rawHeight) {UiThreadHandler.postDelayed (new Runnable () {@ Override public void run () {chatInputPanel.setVisibility (View.VISIBLE); chatInputPanel.setTranslationY (- (rawHeight-height));}}, 200) } else {UiThreadHandler.postDelayed () {@ Override public void run () {chatInputPanel.setVisibility (View.GONE);}}, 100);} mLastHeight = height;}})

You can see that you first get the display height of the current window and the actual height of the screen (window section)

Then first determine whether the window display area has changed, if there is no change, do not deal with it.

If there is a change, then judge whether it is bigger or smaller.

If it gets smaller,

When the keyboard pops up, chatInputPanel is displayed and translationY is set to-(rawHeight-height).

First of all, the bottom of the initial position of chatInputPanel is aligned with the bottom of the screen. Although setY is set, setY is actually setTranslationY. The initial position remains the same, and the source code:

Public void setY (float y) {setTranslationY (y-mTop);}

If you want to display above the keyboard after you pop up the keyboard, you need to move the height of the keyboard up from the bottom, which is rawHeight-height, so move up is to set translationY to-(rawHeight-height).

If it gets bigger,

Explain that the keyboard is put away and the chatInputPanel is hidden.

This not only solves the problem of window pushing up, but also solves the problem of soft keyboard blocking part of the suspension bar, because the position of the suspension bar is obtained by calculation, not through the soft keyboard push leading to layout adjustment and change the position.

At this point, the study on "how to use Android to achieve an input field suspended on a soft keyboard" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report