标签:tool data style request size cte line textview err
大家在网上购物时都有这样一个体验,在确认订单选择收货人以及地址时,会跳转页面到我们存入网站内的所有收货信息(包含收货地址,收货人)的界面供我们选择,一旦我们点击其中某一条信息,则会自动跳转到订单提交界面,此时的收货信息已经变为我们之前选择的收件信息、
为了实现这个功能,Android提供了一个机制,跳转到其他activity时,再返回,可以接受到其他activity返回的值,无需再start新的当前activity;下面的示例中,创建两个activity,其中在MainActivity中提示输入“您的属相”,然后单击TextView“您的属相”启动Main2Activity,在Main2Activity中选择属相,然后当返回MainActivity时,把之前Main2Activity中选择的数据原样返回给MainActivity。实现效果如下图:
(1)在MainActivity中设置启动带结果requestCode的Activity
(2)在MainActivity中设置处理返回结果的方法
(3)在Main2Activity中设置返回结果
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:id="@+id/activity_main" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:orientation="vertical" 9 tools:context="com.example.administrator.testactivity.MainActivity"> 10 <TextView 11 android:id="@+id/tv1" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:hint="潘侯爷的属相" 15 android:gravity="center" 16 android:textSize="30sp"/> 17 <Button 18 android:id="@+id/btn1" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:layout_gravity="right" 22 android:text="确定"/> 23 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:id="@+id/activity_main2" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context="com.example.administrator.day15.Main2Activity"> 8 <ListView 9 android:id="@+id/lv" 10 android:layout_width="match_parent" 11 android:layout_height="match_parent"></ListView> 12 </RelativeLayout>
1 import android.content.Intent; 2 import android.support.v7.app.AppCompatActivity; 3 import android.os.Bundle; 4 import android.view.Gravity; 5 import android.view.View; 6 import android.widget.Button; 7 import android.widget.TextView; 8 import android.widget.Toast; 9 public class MainActivity extends AppCompatActivity { 10 private Button btn1; 11 private TextView tv1; 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main); 16 btn1 = (Button) findViewById(R.id.btn1); 17 tv1 = (TextView) findViewById(R.id.tv1); 18 tv1.setClickable(true); 19 tv1.setOnClickListener(new View.OnClickListener() { 20 @Override 21 public void onClick(View v) { 22 Intent intent = new Intent(MainActivity.this, Main2Activity.class); 23 //设置启动intent以及对应的请求码 24 startActivityForResult(intent, 2); 25 } 26 }); 27 btn1.setOnClickListener(new View.OnClickListener() { 28 @Override 29 public void onClick(View v) { 30 Toast t= Toast.makeText(MainActivity.this, tv1.getText().toString(), Toast.LENGTH_SHORT); 31 t.setGravity(Gravity.TOP,0,0); 32 t.show(); 33 } 34 }); 35 } 36 @Override 37 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 38 super.onActivityResult(requestCode, resultCode, data); 39 //建议添加逻辑判断(使请求码与结果码配对,确保返回结果是请求码所请求的) 40 tv1.setText(data.getStringExtra("select")); 41 } 42 }
1 import android.content.Intent; 2 import android.support.v7.app.AppCompatActivity; 3 import android.os.Bundle; 4 import android.view.View; 5 import android.widget.AdapterView; 6 import android.widget.ArrayAdapter; 7 import android.widget.ListView; 8 public class Main2Activity extends AppCompatActivity { 9 private ListView lv; 10 String[] names={"马","牛","虎","兔","龙"}; 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main2); 15 lv = (ListView) findViewById(R.id.lv); 16 //使用ArrayAdapter为ListView每个单元格填充数据 17 ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main2Activity. 18 this,android.R.layout.simple_list_item_single_choice,names); 19 //设置ListView为单选模式 20 lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 21 //为ListView中的每个ITERM设置单击事件 22 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 23 @Override 24 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 25 Intent intent =new Intent(Main2Activity.this, MainActivity.class);; 26 //将选中的item对应的信息配上键值装入Extra中供传递用 27 intent.putExtra("select",names[position]); 28 //设置3为结果码 29 setResult(3,intent); 30 finish();//结束当前Activity,返回上一个activity 31 } 32 }); 33 lv.setAdapter(adapter); 34 } 35 }
标签:tool data style request size cte line textview err
原文地址:http://www.cnblogs.com/panhouye/p/6213287.html