【问题标题】:Opening a Url from OnClickListener Android从 OnClickListener Android 打开一个 URL
【发布时间】:2013-04-22 01:06:27
【问题描述】:

我正在尝试通过 Android 中的 a 按钮打开网页,但无法正常工作。我找到了一个看起来非常简单的教程,但诀窍是我试图显示一个从另一个活动类中创建的对象中获取的 url。我使用的代码是标准的 OnClickListener:

private void addListeneronButton() {
    // TODO Auto-generated method stub
    button1 = (Button) findViewById(R.id.stationHistory);

    button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

          Intent browserIntent = 
                        new Intent(Intent.ACTION_VIEW, Uri.parse(//need help here));
            startActivity(browserIntent);

        }

该项目正在读取气象站列表。我有一个带有适当的 getter/setter 的 Station 类,所以 Url 已经存储了。我只是不知道从这里访问的适当命令。我认为它会像 Uri.parse(station.get_url) 一样简单,但它只会提示我创建局部变量...有人有什么建议吗?

<station>
    <station_id>NFNA</station_id>
    <state>FJ</state>
    <station_name>Nausori</station_name>
    <latitude>-18.05</latitude>
    <longitude>178.567</longitude>
    <html_url>http://weather.noaa.gov/weather/current/NFNA.html</html_url>
    <rss_url>http://weather.gov/xml/current_obs/NFNA.rss</rss_url>
    <xml_url>http://weather.gov/xml/current_obs/NFNA.xml</xml_url>
</station>

<station>
    <station_id>KCEW</station_id>
    <state>FL</state>
            <station_name>Crestview, Sikes Airport</station_name>
    <latitude>30.79</latitude>
    <longitude>-86.52</longitude>
            <html_url>http://weather.noaa.gov/weather/current/KCEW.html</html_url>
            <rss_url>http://weather.gov/xml/current_obs/KCEW.rss</rss_url>
            <xml_url>http://weather.gov/xml/current_obs/KCEW.xml</xml_url>
</station>

<station>
    <station_id>KDTS</station_id>
    <state>FL</state>
            <station_name>Destin, Ft. Walton Beach Airport</station_name>
    <latitude>30.4</latitude>
    <longitude>-86.47</longitude>
            <html_url>http://weather.noaa.gov/weather/current/KDTS.html</html_url>
            <rss_url>http://weather.gov/xml/current_obs/KDTS.rss</rss_url>
            <xml_url>http://weather.gov/xml/current_obs/KDTS.xml</xml_url>
</station>

我将这些解析为带有构造函数/getter/setter 的 Station 类。从先前的活动中,用户选择这些站之一。在下一个活动中,它会显示此 xml 中的名称、id 等元素。但现在我想通过一个按钮打开电台网址,但我不确定如何。

【问题讨论】:

  • 粘贴网址,注意需要在Uri.parse中包含http://https://
  • 所以试着把http://放在前面:http://http://weather.noaa.gov/weather/current/NFNA.html
  • 但是我从上面发布的示例中得到了这些。
  • 我不知道为什么这行不通,但是您是否尝试过创建像 Uri uri = http:// weather.noaa.gov/weather/current/NFNA.html 这样的变量,然后将其放入您的 Intent

标签: java android url webview onclicklistener


【解决方案1】:

我想我明白你说的是现在的问题,虽然你真的没有说得很清楚。你有这个

private void addListeneronButton() {
// TODO Auto-generated method stub
button1 = (Button) findViewById(R.id.stationHistory);

button1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

      Intent browserIntent = 
                    new Intent(Intent.ACTION_VIEW, Uri.parse(station.get_url);
        startActivity(browserIntent);

    }

它告诉你station.get_url 不是一个变量,所以你需要创建一个。 get_url 应该是 get_url() 如果它是一个方法。如果您的班级是Station(大写“S”),那么您可以使用Station.get_url() 访问它。但是,您可能需要传入某个参数来告诉它要获取哪个 url。这是假设它是一个static 方法。如果不是,则需要创建Station 类的实例并调用该方法

Station myStation = new Station();  //pass parameters here to constructor as needed
String address = myStation.get_url();  // object

然后你可以在你的Intent 中使用address

 Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(address));
 startActivity(browserIntent);

如果http:// 没有与url 一起传回,那么它必须在Intent 中使用之前附加,正如wangyif2 已经说明的那样。我希望这是有道理的,但这是我能用所提供的信息做的最好的事情。

【讨论】:

    【解决方案2】:

    在解析 URI 时尝试将 http:// 放在字符串之前:

    private void addListeneronButton() {
       button1 = (Button) findViewById(R.id.stationHistory);
       button1.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View arg0) {
               Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                 Uri.parse(Station.getHtmlUrl("NFNA"));
               startActivity(browserIntent);
           }
    }
    

    站类:

    public static String getHtmlUrl(String station) { 
        //return the full URL here based on station 
        //eg. "http://weather.noaa.gov/weather/current/NFNA.html"
    }
    

    【讨论】:

    • 再检查一下,是你想要的吗?
    猜你喜欢
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    • 2011-06-23
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多