2

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

主要活动,

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);
}

XM1,

<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) 
4

3 回答 3

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();
    };
}); 

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

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

<EditText  ...>
    <requestFocus/>
</EditText>
于 2018-01-31T17:24:50.917 回答
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();

如果 incase EditText 获得焦点但没有显示键盘,那么您应该在 onAnimationEnd() 中尝试以下操作:-

@Override
public void onAnimationEnd(Animation animation) {

    if(w_SearchBarFullScreen.requestFocus()) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}
于 2018-01-31T17:38:09.127 回答
-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);
   }
于 2018-02-01T03:00:54.737 回答