片段中的webview在android 4.4.x之前没有调用select javascipt fucntion change()。系统浏览器可以这样做。但是Activity中的webview可以调用。请帮助我。
mi_fragment_tab_test.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="com.tanliani.fragment.TabHomeFragment" >
<WebView
android:id="@+id/mi_tab_test_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
TabTestFragment.java
public class TabTestFragment extends Fragment {
private static final String TAG = TabTestFragment.class.getSimpleName();
private View view;
private WebView mCurrentWebView;
private HashMap<String, String> additionalHttpHeaders = new HashMap<String, String>();
private Context context;
public static TabTestFragment newInstance() {
TabTestFragment fragment = new TabTestFragment();
return fragment;
}
public TabTestFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
Logger.i(TAG, "onCreate :: savedInstanceState = " + savedInstanceState);
super.onCreate(savedInstanceState);
context = getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater
.inflate(R.layout.mi_fragment_tab_test, container, false);
mCurrentWebView = (WebView) view.findViewById(R.id.mi_tab_test_webview);
WebSettings setting = mCurrentWebView.getSettings();
setting.setJavaScriptEnabled(true);
setting.setUserAgentString("Mi-Android");
mCurrentWebView.setWebChromeClient(new WebChromeClient());
mCurrentWebView.setWebViewClient(new WebViewClient());
additionalHttpHeaders.put("unique_id", DeviceUtils.getIMEI(context));
additionalHttpHeaders.put("ApiKey", "76b86bbd");
// mCurrentWebView.loadUrl(CommonDefine.MI_H5_MEMBERS_SEARCH);
mCurrentWebView.loadUrl("file:///android_asset/select.html");
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onResume() {
Logger.i(TAG, "onResume :: ");
super.onResume();
}
@Override
public void onPause() {
Logger.i(TAG, "onPause :: ");
super.onPause();
}
@Override
public void onStop() {
Logger.i(TAG, "onStop :: ");
super.onStop();
}
@Override
public void onDetach() {
super.onDetach();
}
}
select.html
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试select</title>
<script type="text/javascript" src="file:///android_asset/jquery-1.11.2.min.js"></script>
</head>
<body>
<select name="province" id="search-province">
<option value="">地区不限</option>
<option value="1">北京</option>
<option value="2">河南</option>
<option value="3">广西</option>
<option value="4">山东</option>
<option value="5">青海</option>
<option selected="selected" value="6">江西</option>
<option value="7">湖北</option>
<option value="8">西藏</option>
<option value="9">新疆</option>
<option value="10">四川</option>
<option value="12">湖南</option>
<option value="13">内蒙古</option>
<option value="14">上海</option>
<option value="15">浙江</option>
<option value="16">重庆</option>
<option value="17">海南</option>
<option value="18">河北</option>
<option value="19">吉林</option>
<option value="20">江苏</option>
<option value="21">贵州</option>
<option value="23">黑龙江</option>
<option value="24">山西</option>
<option value="25">甘肃</option>
<option value="26">辽宁</option>
<option value="27">天津</option>
<option value="28">广东</option>
<option value="29">宁夏</option>
<option value="30">陕西</option>
<option value="32">福建</option>
<option value="33">安徽</option>
<option value="34">云南</option>
</select>
<script>
//选择地区
$("#search-province").change(function () {
var province = $("#search-province").find("option:selected").text();
alert(province);
});
</script>
</body>
</html>
FragmentTabHost中的片段
@Override
protected void onCreate(Bundle savedInstanceState) {
Logger.i(TAG, "onCreate :: savedInstanceState = " + savedInstanceState);
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.mi_activity_main);
context = this;
// View
mFragmentManager = getSupportFragmentManager();
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, mFragmentManager, android.R.id.tabcontent);
TabWidget mTabWidget = (TabWidget) findViewById(android.R.id.tabs);
if (Build.VERSION.SDK_INT >= 11) {
mTabWidget.setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
}
...
// Test
mTabHost.addTab(
mTabHost.newTabSpec(TAB_TAG_Test).setIndicator(
newTabItem(R.drawable.mi_selector_tab_search,
R.string.mi_navi_title_search)),
TabTestFragment.class, null);
...
}
您可以从此处WebViewDemo
下载演示