要求
建立一个Button数组,实现一个颜色选择器。
MainActivity
package com.example.hades.demo2;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import static android.R.attr.button;
public class MainActivity extends AppCompatActivity {
EditText et;
TextView tv;
Integer []bts = {R.id.button_0,R.id.button_1,R.id.button_2,R.id.button_3,R.id.button_4,R.id.button_5};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView);
et = (EditText) findViewById(R.id.editText);
et.addTextChangedListener(new changeText());
for(int i=0;i<6;i++){
new button_click(bts[i]);
}
}
private class button_click{
button_click(Integer id){
Button bt;
bt =(Button)findViewById(id);
Drawable background = bt.getBackground();
ColorDrawable colorDrawable = (ColorDrawable) background;
int color = colorDrawable.getColor();
bt.setOnClickListener(new click(color));
}
}
private class changeText implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
tv.setText(s);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
tv.setText(s);
}
@Override
public void afterTextChanged(Editable s) {
}
}
private class click implements View.OnClickListener {
int bt_color;
click(int color){
bt_color = color;
}
public void onClick(View v) {
tv.setTextColor(bt_color);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hades.demo2.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="7dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.04"
android:fontFamily="sans-serif"
android:text="Hello World"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="30sp"
android:typeface="normal" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Hello World" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.04"
android:orientation="horizontal"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="116dp">
<Button
android:id="@+id/button_0"
android:layout_width="35dp"
android:layout_height="40dp"
android:background="@android:color/black"
android:layout_weight="1" />
<Button
android:id="@+id/button_1"
android:layout_width="35dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@android:color/holo_red_dark" />
<Button
android:id="@+id/button_2"
android:layout_width="35dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@android:color/holo_green_dark" />
<Button
android:id="@+id/button_3"
android:layout_width="35dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@android:color/holo_blue_bright" />
<Button
android:id="@+id/button_4"
android:layout_width="35dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@android:color/holo_orange_dark" />
<Button
android:id="@+id/button_5"
android:layout_width="35dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@android:color/holo_blue_light" />
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>