I. Overview of issues
When the edit box input content will pop up the soft keyboard, and the mobile screen area is limited will often obscure the input interface, we first look at the problem Effect chart:
When you enter a username and password, the system will eject the keyboard, causing the system keyboard to block the text box, as shown in the figure:
Enter the password when the input box is blocked by the system keyboard, greatly reducing the user experience, this is the development of a very common soft keyboard occlusion problem, how to solve?
Second, simple solution
Method One
Write this code before setcontentview in the OnCreate in your activity.
GetWindow (). Setsoftinputmode (WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Method Two
Join in the <activity> of the interface in the project's Androidmanifest.xml file
This will let the screen move up the whole. If you add a android:windowsoftinputmode= "Adjustpan", the keyboard will overwrite the screen.
About Android:windowsoftinputmode
The interaction mode between the main activity window and the soft keyboard can be used to avoid the problem of the Input Panel occlusion and a new feature after Android1.5.
This property affects two things:
"One" when the focus is generated, the soft keyboard is hidden or displayed
"Two" reduces the active main window size to make room for soft keyboard
Its setting must be a value in the following list, or a combination of a "state ..." value plus a "adjust ..." value. Set multiple values in either group-multiple "state ..." values such as &mdash have undefined results. Each value is separated by |
For example:
<activity android:windowsoftinputmode= "Statevisible|adjustresize" ... >
The values set here (except "stateunspecified" and "adjustunspecified") override the values set in the theme
Meaning of each value:
"A" stateunspecified: the state of the soft keyboard is not specified, the system will choose a suitable state or dependent on the theme of the settings
"B" stateunchanged: When this activity appears, the soft keyboard will remain in the previous activity, whether hidden or displayed
"C" Statehidden: When a user chooses an activity, the soft keyboard is always hidden
"D" Statealwayshidden: When the Activity main window gets the focus, the soft keyboard is always hidden
"E" statevisible: Soft keyboards are usually visible
"F" statealwaysvisible: When a user chooses an activity, the soft keyboard always shows the state
"G" adjustunspecified: Default settings, usually determined by the system to hide or show
"H" adjustresize: The activity always adjusts the size of the screen to allow space for the soft keyboard
"I" Adjustpan: The contents of the current window will automatically move so that the current focus is never covered by the keyboard and the user can always see the part of the input
Method Three
Replace top layout with ScrollView, or add a layer of ScrollView to top layout. This will scroll the soft keyboard and the input box together, and the soft keyboard will always be at the bottom.
<scrollview xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent "Android:layout_height=" fill_parent >
<linearlayout android:layout_width= "Fill_parent"
android: layout_height= "fill_parent" android:orientation= "vertical" >
</LinearLayout>
</scrollview >
But these methods although relatively simple, but often have a certain limitation is not very flexible, sometimes not to the desired effect, we can try may also be able to solve your problem, the following teach you a kind of code controllability of a method:
Three, code controllability method
1. Main interface Layout file
<com.jereh.overidelinearlayout.linearlayoutview xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent" android:id= "@+id/login_root_layout" android:layout_height= "Match_parent"
android:orientation= "vertical" > <!-here mimic a login interface--> <linearlayout android:id= "@+id/login_layout_logo" Android:layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "android:background=" # ff0000 "android:orientation=" vertical "> <imageview android:id=" @+id/textview1 "android:layout_width=" Fill_ Parent "android:layout_height=" fill_parent "android:src=" "@drawable/login_logo" android:scaletype= "Fitxy"/> < /linearlayout> <!-input box and Password box--> <linearlayout android:layout_width= "Match_parent"
0DP "android:layout_margintop=" 20DP "android:layout_weight=" 3 "android:orientation=" vertical "> <edittext Android:id= "@+id/edittext1" android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:layout_alignleft= "@+id/textview1" android:layout_alignparentbottom= "true" android:layout_gravity= "Center_ Vertical "android:hint=" username "android:ems=" > <requestfocus/> </EditText> <edittext android:id= "@ +id/edittext1 "android:layout_width=" match_parent "android:layout_height=" Wrap_content "android:layout_alignLeft=" "@+id/textview1" android:layout_alignparentbottom= "true" android:layout_gravity= "center_vertical" android:hint= " Password "android:ems=" > <requestfocus/> </EditText> </LinearLayout> </ Com.jereh.overidelinearlayout.linearlayoutview>
You can see that the key place is Linearlayoutview this custom component
2. Custom Linearlayoutview
This component can be used to hide and display certain areas depending on the popup/shutdown of the soft keyboard, which is the key part of the problem resolution, with two main points:
① Rewrite onsizechanged method
This method is the view life cycle method, when the view size changes, called, such as vertical screen switch, soft keyboard pop-up. Here when the soft keyboard pop-up caused view size changes, will call the Onsizechanged method, in this method to achieve the core idea of the code is based on the size of changes, when the larger (soft keyboard pop-up), some areas are hidden to edit the interface to allow sufficient display space; when resuming (soft keyboard closed), And then show the hidden areas.
protected void onsizechanged (int w,final int h, int oldw,final int oldh) {
super.onsizechanged (W, H, OLDW, OLDH);
Uihandler.post (New Runnable () {public
void run () {
if (Oldh-h > Softkeypad_min_height) {//Soft keyboard off
Keybord Statelistener.statechange (keyborad_show);//callback method displays partial area
}else {//Soft keyboard eject
if (keybordstatelistener!= null) {
Keybordstatelistener.statechange (keyborad_hide);//callback Method Hide part area}}}
);
② provides a keybordstatelistener interface to invoke the implementation of the interface using a callback mechanism.
Code:
public interface keybordstatelistener{public void statechange (int state);
Define interface private Keybordstatelistener Keybordstatelistener; public void Setkeybordstatelistener (Keybordstatelistener keybordstatelistener) {This.keybordstatelistener =
Keybordstatelistener;
The complete code for the Linearlayoutview component: public class Linearlayoutview extends linearlayout{public static final int keyborad_hide = 0;
public static final int keyborad_show = 1;
private static final int softkeypad_min_height = 50;
Private Handler Uihandler = new Handler (); Public Linearlayoutview {Super [context];} public Linearlayoutview (context, AttributeSet attrs {Super (context, attrs);} @Override protected void onsizechanged (int w,final int h, int oldw,final int OLDH) {//TODO
auto-generated Method Stub super.onsizechanged (W, H, OLDW, OLDH); Uihandler.post (New Runnable () {@Override public void run () {if (Oldh-h > Softkeypad_min_height) {keybordstateliste
Ner.statechange (keyborad_show); }else {if (Keybordstatelistener!= null) {Keybordstatelistener.statechange (keyborad_hide);}}
}
});
Private Keybordstatelistener Keybordstatelistener; public void, Setkeybordstatelistener (Keybordstatelistener keybordstatelistener) {This.keybordstatelistener =
Keybordstatelistener; public interface keybordstatelistener{public void statechange (int state);
3. Main interface mainactivity
public class Mainactivity extends activity implements Keybordstatelistener {
private linearlayoutview resizelayout;
private LinearLayout logolayout;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
Gets the Linearlayoutview component
Resizelayout = (Linearlayoutview) Findviewbyid, which hides and displays certain areas depending on the popup/shutdown of the soft keyboard (r.id.login_root_ layout);
Obtain the area
Logolayout = (linearlayout) Findviewbyid (R.id.login_layout_logo) to control the concealment and display;
Resizelayout.setkeybordstatelistener (this);//Set callback method
}
//Implement the method in the interface, which is called in the Resizelayout onsizechanged method
@Override public
void StateChange (int state) {
//TODO auto-generated a stub
switch (state) { C19/>case linearlayoutview.keyborad_hide:
logolayout.setvisibility (view.visible);
break;
Case Linearlayoutview.keyborad_show:
logolayout.setvisibility (view.gone);
break;
}
}
Four, the realization effect
Keyboard Eject:
Keyboard off:
The above is a small set to introduce the Android soft keyboard shielding four kinds of perfect solution, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!