我知道showFindDialog已经过折旧,但它在我的webview中提供了我需要的功能。为此,我在片段中使用此代码在webview中显示微调器选择:
if (wv != null) {
Log.d("Log", "Search Method");
wv.showFindDialog(null, true);
}
此代码的重点应该是非常自我解释,但它在操作栏中提供了一个搜索栏以及下一个,上一个和一个"复选标记"按钮以关闭搜索。
我的问题是,在切换到片段之后,在导航抽屉中,搜索选项不会显示搜索文本条目或任何按钮,但它会显示键盘。
我的片段菜单正在使用这些详细信息:
在onCreate中我有:
setHasOptionsMenu(true);
我调用onCreateOptionsMenu并使用MainActivity正在使用的单独菜单,我清除菜单选项:
menu.clear();
最后,我的onOptionsItemSelected使用switch / case类型来运行方法:
search();
最后是搜索方法:
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void search() {
if (Build.VERSION.SDK_INT >= 11) {
if (wv != null) {
Log.d("Log", "Search Method");
wv.showFindDialog(null, true);
}
} else {
searchOldMethod();
}
}
*注意 - 我不关心searchOldMethod();如上所示。
我正在链接下面发生的事情的照片。这是在我从显示的片段切换到另一个片段后再次发生的。我点击了搜索,只填充了键盘。 (托管在imgur上):
有谁知道为什么会这样?
碎片:
package com.example.fragmenttest.app;
import android.annotation.TargetApi;
import android.app.Activity;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.Toast;
import java.lang.reflect.Method;
public class BlankFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
ProgressBar pd = null;
public static final String PREFS_NAME2 = "SearchFile";
Spinner spLoadFrom;
WebView wv;
private LinearLayout container;
private Button nextButton, closeButton;
private EditText findBox;
private ArrayAdapter<CharSequence> spinnerArrayAdapter;
String name[] = { "Stuff" };
String displayName[] = {
"stuff" };
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment BlankFragment.
*/
// TODO: Rename and change types and number of parameters
public static BlankFragment newInstance(String param1, String param2) {
BlankFragment fragment = new BlankFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public BlankFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
//create a option menu for the current Fragment displayed, add setHasOptionsMenu(true); to the Fragment onCreate() method
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
container.removeAllViews();
//Use View "v" containing the layout and container - use v.findViewById in others
View v = inflater.inflate(R.layout.atcsectionweb2, container, false);
wv = (WebView) v.findViewById(R.id.webview);
pd = (ProgressBar) v.findViewById(R.id.pBar);
spLoadFrom = (Spinner) v.findViewById(R.id.Spinner02);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, displayName);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spLoadFrom.setAdapter(spinnerArrayAdapter);
SpinnerListener spListener = new SpinnerListener();
spLoadFrom.setOnItemSelectedListener(spListener);
// Inflate the layout for this fragment
return v;
}
public class SpinnerListener implements AdapterView.OnItemSelectedListener {
public SpinnerListener() {
}
public void onItemSelected(AdapterView<?> arg0, View arg1,
final int position, long arg2) {
WebView wv = (WebView) getView().findViewById(R.id.webview);
wv.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (progress < 100
&& pd.getVisibility() == ProgressBar.GONE) {
pd.setVisibility(ProgressBar.VISIBLE);
}
pd.setProgress(progress);
if (progress == 100) {
pd.setVisibility(ProgressBar.GONE);
}
}
});
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setSupportZoom(true);
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
wv.loadUrl("file:///android_asset/" + name[position]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
//inflate the corresponding option menu by override the onCreateOptionsMenu() method
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
Log.d("Log", "Inflating Menu");
inflater.inflate(R.menu.blank, menu);
super.onCreateOptionsMenu(menu, inflater);
}
//handling menu selections override the onOptionsItemSelected()
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mySearch:
Log.d("Log", "Implementing Search Method");
search();
return true;
case R.id.action_settings:
//do something
return true;
}
return false;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void search() {
if (Build.VERSION.SDK_INT >= 11) {
if (wv != null) {
Log.d("Log", "Search Method");
wv.showFindDialog(null, true);
}
} else {
Toast.makeText(getActivity(),"Search is only supported on Android version 4.0 and higher.",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
}