Android - keyboard won't appear for edittext after animation happens

偶尔善良 提交于 2019-12-23 22:39:32

问题


I have seen questions/solutions here for the keyboard not showing up, but none relating to my specific issue. When you click an EditText, the keyboard shows up fine. But, when I animate the EditText first, then when you click the EditText the keyboard will NOT show up.

Any ideas why this could be happening?

In my Activity I first animate my logo, and after it's done animating I create a fade-in animation for the EditTexts. After they finish the animation, I try and click any of them but the keyboard will not show up.

Here is my onCreate method in my Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); //Remove title bar

    setContentView(R.layout.login);

    final EditText emailField = (EditText) findViewById(R.id.emailField);
    final EditText passwordField = (EditText) findViewById(R.id.passwordField);
    final TextView logInText = (TextView) findViewById(R.id.logInText);
    final Button signupButton = (Button) findViewById(R.id.signupButton);
    final Button loginButton = (Button) findViewById(R.id.loginButton);

    //animation of logo
    ImageView img_animation = (ImageView) findViewById(R.id.encoreLogo);

    TranslateAnimation animation = new TranslateAnimation(0.0f, 00f,
            0.0f, -400.0f);  
    animation.setDuration(4000);  
    animation.setFillAfter(true);   
    animation.setStartOffset(2000);

    img_animation.startAnimation(animation); 

    animation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationEnd(Animation animation) {
            Animation animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_in);
            animFadeIn.setDuration(2000);
            animFadeIn.setFillAfter(true);

            emailField.setAnimation(animFadeIn);
            passwordField.setAnimation(animFadeIn);
            logInText.setAnimation(animFadeIn);
            loginButton.setAnimation(animFadeIn);
            signupButton.setAnimation(animFadeIn);


        }
        @Override
        public void onAnimationRepeat(Animation animation) {}
        @Override
        public void onAnimationStart(Animation animation) {}

    });

    signupButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(MainActivity.this, SignUpActivity.class);
            MainActivity.this.startActivity(myIntent);

        }
    });


}

and here is my corresponding login.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"
tools:context=".MainActivity" >

<ImageView
    android:id="@+id/backroundImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:cropToPadding="false"
    android:scaleType="centerCrop"
    android:src="@drawable/crowdblur1" />

<ImageView
    android:id="@+id/encoreLogo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:adjustViewBounds="true"
    android:maxHeight="@dimen/thumbnail_height"
    android:maxWidth="@dimen/thumbnail_width"
    android:scaleType="centerInside"
    android:src="@drawable/hand72" />

<TextView
    android:id="@+id/logInText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/encoreLogo"
    android:layout_alignLeft="@+id/passwordField"
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true"
    android:layout_marginBottom="79dp"
    android:paddingLeft="@dimen/left_padding_login_text"
    android:text="Login to Encore"
    android:textColor="@android:color/white"
    android:textSize="@dimen/log_in_text"
    android:textStyle="bold"
    android:visibility="invisible" />

<EditText
    android:id="@+id/emailField"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/encoreLogo"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="18dp"
    android:background="@drawable/rounded_corners"
    android:ems="10"
    android:inputType="textEmailAddress"
    android:hint="Email"
    android:textColor="@android:color/black"
    android:textStyle="italic"
    android:visibility="invisible" />

<EditText
    android:id="@+id/passwordField"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/encoreLogo"
    android:layout_centerHorizontal="true"
    android:background="@drawable/rounded_corners"
    android:ems="10"
    android:inputType="textPassword"
    android:hint="Password"
    android:textStyle="italic"
    android:textColor="@android:color/black"
    android:visibility="invisible" />

<Button
    android:id="@+id/signupButton"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/loginButton"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="22dp"
    android:background="@android:color/transparent"
    android:text="@string/signUp"
    android:textColor="#5FC2FF"
    android:textSize="@dimen/signupText"
    android:textStyle="bold"
    android:visibility="invisible" />

<Button
    android:id="@+id/loginButton"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/passwordField"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:background="@android:color/transparent"
    android:text="@string/login"
    android:textColor="@android:color/white"
    android:textSize="@dimen/signupText"
    android:visibility="invisible" />

Thanks in advance!


回答1:


So it seems that when you have the visibility set to INVISIBLE on the EditText fields it doesn't want to get focus.

I fixed this issue by changing the visibility on those fields after the animation completes like this:

animFadeIn.setAnimationListener(new AnimationListener() {
    @Override
    public void onAnimationEnd(Animation animation) {
        emailField.setVisibility(View.VISIBLE);
        passwordField.setVisibility(View.VISIBLE);
        logInText.setVisibility(View.VISIBLE);
        loginButton.setVisibility(View.VISIBLE);
        signupButton.setVisibility(View.VISIBLE);
    }

    @Override
    public void onAnimationStart(Animation animation) {}

    @Override
    public void onAnimationRepeat(Animation animation) {}

});

Hope this helps you :)




回答2:


It seems like you want to wait for the image (logo) to finish its animation then start the animations for TextView and EditView. Instead of using setAnimationListener what you can do is set the start offset value of animFadeIn to 4000 miliseconds.

Try this:

TranslateAnimation animation = new TranslateAnimation(0.0f, 00f,
                0.0f, -400.0f);  
        animation.setDuration(4000);  
        animation.setFillAfter(true);   
        animation.setStartOffset(2000);
        img_animation.startAnimation(animation);

        Animation animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_in);
        animFadeIn.setDuration(2000);
        animFadeIn.setStartOffset(4000);
        animFadeIn.setFillAfter(true);

        emailField.setAnimation(animFadeIn);
        passwordField.setAnimation(animFadeIn);
        logInText.setAnimation(animFadeIn);
        loginButton.setAnimation(animFadeIn);
        signupButton.setAnimation(animFadeIn);

The keyboard opens up fine for me.




回答3:


Had the same problem. Just don't use fillAfter, it's crap. Combine with @Wextux's solution.



来源:https://stackoverflow.com/questions/20221405/android-keyboard-wont-appear-for-edittext-after-animation-happens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!