簡體   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