嘿我試圖創建爲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釐米
我以爲你說textfile時,你的意思是一個TextView? – georgiecasey
沒有價值來自現有的文本文件,如上面現在顯示的抱歉關於 –