我是靠谱客的博主 饱满音响,这篇文章主要介绍有道词典案例,现在分享给大家,希望可以做个参考。

运行效果图
此案例使用了SearchView控件和WebView控件。

1、SearchView是搜索框组件,它可以让用户在文本框里输入文字,通过监听器取得用户的输入,当用户点击搜索时,监听器执行实际的搜索。


2、SearchView组件的常用方法如下:

①setIconifiedByDefault(boolean iconified) ===> 设置搜索框默认是否自动缩小为图标。
②setOnQueryTextListener(SearchView,OnQueryTextListener listener) ===> 为搜索框设置监听器
③setSubmitButtonEnabled(boolean enabled) ===> 设置是否显示搜索按钮
④setQueryHint(CharSequence hint) ===> 设置搜索框内的默认显示的提示文本

布局文件activity_main.xml
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context="bzu.edu.cn.happydirectory.MainActivity"> <SearchView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/search"/> <!-- 建立一個WebView --> <WebView android:id="@+id/myWebView1" android:layout_height="match_parent" android:layout_width="match_parent" android:background="@android:color/black" android:focusable="false" /> </LinearLayout>
界面交互代码MainActivity
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package bzu.edu.cn.happydirectory; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.SearchView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { //SearchView申明 private SearchView searchView; //加载数据的WebView申明 private WebView mWebView1; String strURL = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获得布局的几个控件 searchView = (SearchView) findViewById(R.id.search); mWebView1 = (WebView) findViewById(R.id.myWebView1); searchView.setIconifiedByDefault(true);//设置搜索框默认是否自动缩小为图标。 searchView.setSubmitButtonEnabled(true);//设置是否显示搜索按钮 searchView.setQueryHint("input");//设置搜索框内的默认显示的提示文本 searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { strURL = searchView.getQuery().toString().trim();//获取输入文本 if (strURL.length() == 0) { Toast.makeText(MainActivity.this, "查询内容不能为空!", Toast.LENGTH_LONG).show(); } else { strURL = "http://dict.youdao.com/m/search?keyfrom=dict.mindex&q=" + strURL; mWebView1.loadUrl(strURL); // 设置支持JavaScript脚本 mWebView1.getSettings().setJavaScriptEnabled(true); //禁止浏览器打开页面 mWebView1.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); } return false; } @Override public boolean onQueryTextChange(String newText) { return false; } }); } }



最后

以上就是饱满音响最近收集整理的关于有道词典案例的全部内容,更多相关有道词典案例内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部