在android中存储URL的最佳方式

时间:2013-08-09 15:10:59

标签: android xml

我有一个相当基本的页面,其中包含几个我希望在浏览器中启动的外部网站按钮。我不确定的是我应该如何存储页面的URL。

我对Android很新,所以如果我采取了错误的方法,请指导我。

我有几个按钮,如下:

<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" />

我的回调是这样的:

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);       
}

我不确定的是,如何存储URL最好。

我希望能够以与用于访问按钮的ID(即,从视图中)相同的格式直接引用它,或者至少在回调中不是硬编码的一致方式(存储它)在XML中直接定义按钮)。

有人能指出我实现这个目标的正确方法吗?

非常感谢

1 个答案:

答案 0 :(得分:2)

您可以使用标签:

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.

使用

android:tag="http://www.google.com"
在你的Button中

,并通过

在你的方法中获取它
(String)view.getTag()
相关问题