为什么软键盘不会在带有 WebView 的 Fragment 中显示
我正在使用兼容性包,并且我有一个在 onCreateView 中返回 WebView
的 Fragment
。问题是,如果在 Activity
的 onCreate 期间未添加片段,那么当单击 WebView
内部的文本框时,不会显示软键盘。如果在添加自定义 Web 片段后旋转设备并重新创建活动,则在单击文本字段时会显示软键盘。
需要明确的是,这里有两种不同的场景,
public void onCreate(Bundle state){
if(state == null){
WebFragment web = new WebFragment();
getSupportFragmentManager.beginTransaction().add(android.R.id.content, web).commit();
}
}
public void onClick(View v){
WebFragment web = new WebFragment();
getSupportFragmentManager.beginTransaction().add(android.R.id.content, web).commit();
}
在第一种情况下,当在 Activity onCreate 方法期间添加片段时,片段中包含的 WebView 将在按下文本字段时正常工作。但是,在第二个示例中,单击 Web 视图中的文本字段时不会发生任何情况,除非在显示 Web 视图后旋转设备。有人可以提供一个解决方案吗,如果我必须为我的片段创建一个新的活动才能正常工作,那么它首先就超出了片段的目的。
I am using the compatibility package and I have a Fragment
who returns a WebView
in onCreateView. The problem is if the fragment isn't added during onCreate of the Activity
then when a textbox is clicked inside of the WebView
the softkeyboard is not displayed. If the device is rotated after the the custom web fragment has been added, recreating the activity, then the softkeyboard is displayed when clicking on a text field.
Just to be clear Here are the two different scenarios
public void onCreate(Bundle state){
if(state == null){
WebFragment web = new WebFragment();
getSupportFragmentManager.beginTransaction().add(android.R.id.content, web).commit();
}
}
public void onClick(View v){
WebFragment web = new WebFragment();
getSupportFragmentManager.beginTransaction().add(android.R.id.content, web).commit();
}
In the first case when adding the fragment during the Activity onCreate method then the WebView contained in the fragment works as it should when text fields are pressed. However, in the second example nothing happens when clicking a text field in the webview unless you rotate the device after the webview had been displayed. Can someone offer up a solution, if I have to create a new activity for my fragment to work properly it sorta beats the purpose of the fragment in the first place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Fragment 的 onCreateView 方法中
将其添加到使用 android 2.3.3 和 3.2.0 测试的
。基于问题 #7189 和 StackOverflow
Add this to the onCreateView method of the Fragment
Tested with android 2.3.3 and 3.2.0.
Based on issue #7189 and StackOverflow
同样 RuslanK 的解决方案对我有用
创建一个扩展 WebView 的类
,然后将您的视图添加到 xml 布局
:等等,等等代码......
As well solution by RuslanK is worked for me
Create a class that extend the WebView
then add to a xml layout your view:
and so on, so on int the code...