Android 访问固定网页

本文详细介绍了在Android应用中使用WebView组件加载并显示固定网页的步骤,包括设置WebView、加载URL以及处理权限请求等关键操作。
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);
    }
}


SecondActivity布局:

<?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>




                
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值