MainActivity布局:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/myTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
<ListView
android:id="@+id/myListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
MainActivity:
public class MainActivity extends Activity {
private ListView mListView1;
private TextView mTextView1;
private String[] myFavor;
private String myUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView1 = (ListView) findViewById(R.id.myListView1);
mTextView1 = (TextView) findViewById(R.id.myTextView1);
mTextView1.setText(getResources().getString(R.string.hello));
myFavor = new String[]{
getResources().getString(R.string.str_list_url1),
getResources().getString(R.string.str_list_url2),
getResources().getString(R.string.str_list_url3)
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, myFavor);
mListView1.setAdapter(adapter);
mListView1.setItemsCanFocus(true);
mListView1.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mListView1.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (parent.getAdapter().getItem(position).toString() == myFavor[0].toString()) {
myUrl = getResources().getString(R.string.str_url1);
goToUrl(myUrl);
} else if (parent.getAdapter().getItem(position).toString() == myFavor[1].toString()) {
myUrl = getResources().getString(R.string.str_url2);
goToUrl(myUrl);
} else if (parent.getAdapter().getItem(position).toString() == myFavor[2].toString()) {
myUrl = getResources().getString(R.string.str_url3);
goToUrl(myUrl);
} else {
mTextView1.setText("出错了!");
}
}
});
}
private void goToUrl(String url){
String data = url;
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("extra_data",data);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/myWebView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/black"
android:focusable="false"
></WebView>
</LinearLayout>SecondActivity:
public class SecondActivity extends Activity{
private WebView mWebView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mWebView1 = (WebView) findViewById(R.id.myWebView1);
mWebView1.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
Intent intent = getIntent();
String data = intent.getStringExtra("extra_data");
mWebView1.loadUrl(data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_second, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">WebTest</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="hello">启动</string>
<string name="str_list_url1">网站1</string>
<string name="str_list_url2">网站2</string>
<string name="str_list_url3">网站3</string>
<string name="str_url1">http://www.baidu.com</string>
<string name="str_url2">http://www.sohu.com</string>
<string name="str_url3">http://www.lyrunner.cn</string>
<string name="title_activity_second">SecondActivity</string>
<string name="title_activity_third">ThirdActivity</string>
</resources>
本文详细介绍了在Android应用中使用WebView组件加载并显示固定网页的步骤,包括设置WebView、加载URL以及处理权限请求等关键操作。
1730

被折叠的 条评论
为什么被折叠?



