1

我有一个 listView 和一个自定义适配器,并且我为文本视图创建了一个 onclicklistener,现在我想在 webview 中打开该文本视图的链接,我该怎么做?

4

1 回答 1

0

经过大量研究,我找到了答案,这真的很容易。您必须在自定义适配器类中设置 onclickListener,仅此而已,并使用 intent.putextra(); 让一个类显示 webview 向该类发送字符串。我显示 webview 的课程是这个

public class Main extends Activity {

WebView myWebView;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);

Intent in = getIntent();
String url = in.getStringExtra(("url"));
myWebView = (WebView) findViewById(R.id.webview);

myWebView.getSettings().setBlockNetworkImage(true);

myWebView.getSettings().setBlockNetworkLoads(true);


myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setPluginState(PluginState.ON);
myWebView.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
myWebView.loadUrl(url);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
    myWebView.goBack();
    return true;
    }
    return super.onKeyDown(keyCode, event);
    }
class MyWebViewClient extends WebViewClient {
      @Override
      // show the web page in webview but not in web browser
       public boolean shouldOverrideUrlLoading(WebView view, String url) {
        //1 option 
         getApplicationContext().startActivity(
         new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        // 2 option
        view.loadUrl(url);
            return true;
      }
    }
    }
于 2013-05-01T12:06:58.510 回答