Activity.xml中TextView的代碼
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
package com.example.android_day001;
import java.util.Random;
import android.os.Bundle; import android.app.Activity; import
android.graphics.Color; import android.view.Menu; import
android.view.View; import android.view.View.OnClickListener; import
android.widget.TextView;
public class MainActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); TextView textview;
textview = (TextView) this.findViewById(R.id.textview);
textview.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Random random = new Random(255);
textview.setTextColor(Color.rgb(random.nextInt(),
random.nextInt(), random.nextInt())); } }); }
List item
@Override public boolean onCreateOptiosMenu(Menu menu) { //
Inflate the menu; this adds items to the action bar if it is
present. getMenuInflater().inflate(R.menu.main, menu); return
true; }
}
編輯器在代碼的textview.setTextColor();處報錯
Color.rgb(random.nextInt(), random.nextInt(), random.nextInt())改為Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256))試試