动画后EditText专注于输入

时间:2018-01-31 17:18:20

标签: java android animation focus focusable

我正在尝试让我的EditText聚焦在动画之后进行扩展。我尝试了以编程方式设置焦点并在Xml中尝试过。我已经用尽了可以解决这个问题的想法,但有人必须知道

MainActivity,

public void onSearchExpand(View view){
    w_SearchBarFullScreen.setVisibility(View.VISIBLE);
    SearchBarExpand = AnimationUtils.loadAnimation(MainActivity.this,R.anim.web_search_expand);
    w_SearchBarFullScreen.startAnimation(SearchBarExpand);
    w_Toolbar.startAnimation(FadeOut);
    w_SearchBarFullScreen.setFocusable(true);
}

XML,

<EditText 
android:id="@+id/w_SearchBarFullScreen"
android:layout_width="360dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/web_search_bar"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1"
android:singleLine="true"
android:text="   "
android:elevation="5dp"
android:focusable="true"
xmlns:android="http://schemas.android.com/apk/res/android" />

最终目标是当动画完成时,它将显示SoftKeyBoard并准备在其中输入内容。

结束了这个,

public void onSearchExpand(View view){
    w_SearchBarFullScreen.setVisibility(View.VISIBLE);

   SearchBarExpand = AnimationUtils.loadAnimation(MainActivity.this,R.anim.web_search_expand);
   w_SearchBarFullScreen.startAnimation(SearchBarExpand);
   w_Toolbar.startAnimation(FadeOut);

   if(w_SearchBarFullScreen.getVisibility() == View.VISIBLE){
       w_SearchBarFullScreen.requestFocus();


   }

}

但是现在的问题是键盘显示它是否有任何建议?

使用动画侦听器我有这个错误

01-31 13:35:27.411 5950-5950/com.example.mickeyt.onyxbrowser 
E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.mickeyt.onyxbrowser, PID: 5950

java.lang.IllegalStateException: Could not execute method for 
android:onClick
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                               at android.view.View.performClick(View.java:5623)
                                                                               at android.view.View$PerformClick.run(View.java:22433)
                                                                               at android.os.Handler.handleCallback(Handler.java:751)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6247)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
                                                                            Caused by: java.lang.reflect.InvocationTargetException
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                               at android.view.View.performClick(View.java:5623) 
                                                                               at android.view.View$PerformClick.run(View.java:22433) 
                                                                               at android.os.Handler.handleCallback(Handler.java:751) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                               at android.os.Looper.loop(Looper.java:154) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6247) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.animation.Animation.setAnimationListener(android.view.animation.Animation$AnimationListener)' on a null object reference
                                                                               at com.example.mickeyt.onyxbrowser.MainActivity.onSearchExpand(MainActivity.java:338)
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                               at android.view.View.performClick(View.java:5623) 
                                                                               at android.view.View$PerformClick.run(View.java:22433) 
                                                                               at android.os.Handler.handleCallback(Handler.java:751) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                               at android.os.Looper.loop(Looper.java:154) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6247) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 

3 个答案:

答案 0 :(得分:1)

你需要一个监听器,它将在动画结束时触发,所以这样做

FadeOut.setAnimationListener(new AnimationListener() {
    public void onAnimationStart(Animation anim){};
    public void onAnimationRepeat(Animation anim){};
    public void onAnimationEnd(Animation anim)
    {
        // as mentioned by lib4
        w_SearchBarFullScreen.requestFocus();
    };
}); 

因为,在动画完成之前,您目前正在请求焦点

或者您可以尝试通过xml将其添加为

<EditText  ...>
    <requestFocus/>
</EditText>

答案 1 :(得分:1)

你可能会尝试这个,一个重要的想法是你可以使用 requestFocus()来获得焦点。

SearchBarExpand.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
               w_SearchBarFullScreen.requestFocus();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

SearchBarExpand.start();

如果使EditText获得焦点但没有显示键盘,那么你应该在onAnimationEnd()中尝试以下内容: -

@Override
public void onAnimationEnd(Animation animation) {

    if(w_SearchBarFullScreen.requestFocus()) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}

答案 2 :(得分:-1)

使用这个对我有用。

 public void onSearchExpand(View view){
    w_SearchBarFullScreen.setVisibility(View.VISIBLE);

   w_SearchBarFullScreen.startAnimation(SearchBarExpand);
   w_Toolbar.startAnimation(FadeOut);

   if(w_SearchBarFullScreen.getVisibility() == View.VISIBLE){
       w_SearchBarFullScreen.requestFocus();
       w_SearchBarFullScreen.setText("   ");
       InputMethodManager imm = (InputMethodManager)   getSystemService(Context.INPUT_METHOD_SERVICE);
       assert imm != null;
       imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
   }