选择城市 activity之间数据回传

时间:2021-11-29 14:14:15

项目截图

 选择城市 activity之间数据回传   选择城市 activity之间数据回传 选择城市 activity之间数据回传

 
选择城市 activity之间数据回传选择城市 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     <LinearLayout
10         android:layout_width="match_parent"
11         android:layout_height="match_parent"
12         android:orientation="horizontal">
13 
14         <Button
15             android:id="@+id/button"
16             android:layout_width="wrap_content"
17             android:layout_height="wrap_content"
18             android:layout_weight="1"
19             android:text="请选择城市" />
20 
21         <EditText
22             android:id="@+id/editText"
23             android:layout_width="wrap_content"
24             android:layout_height="wrap_content"
25             android:layout_weight="1"
26             android:ems="10"
27             android:inputType="textPersonName" />
28     </LinearLayout>
29 </android.support.constraint.ConstraintLayout>
activity_main.xml
选择城市 activity之间数据回传选择城市 activity之间数据回传
 1 package com.example.city;
 2 
 3 import android.content.Intent;
 4 import android.support.annotation.Nullable;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.EditText;
10 
11 public class MainActivity extends AppCompatActivity {
12 
13     private Button button;
14     private EditText editText;
15 
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main);
20         button = (Button)findViewById(R.id.button);
21         editText = (EditText)findViewById(R.id.editText);
22         button.setOnClickListener(new View.OnClickListener() {
23             @Override
24             public void onClick(View v) {
25                 Intent intent = new Intent(MainActivity.this, SelectCity.class);
26                 startActivityForResult(intent,0);
27             }
28         });
29     }
30 
31     @Override
32     protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
33         super.onActivityResult(requestCode, resultCode, data);
34         if(requestCode==0 && resultCode==1){
35             String cityName = data.getStringExtra("cityName");
36             editText.setText(cityName);
37         }
38     }
39 }
MainActivity.java
选择城市 activity之间数据回传选择城市 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=".SelectCity">
 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_select_city.xml
选择城市 activity之间数据回传选择城市 activity之间数据回传
 1 package com.example.city;
 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 
11 public class SelectCity extends AppCompatActivity {
12 
13     private ListView listView;
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_select_city);
19         listView = (ListView)findViewById(R.id.listView);
20         String[] data = {"北京","上海","广州","深圳"};
21         final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data);
22         listView.setAdapter(arrayAdapter);
23         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
24             @Override
25             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
26                 Intent intent = new Intent();
27                 String cityName = arrayAdapter.getItem(position);
28                 intent.putExtra("cityName",cityName);
29                 setResult(1,intent);
30                 finish();
31             }
32         });
33     }
34 }
SelectCity.java

接受一切能进步的评论,谢谢各位大佬。