Skip to content

Instantly share code, notes, and snippets.

@SZooo
Created August 11, 2014 11:17
Show Gist options
  • Save SZooo/1b4d231b4f9ca6f07367 to your computer and use it in GitHub Desktop.
Save SZooo/1b4d231b4f9ca6f07367 to your computer and use it in GitHub Desktop.
变换颜色小试
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}">
<TextView
android:id="@+id/tv"
android:textSize="40dip"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="改变字体颜色"
/>
</LinearLayout>
public class MainActivity extends Activity {
private Button btnn;
private TextView tvv;
private int[] colorArray;
private int colorNum;
private String strColor[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnn = (Button) findViewById(R.id.btn);
tvv = (TextView) findViewById(R.id.tv);
colorArray = new int[]{Color.YELLOW, Color.BLUE, Color.GREEN};
strColor = new String[]{"黄色", "蓝色", "绿色"};
colorNum = 0;
btnn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
if (colorNum < colorArray.length) {
tvv.setTextColor(colorArray[colorNum]);
Toast toast = Toast.makeText(MainActivity.this, "此时文字颜色为" + strColor[colorNum], Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 150);
toast.show();
colorNum++;
} else colorNum = 0;
}
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment