简体   繁体   English

在android中存储URL的最佳方式

[英]Best way to store URLs in android

I have a fairly basic page which contains several buttons external websites which I want to be launched in the browser. 我有一个相当基本的页面,其中包含几个我希望在浏览器中启动的外部网站按钮。 What I am not sure is exactly how I should store the URL of the pages. 我不确定的是我应该如何存储页面的URL。

I am quite new to Android, so if I am taking the wrong approach please guide me. 我对Android很新,所以如果我采取错误的方法,请指导我。

I have several buttons like so: 我有几个按钮,如下:

<Button
    style="?android:attr/buttonBarButtonStyle"
    android:onClick="openUrlFromButton"
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"             
    android:layout_weight=".3" 
    android:gravity="center_horizontal"
    android:id="@+id/btn_contact_page"
    android:text="@string/contact_page" />

My callback is like so: 我的回调是这样的:

public void openUrlFromButton(View view){       
    Uri uriUrl;

    switch(view.getId()){
        case R.id.btn_contact_facebook:
            uriUrl = Uri.parse("http://google.com/"); 
        default:
            uriUrl = Uri.parse("http://stackoverflow.com/"); 
    }
    Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
    startActivity(launchBrowser);       
}

What I am unsure about, is how would be best to store the URL. 我不确定的是,如何存储URL最好。

I'd like to be able to directly reference it in the same format as the ID used to access the button (ie, from the view), or at least a consistent way that isnt hard coded in the callback (store it in the XML defining the button directly). 我希望能够以与用于访问按钮的ID(即,从视图中)相同的格式直接引用它,或者至少在回调中不是硬编码的一致方式(将其存储在XML中)直接定义按钮)。

Can anyone point me to a proper way of achieving this? 任何人都可以指出我实现这一目标的正确方法吗?

Many thanks 非常感谢

You can use a tag: 您可以使用标签:

Tags

Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.

Use 采用

android:tag="http://www.google.com"

in your Button, and get it in your method via 在你的Button中,通过你的方法获取它

(String)view.getTag()

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

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