繁体   English   中英

在android中存储URL的最佳方式

[英]Best way to store URLs in android

我有一个相当基本的页面,其中包含几个我希望在浏览器中启动的外部网站按钮。 我不确定的是我应该如何存储页面的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中)直接定义按钮)。

任何人都可以指出我实现这一目标的正确方法吗?

非常感谢

您可以使用标签:

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()

暂无
暂无

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

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