簡體   English   中英

通過活動將字符串傳遞給服務

[英]Pass string to a service from activity

好了,所以我有一個活動。 在活動中,有一個textview和一個按鈕。 該按鈕將啟動ColorPicker,選擇顏色后,它將在文本視圖中放置十六進制值。

現在,在一項服務中,我試圖將字符串轉換為color int。 然后將imageview的背景顏色設置為textview的十六進制值。 請參閱下面的示例...

在我的main.xml中,我有一個textview和一個按鈕。 Textview將在其文本中設置十六進制值。


在我的服務中,我有一個imageview。 為了設置imageview的背景色,我從主活動的textview中獲取了文本,然后創建了一個字符串。 然后我將其轉換為Int。 但是當我干燥以將顏色設置為背景時,它將強制關閉!

`BatteryBarTop = (ImageView) view.findViewById(R.id.battery_bar_top);
String tbColor = Setting.ColorValue.getText().toString();
int color = Color.parseColor(tbColor);
BatteryBarTop.setBackgroundResource(color);`

如果我為“顏色”輸入一個十六進制值,它將很好地工作。 但是我需要來自textview的十六進制值是顏色,因為可以在需要時更改它...

您正在調用setBackgroundResource() 這需要一個資源ID。 使用setBackgroundColor()設置顏色。

    BatteryBarTop = (ImageView) view.findViewById(R.id.battery_bar_top);
    String value = Setting.ColorValue.getText().toString();
    int setColor = Integer.parseInt(value);

    try {
        BatteryBarTop.setBackgroundColor(setColor);
    }
    catch (NumberFormatException e)
    {
        // handle the exception
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM