Android - 将JavaScript代码发送到WebView的内部页面

时间:2016-10-02 08:17:46

标签: javascript android webview

表格中有WebView。我正在使用此代码。

WebView ww = (WebView)findViewById(R.id.webView);
WebSettings webSettings = ww.getSettings();
webSettings.setJavaScriptEnabled(true);
ww.loadUrl("http://www.youtube.com");

我想要的是,当页面加载完毕后,我希望在搜索输入框中设置我的TextView值并开始搜索。

首先,我认为这段代码可行。

webView.loadUrl("javascript: {document.getElementsByID('masthead-search-term').value ='TEST1';};");

但是当我使用这段代码时,我在所有页面中只看到“TEST1”。

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

我认为<h3>'.date("d/m/Y", strtotime($profile->vehicles['cars'][$i]['post_date'])).'</h3> 是您查询所在的textView1

请尝试以下代码

TextView

答案 1 :(得分:0)

webView.loadUrl("javascript: {document.getElementsByID('masthead-search').value ='TEST1';};");

我查看了youtube的页面,看到youtube的搜索栏ID是标头搜索。其次,如果你运行它只会在搜索栏中写下 TEST1

我没有正确理解你的问题,请原谅我,我猜你问是否可以点击该搜索按钮。

webView.loadUrl("javascript: {document.getElementsByID('masthead-search').value ='TEST1'.click();};");

我不是百分百肯定。这是一种尝试和错误的方法。 干杯!

答案 2 :(得分:0)

看起来浏览器会插入到搜索栏中private final String LOG_TAG = MovieFragment.class.getSimpleName(); public ImageListAdapter imageAdapter; String urlPopular = "https://api.themoviedb.org/3/movie/popular?api_key="; String urlRatings = "https://api.themoviedb.org/3/movie/top_rated?api_key="; public MovieFragment() { }; @Override public void onStart() { super.onStart(); FetchMovieData movieTask = new FetchMovieData(); movieTask.execute(urlPopular); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.movie_fragment_main, container, false); GridView gridview = (GridView) rootView.findViewById(R.id.grid_view); Log.d(LOG_TAG, imageAdapter.getCount() +""); gridview.setAdapter(imageAdapter); gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(getActivity(), "" + position, Toast.LENGTH_SHORT).show(); } }); return rootView; } public class ImageListAdapter extends ArrayAdapter { private Context context; private LayoutInflater inflater; private String[] imageUrls; public ImageListAdapter(Context context, String[] imageUrls) { super(context, R.layout.image_item, imageUrls); this.context = context; this.imageUrls = imageUrls; inflater = LayoutInflater.from(context); } @Override public View getView(int position, View convertView, ViewGroup parent) { Log.d(LOG_TAG, imageUrls[position]); if (null == convertView) { convertView = inflater.inflate(R.layout.image_item, parent, false); } Picasso .with(context) .load(imageUrls[position]) .fit() // will explain later .into((ImageView) convertView); return convertView; } } public class FetchMovieData extends AsyncTask<String,Void,String[][]> { private final String LOG_TAG = FetchMovieData.class.getSimpleName(); @Override protected String[][] doInBackground(String... params) { String url = params[0]; if (params.length == 0){ return null; } // Will contain the raw JSON response as a string. String movieJSON = null; try { movieJSON = getMovieInfo(url); } catch (final IOException e) { Log.d(LOG_TAG, "Error closing stream", e); } try{ return getMovieDataFromJSON(movieJSON); } catch (JSONException e){ Log.d(LOG_TAG, e.getMessage(),e); e.printStackTrace(); } //Just in case... return null; } private String getMovieInfo(String myurl) throws IOException{ HttpURLConnection urlConnection = null; InputStream inputStream = null; try{ URL url = new URL(myurl); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.connect(); inputStream = urlConnection.getInputStream(); StringBuffer stringBuffer = new StringBuffer(); if (inputStream == null){ return null; } BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { // Since it's JSON, adding a newline isn't necessary (it won't affect parsing) // But it does make debugging a *lot* easier if you print out the completed // buffer for debugging. stringBuffer.append(line + "\n"); } if (stringBuffer.length() == 0) { // Stream was empty. No point in parsing. return null; } Log.d(LOG_TAG, stringBuffer.toString()); return stringBuffer.toString(); } finally { if(urlConnection != null){ urlConnection.disconnect(); } } } private String[][] getMovieDataFromJSON(String jsonObj) throws JSONException{ JSONObject mainJSON = new JSONObject(jsonObj); final String POSTERS = "poster_path"; final String ORIGINAL_TITLE = "original_title"; final String SYNOPSIS = "overview"; final String USER_RATING = "vote_average"; final String RELEASE_DATE = "release_date"; final String RESULTS = "results"; // Building a String[][] of movie info while: // movie[][0] = Poster path // movie[][1] = Original Title // movie[][2] = Synopsis // movie[][3] = User Rating // movie[][4] = Release Date JSONArray movieIndexArray = mainJSON.getJSONArray(RESULTS); String[][] movie = new String[20][5]; for(int i = 0; i < movieIndexArray.length(); i++) { JSONObject movieObject = movieIndexArray.getJSONObject(i); movie[i][0] = movieObject.getString(POSTERS); movie[i][1] = movieObject.getString(ORIGINAL_TITLE); movie[i][2] = movieObject.getString(SYNOPSIS); movie[i][3] = movieObject.getString(USER_RATING); movie[i][4] = movieObject.getString(RELEASE_DATE); } return movie; } private String[] buildPosterArray(String[][] result){ String[] url = new String[20]; Uri.Builder builtUri = new Uri.Builder(); builtUri.scheme("http"); builtUri.authority("image.tmdb.org"); builtUri.appendPath("t"); builtUri.appendPath("p"); builtUri.appendPath("w185"); for (int i = 0 ; i < result.length ; i++){ builtUri.appendEncodedPath((result[i][0])); builtUri.build(); url[i] = builtUri.toString(); } return url; } protected void onPostExecute(String[][] result){ if(result != null){ String[] posterArray = buildPosterArray(result); imageAdapter = new ImageListAdapter(getActivity(), posterArray); Log.d(LOG_TAG, imageAdapter.getCount() +""); } } } 代码返回的文档值中。为了避免这种情况,您可以将调用包装成函数,如下所示:

javascript:

另外注意,它是javascript:(function(){document.getElementById('masthead-search-term').value ='TEST1';document.getElementById('masthead-search').submit();})() ,而不是getElementById()

P.S。值得注意的是,只需将用户重定向到getElementsByID()网址就可以达到同样的效果。