Activity值传递的一个小练习,不多说直接上代码。

---------------------------XML部分-----------------------------

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:id="@+id/bg_cloor" 4     android:layout_width="match_parent" 5     android:layout_height="match_parent" 6     android:background="#FFFFFF" 7     android:paddingBottom="@dimen/activity_vertical_margin" 8     android:paddingLeft="@dimen/activity_horizontal_margin" 9     android:paddingRight="@dimen/activity_horizontal_margin"10     android:paddingTop="@dimen/activity_vertical_margin"11     tools:context=".MainActivity" >12 13     <Button14         android:id="@+id/button1"15         android:layout_width="match_parent"16         android:layout_height="wrap_content"17         android:layout_alignParentLeft="true"18         android:text="选择背景颜色" />19 20 </RelativeLayout>
 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" > 5  6     <TextView 7         android:id="@+id/textView1" 8         android:layout_width="wrap_content" 9         android:layout_height="wrap_content"10         android:layout_alignParentLeft="true"11         android:layout_alignParentTop="true"12         android:layout_marginLeft="32dp"13         android:layout_marginTop="26dp"14         android:background="#663300"15         android:text="#663300" />16 17     <TextView18         android:id="@+id/textView4"19         android:layout_width="wrap_content"20         android:layout_height="wrap_content"21         android:layout_alignRight="@+id/textView1"22         android:layout_below="@+id/textView1"23         android:layout_marginTop="38dp"24         android:background="#00CC00"25         android:text="#00CC00" />26 27     <TextView28         android:id="@+id/textView5"29         android:layout_width="wrap_content"30         android:layout_height="wrap_content"31         android:layout_alignBaseline="@+id/textView4"32         android:layout_alignBottom="@+id/textView4"33         android:layout_alignRight="@+id/textView2"34         android:background="#FF3366"35         android:text="#FF3366" />36 37     <TextView38         android:id="@+id/textView6"39         android:layout_width="wrap_content"40         android:layout_height="wrap_content"41         android:layout_alignBaseline="@+id/textView5"42         android:layout_alignBottom="@+id/textView5"43         android:layout_alignRight="@+id/textView3"44         android:background="#FFFFFF"45         android:text="#FFFFFF" />46 47     <TextView48         android:id="@+id/textView2"49         android:layout_width="wrap_content"50         android:layout_height="wrap_content"51         android:layout_alignBaseline="@+id/textView1"52         android:layout_alignBottom="@+id/textView1"53         android:layout_centerHorizontal="true"54         android:background="#FFCCCC"55         android:text="#FFCCCC" />56 57     <TextView58         android:id="@+id/textView3"59         android:layout_width="wrap_content"60         android:layout_height="wrap_content"61         android:layout_above="@+id/textView4"62         android:layout_alignParentRight="true"63         android:layout_marginRight="30dp"64         android:background="#FFCC33"65         android:text="#FFCC33" />66 67 </RelativeLayout>

--------------------------java部分----------------------

 1 package com.example.activitydeom; 2  3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.Intent; 6 import android.graphics.Color; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button;10 import android.widget.RelativeLayout;11 12 public class MainActivity extends Activity implements OnClickListener {13 14     Button bt;15     RelativeLayout bgc;16 17     @Override18     protected void onCreate(Bundle savedInstanceState) {19         super.onCreate(savedInstanceState);20         setContentView(R.layout.activity_main);21         bt = (Button) findViewById(R.id.button1);22 23         bt.setOnClickListener(this);24 25     }26 27     @Override28     public void onClick(View arg0) {29         Intent it = new Intent(this, ColorDeom.class);30         startActivityForResult(it, 0);31 32     }33 34     // 重点来了,这个方法就是用来接收值传递35     @Override36     protected void onActivityResult(int requestCode, int resultCode, Intent data) {37         super.onActivityResult(requestCode, resultCode, data);38 39         if (0 == requestCode && RESULT_OK == resultCode) {40             String color = data.getStringExtra("color");41             // 这里利用了java提供的Color方法来转换String→int42             int cc = Color.parseColor(color);43             bgc = (RelativeLayout) findViewById(R.id.bg_cloor);44             bgc.setBackgroundColor(cc);45         }46     }47 }
 1 package com.example.activitydeom; 2  3 import android.app.Activity; 4 import android.content.Intent; 5 import android.graphics.Color; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.TextView;10 11 public class ColorDeom extends Activity implements OnClickListener{12 13     TextView tv1;14     TextView tv2;15     TextView tv3;16     TextView tv4;17     TextView tv5;18     TextView tv6;19     @Override20     protected void onCreate(Bundle savedInstanceState) {21         super.onCreate(savedInstanceState);22         setContentView(R.layout.color);23         tv1=(TextView) findViewById(R.id.textView1);24         tv2=(TextView) findViewById(R.id.textView2);25         tv3=(TextView) findViewById(R.id.textView3);26         tv4=(TextView) findViewById(R.id.textView4);27         tv5=(TextView) findViewById(R.id.textView5);28         tv6=(TextView) findViewById(R.id.textView6);29         tv1.setOnClickListener(this);30         tv2.setOnClickListener(this);31         tv3.setOnClickListener(this);32         tv4.setOnClickListener(this);33         tv5.setOnClickListener(this);34         tv6.setOnClickListener(this);35     }36     37     38     @Override39     public void onClick(View v) {40         41         42         Intent it=new Intent();43         String color="0Xffffff";44         switch (v.getId()) {45         case R.id.textView1:46             color=(String) tv1.getText();47             Color.parseColor((String) tv1.getText());48             it.putExtra("color",color);49             setResult(RESULT_OK, it);50             this.finish();51             break;52         case R.id.textView2:53             color=(String) tv2.getText();54             it.putExtra("color",color);55             setResult(RESULT_OK, it);56             this.finish();57             break;58         case R.id.textView3:59             color=(String) tv3.getText();60             it.putExtra("color",color);61             setResult(RESULT_OK, it);62             this.finish();63             break;64         case R.id.textView4:65             color=(String) tv4.getText();66             it.putExtra("color",color);67             setResult(RESULT_OK, it);68             this.finish();69             break;70         case R.id.textView5:71             color=(String) tv5.getText();72             it.putExtra("color",color);73             setResult(RESULT_OK, it);74             this.finish();75             break;76         case R.id.textView6:77             color=(String) tv6.getText();78             it.putExtra("color",color);79             setResult(RESULT_OK, it);80             this.finish();81             break;82 83         default:84             break;85         }86         87     }88 89 }

效果图如下

Android练习—修改背景颜色_第1张图片
Android练习—修改背景颜色_第2张图片

Android练习—修改背景颜色_第3张图片

总结:

这次的练习本来想得很简单只是一个单纯的启动一个Activity并返回结果但是在返回值如何和转换的问题上纠结了好长时间最后还是班里的大神交给了我们用系统性自带的Color类的方法才得以解决,QAQ在此对大神表示感谢~~

知识点:
* 启动Activity的方式:
* 1、startActivity(...);
* 2、startActivityForResult(...);
*
* 启动一个Activity并返回结果的步骤:
* 1、启动Activity时使用startActivityForResult方法,两个参数为:intent,请求编码
* 2、在被启动的Activity中使用Intent来保存数据,并把intent通过setResult方法设置到返回结果中
* 3、关闭被启动的Activity
* 4、在启动的Activity中重写onActivityResult方法来接收返回结果
* 5、判断请求编码与请求结果标记是否匹配,进行取值

更多相关文章

  1. android粗略获得程序运行时间的方法
  2. TextView英文自动换行解决方法
  3. android-RadioButton背景使用图片
  4. Android实现全屏的方法
  5. [android]布局(容器)简介和使用方法
  6. android 动态控制状态栏显示和隐藏的方法实例
  7. Android文件系统的结构及目录用途、操作方法 整理
  8. Android:Error retrieving parent for item: No resource found

随机推荐

  1. Android icon适配mipmap
  2. Android之telnet编程
  3. android samplerExternalOES 纹理
  4. Google应用在Android上的Push机制以及C2D
  5. Android项目总结5
  6. Eclipse使用
  7. android通过Camera录制视频
  8. 在mac上运行android的intel模拟器导致的
  9. Android.bp正确姿势添加宏控制编译指南
  10. 64位win7下Android SDK Manager闪退的解