1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:tools="http://schemas.android.com/tools"
4 android:layout_width="match_parent"
5 android:layout_height 6 tools:context=".MainActivity"
7 android:orientation="vertical">
8
9 TextView
10 android:text="请输入argb值:"
11 android:textSize="20sp"
12 android:textColor="#000"
13 android:layout_width14 android:layout_height="wrap_content"
15 android:layout_margin="10dp"/>
16
17 LinearLayout
18 android:layout_width19 ="50dp"
20 android:orientation="horizontal"21 EditText
22 android:id="@+id/etA"
23 android:layout_width="0dp"
24 android:layout_weight="1"
25 android:layout_height26 android:layout_margin="1dp"
27 android:hint="透明度(0-255)"
28 android:inputType="number"29
30 31 ="@+id/etR"
32 ="红(0-255)"
33 34 35 36 android:background="#f00"
37 38 android:gravity="center"
39 ="number"40 </LinearLayout41 42 43 44 45 46 ="@+id/etG"
47 ="绿(0-255)"
48 49 50 51 ="#0f0"
52 53 54 55
56 57 ="@+id/etB"
58 ="蓝(0-255)"
59 60 61 62 ="#00f"
63 64 65 66 67
68 69 ="@+id/tv_show"
70 android:text="TextView"
71 ="200dp"
72 73 android:background74 android:layout_gravity75 android:layout_marginTop="20dp"
76 77
78 Button
79 ="@+id/btn"
80 ="确定配置"
81 82 83 ="wrap_content" 84 >
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
2
3 private EditText etA;
4 EditText etR;
5 EditText etG;
6 EditText etB;
7 TextView tvShow;
8 Button btn;
9
10 @Override
11 protected void onCreate(Bundle savedInstanceState) {
12 super.onCreate(savedInstanceState);
13 setContentView(R.layout.activity_main);
14
15 initView();
16 }
17
18 private initView() {
19 etA = (EditText) findViewById(R.id.etA);
20 etR = (EditText) findViewById(R.id.etR);
21 etG = (EditText) findViewById(R.id.etG);
22 etB = (EditText) findViewById(R.id.etB);
23 tvShow = (TextView) findViewById(R.id.tv_show);
24 btn = (Button) findViewById(R.id.btn);
25
26 btn.setOnClickListener(this);
27 28
29 30 onClick(View v) {
31 switch (v.getId()) {
32 case R.id.btn:
33 submit();
34 break;
35 }
36 37
38 submit() {
39 // validate
40 if (!etA.getText().equals("")&&!etB.getText().equals("")
41 &&!etR.getText().equals("")&&!etG.getText().equals("")) {
42 对用户输入的数值进行判断是否为空。避免空字符无法转换为int异常
43 int et_a = Integer.parseInt(etA.getText().toString());
44 int et_r = Integer.parseInt(etR.getText().toString());
45 int et_g = Integer.parseInt(etG.getText().toString());
46 int et_b = Integer.parseInt(etB.getText().toString());
47 tvShow.setBackgroundColor(Color.argb(et_a,et_r,et_g,et_b));
48 }else {
49 Toast.makeText(this,"输入的值不能为空",Toast.LENGTH_SHORT).show();
50 51 52 }