2012-01-20 57 views
2

嘿我試圖創建爲Android交通燈系統,但沒有多少運氣Android的Eclipse文本文件的顏色變化基於價值

你看到我想要的文字來改變基於它裏面

但我也從我以前創建的預先存在的文本文件中獲取這些值 如果值爲0到2,我希望文本文件爲綠色如果其2到6爲黃色,

任何想法謝謝 這是代碼即時通訊使用 公共類Vi EW1延伸活動{

/** Called when the activity is first created. */ 
// @Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.view); 

    readFromFile(); 

    } 


    public void readFromFile() 
    { 
     //put your code in here 
     //you may want to use a buffered reader and check out your bytes! 
     TextView tv1 = new TextView(this); 

     String LOC = "Newcas.txt"; 
     String NAME = LOC; 

     try { 
      FileInputStream fileIn = openFileInput(NAME); 
      InputStreamReader isr = new InputStreamReader(fileIn); 

      char[] inputBuffer = new char[250]; 
      String s =" "; 

      int charRead; 

      while ((charRead = isr.read(inputBuffer))>0) 
      { 
       String readString = String.copyValueOf(inputBuffer, 0, charRead); 
       s += readString; 

       inputBuffer = new char[250]; 
      } 

      tv1.setText(s); 

     } catch (IOException ioe) 
     { 
      ioe.printStackTrace(); 
     } 

     setContentView(tv1); 
    } 
    public class ColorChangingTextView extends TextView { 
     //Default constructor for creating view from layout. You can add the rest if you want. 
     public ColorChangingTextView(Context context, AttributeSet attrs, 
       int defStyle) { 
      super(context, attrs, defStyle); 
     } 
     @Override 
     public void onTextChanged(CharSequence text, int start, int before, int after) { 
      int val = Integer.parseInt(text.toString()); 
      if(val >= 0 && val <= 2) 
       super.setTextColor(Color.GREEN); 
      else if(val >= 2 && val <= 6) 
       super.setTextColor(Color.rgb(255, 126, 0)); //RGB(255, 126, 0) is Amber 
      else if(val >= 6) 
       super.setTextColor(Color.RED); 
     } 
    }} 

我的佈局文件視圖

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<Button 
    android:id="@+id/Loc01" 
    android:layout_width="250dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="50dp" 
    android:text="@string/Location1" /> 

<Button 
    android:id="@+id/Loc02" 
    android:layout_width="250dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="25dp" 
    android:text="@string/Location2" /> 

<Button 
    android:id="@+id/Loc03" 
    android:layout_width="250dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="25dp" 
    android:text="@string/Location3" /> 

<Button 
    android:id="@+id/Loc04" 
    android:layout_width="250dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="25dp" 
    android:text="@string/Location4" /> 

<Button 
    android:id="@+id/Loc05" 
    android:layout_width="250dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="25dp" 
    android:text="@string/Location5" /> 

</LinearLayout> 

,這保險業監督從我的txt文件例如 日期:20/1/2012時間:12:51, 降雨量:5釐米

日期:22/1/2012時間:12:50, 降雨量:4釐米

+0

我以爲你說textfile時,你的意思是一個TextView? – georgiecasey

+0

沒有價值來自現有的文本文件,如上面現在顯示的抱歉關於 –

回答

0

這個類可以做的工作:

public class ColorChangingTextView extends TextView { 
    //Default constructor for creating view from layout. You can add the rest if you want. 
    public ColorChangingTextView(Context context, AttributeSet attrs, 
      int defStyle) { 
     super(context, attrs, defStyle); 
    } 
    @Override 
    public void onTextChanged(CharSequence text, int start, int before, int after) { 
     int val = Integer.parseInt(text.toString()); 
     if(val >= 0 && val <= 2) 
      super.setTextColor(Color.GREEN); 
     else if(val >= 2 && val <= 6) 
      super.setTextColor(Color.rgb(255, 126, 0)); //RGB(255, 126, 0) is Amber 
     else if(val >= 6) 
      super.setTextColor(Color.RED); 
    } 
} 

下面是如何把它的代碼(XML佈局)的例子:

<your.package.name.ColorChangingTextView id="@+id/idForTextView" 
    text="This is some text" 
    .... 
    /> 
+0

抱歉,但我怎麼會把這個到我的代碼,所以它的作品? –

+0

我會舉一個例子。 – Jong

+0

,只是導致文件得到一個錯誤,並沒有打開,iv更新我的代碼,包括我的xml文件,以及如何編輯my.java文件,你會介意看看它,找出什麼是錯的? apoligies爲此,我相當新的androide發展 –

0

的代碼下面有一個小一點,可以幫助你。我將使它成爲基於EditText的文本計數器。

首先你得在活動中聲明你的TextView

TextView txtCount; //txtCount is an arbitrary name 
    EditText smsMessage; //This is the EditText we'll be watching. 

因此,在您的主要活動,你會想:

extends Activity implements TextWatcher 

在OnCreate,你需要這樣的:

txtCount = (TextView) findViewById(R.id.txtCount); // 1 
    txtCount.setText(Integer.toString(140)); // 2 
    txtCount.setTextColor(Color.parseColor("#888888")); // 3 
    smsMessage.addTextChangedListener(this); //4 

1:其中txtCount是的TextView的id

2:這設置文本。您將不得不從文件中提取初始值,或者只設置 之一。

3:更改默認的顏色,可以根據你的情況下使用Color.GREEN。

4:在這種情況下,我們正在關注EditText的更改,但在您的情況下,您將有 來研究如何從文件中提取信息。

後來,你需要添加這些:

public void afterTextChanged(Editable smsMessage) { // 1 
    int count = 140 - smsMessage.length(); //2 
    txtCount.setText(Integer.toString(count)); 
    txtCount.setTextColor(Color.parseColor("#888888"));//3 
    if (count < 0) 
     txtCount.setText("2 SMS"); //4 
    if (count < -140) 
     txtCount.setText("3 SMS"); 

} 

    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

} //5 

    public void onTextChanged(CharSequence s, int start, int before, int count) { 

} //5 

1:可編輯smsMessage是你必須改變,又因爲我們使用一個EditText在這裏檢查狀態。

2:這只是減少顯示的數字

3:更多的顏色變化。

4:在這個if語句中,如果數字低於0,那麼它完全改變。

5:這些都只是強制性的方法,你可以留空

現在,這個代碼是非常混亂的。並處理EditText,但希望它會讓你在正確的軌道上。