Here you will get android webview example.
在这里,您将获得android webview示例。
Android WebView is a UI component used to open web url or show html data inside the activity.
Android WebView是一个UI组件,用于打开Web URL或在活动中显示html数据。
Below I have given two examples, one is for opening a web url and another is for showing some html data in an activity.
下面我给出了两个示例,一个示例用于打开Web URL,另一个示例用于显示活动中的一些html数据。
Also Read: Android Load Image from URL (Internet) Example
另请参阅: Android从URL加载图像(Internet)示例
Android WebView示例 (Android WebView Example)
开启网址 (Open Web URL)
For opening a url we use loadURL() method of WebView class. By default the url is opened in android browser. As we have to open it inside the activity so we have to provide our own WebViewClient.
为了打开URL,我们使用WebView类的loadURL()方法。 默认情况下,网址是在android浏览器中打开的。 由于我们必须在活动中打开它,因此我们必须提供自己的WebViewClient。
In this example the activity consists of three UI components namely EditText, Button and WebView. When user will enter url of a website in the edittext and click on button then the website will open in webview.
在此示例中,活动包括三个UI组件,即EditText,Button和WebView。 当用户在编辑文本中输入网站的网址并单击按钮时,该网站将在webview中打开。
Create an android project with package name thecrazyprogrammer.androidexample and add following code in respective files.
创建一个程序包名为thecrazyprogrammer.androidexample的android项目,并在相应文件中添加以下代码。
activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/url"
android:hint="Enter the url to open..."/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open"
android:id="@+id/openBtn"/>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"/>
</LinearLayout>
MainActivity.java
MainActivity.java
package thecrazyprogrammer.androidexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
WebView webView;
Button openBtn;
EditText url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView)findViewById(R.id.webView);
openBtn = (Button)findViewById(R.id.openBtn);
url = (EditText)findViewById(R.id.url);
openBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
webView.setWebViewClient(new CustomWebClient());
webView.loadUrl(url.getText().toString());
}
});
}
public class CustomWebClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Note: As we are fetching data from internet so we have to provide internet access permission. Just add following line in AndroidManifest.xml file.
注意:由于我们正在从Internet上获取数据,因此我们必须提供Internet访问权限。 只需在AndroidManifest.xml文件中添加以下行。
<uses-permission android:name="android.permission.INTERNET"/>
Output
输出量
显示HTML数据 (Show HTML Data)
The loadData() method of WebView class is used to show html data. It can be done in following way.
WebView类的loadData()方法用于显示html数据。 可以通过以下方式完成。
Create an android project with package name thecrazyprogrammer.androidexample and add following code in respective files.
创建一个程序包名为thecrazyprogrammer.androidexample的android项目,并在相应文件中添加以下代码。
activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"/>
</LinearLayout>
MainActivity.java
MainActivity.java
package thecrazyprogrammer.androidexample;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView)findViewById(R.id.webView);
webView.loadData("<h1>Android WebView Example</h1>","text/html","UTF-8");
}
}
Output
输出量
Comment below if you found anything incorrect or have doubts related to above android webview example.
如果发现任何不正确的内容或与上述android webview示例相关的疑问,请在下面评论。
翻译自: https://www.thecrazyprogrammer.com/2016/07/android-webview-example.html