android eclipse文本文件颜色根据值变化

标签 android eclipse

嘿,我正在尝试为 android 创建一个红绿灯系统,但运气不佳

你看,我希望文本根据其中的值进行更改

但我也从我之前创建的预先存在的文本文件中获取这些值 如果值为 0 到 2,我希望文本文件为绿色;如果值为 2 到 6,则为琥珀色;如果值为更高,则为红色

任何想法谢谢 这是我使用的代码 公共(public)类 View1 扩展 Activity {

/** 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);
        }
    }}

我的布局文件 View

<?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, 降雨量:5cm

日期:22/1/2012 时间:12:50, 降雨量:4cm

最佳答案

下面是一小段代码,可能会对您有所帮助。我将使它成为一个基于 EditText 的文本计数器。

首先你必须在 Activity 中声明你的textview

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

所以在您的主要 Activity 中,您需要:

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,但希望它能让您走上正轨。

关于android eclipse文本文件颜色根据值变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8942304/

相关文章:

android - doInBackground 运行时是否可以停止 asynctask?

Android 重新连接调试器

java - Google Glass 的 Hello World 程序 - 一步一步

java - 在android studio的所有activity中保留账户信息

Android重新显示来自单独 fragment 的dialogfragment

android - 带有 ADT 的 Eclipse - adb.exe 即使在 IDE 退出后仍继续运行

eclipse - 安装Maven Integration for Eclipse WTP时出错

java - Eclipse:抑制, "cannot be resolved to a type"

java - 如何使用 -e 开关从 eclipse 运行 maven

android - 选项卡更改时保留以前加载的 fragment 实例,而不是在 android 中创建新 fragment 实例