Android入门-网页展示(1)


目标效果


基本要求和基础知识和上一篇相同

1、新建项目YouDaoDictionary

2、在activity_main.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"
    android:background="@drawable/mainbg"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <EditText
        android:id="@+id/etWord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="27dp"
        android:background="@android:drawable/edit_text"
        android:ems="10"
        android:singleLine="true"
        android:textColor="#552006"
        android:textColorHint="#782f10" >
        <requestFocus />
    </EditText>
    <WebView
        android:id="@+id/wvSearchResult"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignLeft="@+id/etWord"
        android:layout_below="@+id/etWord"
        android:layout_marginTop="22dp"
        android:background="@drawable/bg_roundcorner"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="25sp" />

    <Button
        android:id="@+id/btnSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/wvSearchResult"
        android:layout_alignParentRight="true"
        android:background="@drawable/ibsearchword"
        android:onClick="searchWord" />

</RelativeLayout>
3、MainActivity.java中代码

public class MainActivity extends Activity {
	private EditText etWord;
	private WebView wvResult;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initViews();
	}

	private void initViews() {
		etWord = (EditText) findViewById(R.id.etWord);
		wvResult = (WebView) findViewById(R.id.wvSearchResult);
	    wvResult.getSettings().setJavaScriptEnabled(true);
		wvResult.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void searchWord(View view) {
		String word = etWord.getText().toString();
		Toast.makeText(this, "word="+word, Toast.LENGTH_LONG).show();
		if (TextUtils.isEmpty(word)) {
			Toast.makeText(this, "查询内容不能为空!", Toast.LENGTH_LONG).show();
		} else {

			final String strUrl = "http://dict.youdao.com/m/search?keyfrom=dict.mindex&vendor=&q="
					+ word;
			wvResult.loadUrl(strUrl);
		}
	}
}
5、同时不要忘记AndroidManifest.xml中

 </application>
    <uses-permission android:name="android.permission.INTERNET" />

</manifest>
项目完成




已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页