简体   繁体   English

Android通过onclick获取URL

[英]Android get url with onclick

Hello I am trying to get the value of a url with onclick but it is not working for me. 您好,我正在尝试使用onclick获取url的值,但对我而言不起作用。 I am usign the following code. 我使用以下代码。

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.webkit.WebView;


public class MainActivity extends Activity {
String valueOfURL = null;
WebView webView;
Intent browserIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://www.example.com"));
    startActivity(browserIntent);
}

public String handleLinkClicked(String value) {
    if (value.startsWith("http"))  

    valueOfURL = value;
    return valueOfURL;

}
public View.OnClickListener myhandler = new View.OnClickListener() {
    public void onClick(View view) {
    handleLinkClicked(valueOfURL);
    webView.loadUrl("http://docs.google.com/gview?embedded=true&url=valueOfURL");
    }};
}

The first page will load fine but when i click on a pdf on that page it wont load. 第一页将很好地加载,但是当我单击该页面上的pdf时,它将不会加载。 Basically what I am trying to do is get the value of the URL and pass it to a variable that is then used to be read via google docs. 基本上,我想做的是获取URL的值并将其传递给一个变量,该变量随后用于通过Google文档进行读取。 It is a pdf file and its URL looks like this http://wwww.example.com/c/document_library/get_file ? 这是一个pdf文件,其网址看起来像这样http://wwww.example.com/c/document_library/get_file

Can anyone see where I am going wrong and maybe point me in the right direction. 谁能看到我要去哪里错了,也许可以向我指出正确的方向。

Thanking You 感谢您

You did not set your onClickListener() myhandler to any View . 您没有将myhandler onClickListener() myhandler为任何View Your code ( browserIntent ) starts an implicit intent that will automatically start a Web browser (new Activity not linked to your application) to handle your URL. 您的代码( browserIntent )启动一个隐式intent ,该intent将自动启动Web浏览器(未链接到您的应用程序的新Activity )来处理您的URL。 If you need to control the browsing from your App, you should implement a WebView inside your Layout and set your onClickListener to it instead of using implicit intents. 如果需要控制从App的浏览,则应在Layout中实现WebView并将其onClickListener设置为它,而不是使用隐式意图。 Check this link on how Android handles implicit intents 查看此链接 ,了解Android如何处理隐式意图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM