我试图在动画展开后让我的 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)