WebView 的button多操作

来源:互联网 发布:软件销售服务协议 编辑:程序博客网 时间:2024/06/01 23:22


第一次写博客,不会写呢,我能力不大,望各位前辈多指教!

(不知道怎么上图,就不上图了)


                            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>

原创粉丝点击