布局文件中:
<TextView android:id="@+id/myTextView" android:layout_gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:ellipsize="marquee" android:focusable="true" android:marqueeRepeatLimit="marquee_forever" android:singleLine="true" android:textSize="24sp" android:text="北京八维研修学院是经北京市教委批准的以IT工程专业为主,集职业性高等教育、职业技能认证等教育为一体的全日制现代职业教育高等院校,民办非学历高等教育机构。" android:textColor="@android:color/black" > </TextView>
代码:
package com.example.a05_horse_race_lamp; import android.annotation.SuppressLint; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import java.util.Random; public class MainActivity extends AppCompatActivity { @SuppressLint("HandlerLeak") private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what == 0){ Random random = new Random(); int red = random.nextInt(255); int green = random.nextInt(255); int blue = random.nextInt(255); textView.setTextColor(Color.rgb(red, green, blue)); handler.sendEmptyMessageDelayed(0, 200); } } }; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.myTextView); textView.setSelected(true); textView.setTextColor(Color.rgb(100, 100, 100)); handler.sendEmptyMessageDelayed(0, 200); } }