WebViewを使用して簡単なブラウザを作成する
Make Simple Browser Using Webview
Androidでは、組み込みのブラウザーを使用するには、WebViewコンポーネントを介して実装する必要があります。コアは、オープンソースのWebKitエンジンです。
WebViewは、Webページを閲覧するために特別に使用されます。タグを使用してXMLファイルに追加することも、newキーワードを使用してJavaファイルに作成することもできます。 XMLメソッドを使用することをお勧めします。
WebViewの最も単純なアプリケーションは、レイアウトファイルでWebViewコンポーネントを定義し、プログラムコードでコンポーネントをインスタンス化し、そのloadUrlメソッドを呼び出して、アクセスする必要のあるアドレスを渡すことです。
ステップ:
1.レイアウトファイルでWebViewを定義します
2.WebViewをインスタンス化します
WebView webview =(WebView)findviewbyId(R.id.xxx)
webview.loadUrl( ' www.baidu.com ')
3.マニフェストファイルに権限を含めることを忘れないでください
これは、ブラウザの面白くて簡単な例です。
WebGo.javaコード:
(GridViewでは、詳細なGridViewコードは提供されなくなりました)
パブリッククラスWebGoはActivity {を拡張します
public static String flag = 'id'
GridViewグリッドビュー
int [] p_w_picpaths = new int [] {R.drawable.neihanshequ、R.drawable.wangyi、R.drawable.kuailemahua、R.drawable.baidu、R.drawable.taobao、R.drawable.kuaidi、
R.drawable.pengfu、R.drawable.shenmasousuo、R.drawable.manhua、R.drawable.xiaohua、R.drawable.wenti、R.drawable.jiuyou、R.drawable.jiuyou、R.drawable.yxlm、R。 drawable.logo17173}
String [] titles = new String [] {'Connotation'、 'Netease News'、 'Happy Twist'、 'Baidu'、 'Walk in Taobao'、 'Check Express'、 'Waiqiu'、 'God Horse Search' '最も人気のある漫画 '、'クラシック面白い '、'ブレインストーム '、'今日の新しい服 '、'無料スタンドアロン '、'ゲーム情報 '、' 17173ホームページ '}
文字列string1 = 'http://m.neihanshequ.com'
文字列string2 = 'http://3g.163.com/touch/'
文字列string3 = 'http://m.mahua.com'
文字列string4 = 'http://m.baidu.com/?from = 2001a'
文字列string5 = 'http://m.taobao.com'
文字列string6 = 'http://m.kuaidi100.com'
文字列string7 = 'http://m.pengfu.com'
文字列string8 = 'http://m.sa.sm.cn'
文字列string9 = 'http://m。dmzj。com'
文字列string10 = 'http://info.3g.qq.com'
文字列string11 = 'http://m.xuexila。com/ jzw /'
文字列string12 = 'http://a.9game.cn/xinfu/'
文字列string13 = 'http://a.9game.cn/danji2/'
文字列string14 = 'http://m.baidu.com'
文字列string15 = 'http://m.17173.com'
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState)
setContentView(R.layout.webgo)
gridview =(GridView)findViewById(R.id.gv1)
PictureAdapter2アダプタ=新しいPictureAdapter2(titles、p_w_picpaths、this)
gridview.setAdapter(adapter)
gridview.setOnItemClickListener(new OnItemClickListener(){
@オーバーライド
public void onItemClick(AdapterView arg0、View arg1、int arg2、
long arg3){
インテントintent = null
switch(arg2){
ケース0:intent = new Intent(WebGo.this、WebGoOn.class)
intent.putExtra(flag、string1)
startActivity(インテント)
finish()
ブレーク
ケース1:intent = new Intent(WebGo.this、WebGoOn.class)
intent.putExtra(flag、string2)
startActivity(インテント)
finish()
ブレーク
ケース2:intent = new Intent(WebGo.this、WebGoOn.class)
intent.putExtra(flag、string3)
startActivity(インテント)
finish()
ブレーク
ケース3:intent = new Intent(WebGo.this、WebGoOn.class)
intent.putExtra(flag、string4)
startActivity(インテント)
finish()
ブレーク
ケース4:intent = new Intent(WebGo.this、WebGoOn.class)
intent.putExtra(flag、string5)
startActivity(インテント)
finish()
ブレーク
ケース5:intent = new Intent(WebGo.this、WebGoOn.class)
intent.putExtra(flag、string6)
startActivity(インテント)
finish()
ブレーク
ケース6:intent = new Intent(WebGo.this、WebGoOn.class)
intent.putExtra(flag、string7)
startActivity(インテント)
finish()
ブレーク
ケース7:intent = new Intent(WebGo.this、WebGoOn.class)
intent.putExtra(flag、string8)
startActivity(インテント)
finish()
ブレーク
ケース8:intent = new Intent(WebGo.this、WebGoOn.class)
intent.putExtra(flag、string9)
startActivity(インテント)
finish()
ブレーク
ケース9:intent = new Intent(WebGo.this、WebGoOn.class)
intent.putExtra(flag、string10)
startActivity(インテント)
finish()
ブレーク
ケース10:intent = new Intent(WebGo.this、WebGoOn.class)
intent.putExtra(flag、string11)
startActivity(インテント)
finish()
ブレーク
ケース11:intent = new Intent(WebGo.this、WebGoOn.class)
intent.putExtra(flag、string12)
startActivity(インテント)
finish()
ブレーク
ケース12:intent = new Intent(WebGo.this、WebGoOn.class)
intent.putExtra(flag、string13)
startActivity(インテント)
finish()
ブレーク
ケース13:intent = new Intent(WebGo.this、WebGoOn.class)
intent.putExtra(flag、string14)
startActivity(インテント)
finish()
ブレーク
ケース14:intent = new Intent(WebGo.this、WebGoOn.class)
intent.putExtra(flag、string15)
startActivity(インテント)
finish()
ブレーク
}
}
})
}
public boolean onKeyDown(int KeyCode、KeyEvent Event){
if(KeyCode == KeyEvent.KEYCODE_BACK){
インテントインテント=新しいインテント(WebGo.this、MainActivity.class)
startActivity(インテント)
finish()
}
trueを返す
}
}
WebGoOn.javaコード:
パブリッククラスWebGoOnはActivity {を拡張します
プライベートWebViewwebview
プライベートImageButtonp_w_picpathbutton1、p_w_picpathbutton2、p_w_picpathbutton3
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState)
setContentView(R.layout.webgoon)
p_w_picpathbutton1 =(ImageButton)findViewById(R.id.imgbtn1)
p_w_picpathbutton2 =(ImageButton)findViewById(R.id.imgbtn2)
p_w_picpathbutton3 =(ImageButton)findViewById(R.id.imgbtn3)
webview =(WebView)findViewById(R.id.webView1)
webview.getSettings()。setJavaScriptEnabled(true)
webview.setWebChromeClient(新しいWebChromeClient())
webview.setWebViewClient(新しいWebViewClient())
インテントインテント= getIntent()
バンドルbundle = intent.getExtras()
文字列string = bundle.getString(WebGo.flag)
webview.loadUrl(string)
p_w_picpathbutton1.setOnClickListener(new View.OnClickListener(){
@オーバーライド
public void onClick(View arg0){
// TODO自動生成されたメソッドスタブ
webview.goBack()
}
})
p_w_picpathbutton3.setOnClickListener(new View.OnClickListener(){
@オーバーライド
public void onClick(View arg0){
// TODO自動生成されたメソッドスタブ
インテントインテント=新しいインテント(WebGoOn.this、WebGo.class)
startActivity(インテント)
finish()
}
})
p_w_picpathbutton2.setOnClickListener(new View.OnClickListener(){
@オーバーライド
public void onClick(View arg0){
// TODO自動生成されたメソッドスタブ
webview.goForward()
}
})
}
public boolean onKeyDown(int KeyCode、KeyEvent Event){
if(KeyCode == KeyEvent.KEYCODE_BACK){
webview.goBack()
}
trueを返す
}
}
Webgoon.xmlコード:
android:layout_width = 'match_parent'
android:layout_height = 'match_parent'
android:orientation = 'vertical'
android:background = '#cccccc'>
android:layout_width = 'match_parent'
android:layout_height = 'match_parent'>
android:id = '@ + id / webView1'
android:layout_width = 'match_parent'
android:layout_height = 'match_parent'
android:layout_above = '@ + id / imgbtn1' />
android:id = '@ + id / imgbtn1'
android:layout_width = 'wrap_content'
android:layout_height = 'wrap_content'
android:layout_alignParentBottom = 'true'
android:layout_alignParentLeft = 'true'
android:src = '@ drawable / back' />
android:id = '@ + id / imgbtn2'
android:layout_width = 'wrap_content'
android:layout_height = 'wrap_content'
android:layout_alignParentBottom = 'true'
android:layout_alignParentRight = 'true'
android:src = '@ drawable / go' />
android:id = '@ + id / imgbtn3'
android:layout_width = 'wrap_content'
android:layout_height = 'wrap_content'
android:layout_alignParentBottom = 'true'
android:layout_centerHorizontal = 'true'
android:src = '@ drawable / end' />
メインコードはこれらです、効果図は次のとおりです:
とてもシンプルです。
転載:https://blog.51cto.com/bigcrab/1708928