WebView 的button多操作

來源:互聯網
上載者:User

第一次寫部落格,不會寫呢,我能力不大,望各位前輩多指教!

(不知道怎麼,就不了)

                            APP  2012-11-16

package gg.demo.web;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**不知道這個算不算是???
 * 
 * 這個思路方向可以考慮,剩下的你看看。
 * 
 * 
 * @author APP
 *
 */
public class MainActivity extends Activity implements OnClickListener {

MyWeb my;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

my = (MyWeb) this.findViewById(R.id.webView);

this.findViewById(R.id.one).setOnClickListener(this);
this.findViewById(R.id.two).setOnClickListener(this);
this.findViewById(R.id.three).setOnClickListener(this);

my.setWebViewClient(new WebViewClient() {

public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);

view.loadUrl(url);

return true;
}

});
}

String tag = "System.out";

String str1 = "http://www.baidu.com";
String str2 = "http://www.163.com";
String str3 = "http://www.csdn.net";

public void onClick(View v) {
switch (v.getId()) {
case R.id.one:
my.load(MainActivity.this, str1);

break;
case R.id.two:

my.load(MainActivity.this, str2);

break;
case R.id.three:

my.load(MainActivity.this, str2);

break;

}
}
}

重寫webView.

package gg.demo.web;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.WebView;

@SuppressLint("NewApi")
public class MyWeb extends WebView {

private ArrayList<WebView> webs = new ArrayList<WebView>();

private MyWeb web = this;

public MyWeb(Context context) {
super(context);
}

public MyWeb(Context context, AttributeSet attrs) {
super(context, attrs);
}

public MyWeb(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public MyWeb(Context context, AttributeSet attrs, int defStyle,
boolean privateBrowsing) {
super(context, attrs, defStyle, privateBrowsing);
}

private int flag = 0;

public void load(Context context, String url) {
WebView w = this;
webs.add(w);
w.setTag(flag);
flag++;

w.loadUrl(url);
}

public void stop(View v) {
int i = Integer.parseInt(v.getTag().toString());
WebView webView = webs.get(i);
webView.stopLoading();
}

public void stopOthers(View v) {
int j = Integer.parseInt(v.getTag().toString());

for (int k = 0; k < webs.size(); k++) {
if (j != k) {
WebView we = webs.get(k);
we.stopLoading();
}
}
}

}

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

    <LinearLayout
        android:id="@+id/top"
        android:layout_width="fill_parent"
        android:layout_height="45dip"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/one"
            android:layout_width="wrap_content"
            android:layout_height="45dip"
            android:text="button-1" />

        <Button
            android:id="@+id/two"
            android:layout_width="wrap_content"
            android:layout_height="45dip"
            android:text="button-22" />

        <Button
            android:id="@+id/three"
            android:layout_width="wrap_content"
            android:layout_height="45dip"
            android:text="button-333" />
    </LinearLayout>

    <gg.demo.web.MyWeb
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/top" />

</RelativeLayout>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.