主要实现Intent之间值得转递,如从AActivity到BActivity之间传一个数值,一个实体类,一个集合类
下面代码只要是实现对startActivityForResult的使用,用ABC 3个类来实现的,从A到B,再B到C。
现在的需求就是A到B到C,当C完成某个操作时,结束当前界面并转递数据给A。如需求中的添加购物地址,添加卡号。
demo:http://download.csdn.net/download/qq_29774291/9936021
如下是3个类的代码
package com.item.jiejie.intent; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import com.item.jiejie.intent.bean.BankCardUtil; import com.item.jiejie.intent.bean.Bean; public class MainActivity extends AppCompatActivity { private Button btn_to_one; private EditText edt_name; private Button btn_name; private String name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_to_one = (Button)findViewById(R.id.btn_to_one); edt_name = (EditText)findViewById(R.id.edt_name); btn_name = (Button)findViewById(R.id.btn_name); btn_to_one.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this,OneActivity.class); /**第一种方式将数据封装到intent对象中*/ // intent.putExtra("data","随便写的"); /**第二种方式将数据封装至bundle对象中*/ Bundle bundle = new Bundle(); bundle.putString("data","随便写的"); intent.putExtras(bundle); startActivityForResult(intent,0x01); } }); btn_name.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { name = BankCardUtil.getBankName(edt_name.getText().toString().trim().substring(0,6)); Log.d("jiejie",edt_name.getText().toString().trim().substring(0,6) + " " + name); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode){ case 0x01: if(resultCode == 0x03&& data!=null){ Bean bean = (Bean)data.getSerializableExtra("data"); Log.d("jiejie","--------" + bean.getName()); } // if(resultCode == 0x04&& data!=null){ // Bean bean = (Bean)data.getSerializableExtra("data"); // Log.d("jiejie","----dddd----" + bean.getName()); // } break; } } }
package com.item.jiejie.intent; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.item.jiejie.intent.bean.Bean; import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class OneActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_finish; private TextView tv_message; private Button btn_to_two; private List<Bean> beanList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_one); btn_finish = (Button)findViewById(R.id.btn_finish); btn_to_two = (Button)findViewById(R.id.btn_to_two); tv_message = (TextView)findViewById(R.id.tv_message); btn_finish.setOnClickListener(this); btn_to_two.setOnClickListener(this); Intent intent = getIntent(); /**第一种方式*/ // String data = intent.getStringExtra("data"); // tv_message.setText(data); /**第二种方式*/ Bundle bundle = intent.getExtras(); String data = bundle.getString("data"); Log.d("jiejie","从上个页面接受的数据" + data); tv_message.setText(data); beanList.add(new Bean(1,"one")); beanList.add(new Bean(2,"two")); beanList.add(new Bean(3,"three")); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn_finish: finish(); break; case R.id.btn_to_two: /** * 这里跳转到TwoActivity * 并传一个List的集合 */ Intent intent = new Intent(this,TwoActivity.class); intent.putExtra("list",(Serializable)beanList); startActivityForResult(intent,0x02); break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode){ case 0x02: if(resultCode == 0x04){ if(data != null){ setResult(0x03,data); finish(); } } break; } } }
package com.item.jiejie.intent; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import com.item.jiejie.intent.bean.Bean; import java.util.ArrayList; import java.util.List; public class TwoActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_finish; private Button btn_to_main; private ArrayList<Bean> beans; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); btn_finish = (Button)findViewById(R.id.btn_finish); btn_to_main = (Button)findViewById(R.id.btn_to_main); btn_to_main.setOnClickListener(this); btn_finish.setOnClickListener(this); Intent intent = getIntent(); if(intent!=null){ beans = (ArrayList<Bean>)intent.getSerializableExtra("list"); Log.d("jiejie","从上个界面接受的List集合" + beans.size() + " " + beans.get(1).getName()); } } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn_finish: finish(); break; case R.id.btn_to_main: //将一个数据传到MainActivity并把OneActivity杀死 Intent intent = new Intent(); Bean bean = new Bean(1,"你好啊"); intent.putExtra("data",bean); setResult(0x04,intent); finish(); break; } } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.item.jiejie.intent.bean; import java.io.Serializable; /** * 随便建个实体类 用来携带数据 * Created by Administrator o 2017/8/15. */ public class Bean implements Serializable { private int type; private String name; public Bean(int type, String name) { this.type = type; this.name = name; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
补充:添加一个overridePendingTransition来实现启动动画
如下是仿滴滴城市选择的动画效果
下面是3个动画:slide_in_up, slide_out_down,slide_no
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromYDelta="100%p" android:toYDelta="0" android:duration="@android:integer/config_mediumAnimTime" />
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromYDelta="0" android:toYDelta="100%p" android:duration="@android:integer/config_mediumAnimTime" />
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromYDelta="0" android:toYDelta="0" android:duration="@android:integer/config_mediumAnimTime" />
启动Activity
startActivity(new Intent(this, TestActivity.class)); MainActivity.this.overridePendingTransition(R.anim.slide_in_up,R.anim.slide_no);
关闭Activity
TestActivity.this.finish(); TestActivity.this.overridePendingTransition(0, R.anim.slide_out_down);
现在TestActivity就有开启时(从底部打开),关闭时(从顶部向下关闭),但是滴滴出行的布局左右有15dp的间距可以显示上个Activity
所以我们可以把这个TestActivity的背景窗口设置为透明就可以
<style name="TranparentTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
</style>
然后清单文件调用下
<activity android:name=".test.TestActivity"
android:theme="@style/TranparentTheme"/>
这样TestActivity就是透明窗口了,现在设置个内边距及背景颜色就可以了。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_gravity="center_vertical" android:background="#FF7200" android:focusable="true" android:focusableInTouchMode="true" android:orientation="horizontal" > <TextView android:id="@+id/tv_intent_back" android:layout_width="wrap_content" android:layout_height="match_parent" android:drawableTop="@drawable/saosao" android:gravity="center" android:paddingBottom="6dp" android:paddingLeft="12dp" android:paddingRight="12dp" android:paddingTop="4dp" android:text="@string/sweep" android:textColor="#fff" android:textSize="12sp" /> <EditText android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginBottom="6dp" android:layout_marginTop="6dp" android:layout_weight="0.8" android:background="#FF9D4D" android:drawableRight="@drawable/ic_search" android:gravity="center_vertical" android:hint="@string/search" android:paddingBottom="2dp" android:paddingLeft="5dp" android:paddingRight="2dp" android:paddingTop="2dp" android:singleLine="true" android:textColorHint="#fff" android:textSize="14sp" /> <TextView android:id="@+id/tv_intent_other" android:layout_width="wrap_content" android:layout_height="match_parent" android:drawableTop="@drawable/message" android:gravity="center" android:paddingBottom="2dp" android:paddingLeft="12dp" android:paddingRight="12dp" android:paddingTop="6dp" android:text="@string/message" android:textColor="#fff" android:textSize="12sp" > </TextView> </LinearLayout> <ListView android:id="@+id/lv_list" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="#ff