<Android考证之实训项目二>选择颜色 activity之间数据回传

项目截图

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".MainActivity">
 8 
 9     <TextView
10         android:id="@+id/textView"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="大家好,祝我们成功!"
14         android:textSize="24sp"
15         app:layout_constraintBottom_toBottomOf="parent"
16         app:layout_constraintLeft_toLeftOf="parent"
17         app:layout_constraintRight_toRightOf="parent"
18         app:layout_constraintTop_toTopOf="parent" />
19 
20     <Button
21         android:id="@+id/button"
22         android:layout_width="wrap_content"
23         android:layout_height="wrap_content"
24         android:text="改变字体颜色"
25         app:layout_constraintBottom_toBottomOf="parent"
26         app:layout_constraintEnd_toEndOf="parent"
27         app:layout_constraintHorizontal_bias="0.498"
28         app:layout_constraintStart_toStartOf="parent"
29         app:layout_constraintTop_toTopOf="parent"
30         app:layout_constraintVertical_bias="0.6" />
31 
32 </android.support.constraint.ConstraintLayout>
activitty_main.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".ColorActivity">
 8 
 9     <ListView
10         android:id="@+id/listView"
11         android:layout_width="match_parent"
12         android:layout_height="match_parent" />
13 </android.support.constraint.ConstraintLayout>
activity_color.xml
 1 package com.example.selectcolor;
 2 
 3 import android.content.Intent;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.AdapterView;
 8 import android.widget.ArrayAdapter;
 9 import android.widget.ListView;
10 import android.widget.Toast;
11 
12 public class ColorActivity extends AppCompatActivity {
13 
14     private ListView listView;
15 
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_color);
20         String[] data = {"红色","绿色","蓝色","黑色","灰色"};
21         listView = (ListView)findViewById(R.id.listView);
22         final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, data);
23         listView.setAdapter(arrayAdapter);
24 
25         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
26             @Override
27             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
28                 Intent intent = new Intent();
29                 String Color = arrayAdapter.getItem(position);
30                 intent.putExtra("color",Color);
31                 setResult(1,intent);
32                 finish();
33             }
34         });
35     }
36 }
ColorActivity.java
 1 package com.example.selectcolor;
 2 
 3 import android.content.Intent;
 4 import android.graphics.Color;
 5 import android.support.annotation.Nullable;
 6 import android.support.v7.app.AppCompatActivity;
 7 import android.os.Bundle;
 8 import android.view.View;
 9 import android.widget.Button;
10 import android.widget.EditText;
11 import android.widget.TextView;
12 
13 public class MainActivity extends AppCompatActivity {
14 
15     private Button button;
16     private TextView textView;
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21         button = (Button)findViewById(R.id.button);
22         textView = (TextView)findViewById(R.id.textView);
23         button.setOnClickListener(new View.OnClickListener() {
24             @Override
25             public void onClick(View v) {
26                 Intent intent = new Intent(MainActivity.this, ColorActivity.class);
27                 startActivityForResult(intent,1);
28             }
29         });
30     }
31 
32     @Override
33     protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
34         super.onActivityResult(requestCode, resultCode, data);
35         if(requestCode==1 && resultCode==1){
36             String color = data.getStringExtra("color");
37             switch (color){
38                 case "红色":
39                     textView.setTextColor(Color.RED);
40                     break;
41                 case "绿色":
42                     textView.setTextColor(Color.GREEN);
43                     break;
44                 case "蓝色":
45                     textView.setTextColor(Color.BLUE);
46                     break;
47                 case "黑色":
48                     textView.setTextColor(Color.BLACK);
49                     break;
50                 case "灰色":
51                     textView.setTextColor(Color.GRAY);
52                     break;
53             }
54         }
55     }
56 }
MainActivity.java

 

posted @ 2019-05-27 11:29  机电小白  阅读(190)  评论(0编辑  收藏  举报