默认地址显示
两个Activity,主Activity一打开就显示默认的地址,如果是第一次打开,则提示用户没有默认地址。 点击按钮可以打开修改默认地址的Activity。
组件:一个显示框,一个按钮
副Activity
组件:输入框 确认按钮 撤销按钮
输入默认地址,点击确认,就会传回到主Activity显示并且保存。
activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/tv01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="没有默认地址" android:textSize="20dp"/> <Button android:id="@+id/btn01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="open"/> </LinearLayout>
MainActivity.java文件
package com.example.pprp.myapplication_lh; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { Button btn01; TextView tv01; private static final int SUBACTIVITY1=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn01=(Button)findViewById(R.id.btn01); tv01=(TextView)findViewById(R.id.tv01); btn01.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(MainActivity.this,subactivity.class); startActivityForResult(intent,SUBACTIVITY1); } }); } public void onActivityResult(int requestCode,int resultCode,Intent data)//请求码,返回状态,返回的数据 { super.onActivityResult(requestCode,resultCode,data); switch(requestCode){ //对请求码进行匹配 case SUBACTIVITY1: if(resultCode==Activity.RESULT_OK) { Uri uridata=data.getData();//获取uri数据 tv01.setText(uridata.toString()); } else if(resultCode== Activity.RESULT_CANCELED) { } break; //不要忘记break } } }
subactvity.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/et01" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ok"/> <Button android:id="@+id/btncannel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="cannel"/> </LinearLayout>
subactivity.java文件
package com.example.pprp.myapplication_lh; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.PersistableBundle; import android.support.annotation.Nullable; import android.view.View; import android.widget.Button; import android.widget.EditText; /** * Created by lh on 2018/11/20. */ public class subactivity extends Activity{ EditText et01; Button btnok; Button btncannel; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.subactivity); et01= (EditText)findViewById(R.id.et01); btnok=(Button)findViewById(R.id.btnok); btncannel=(Button)findViewById(R.id.btncannel); btnok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String smg=et01.getText().toString(); Uri data= Uri.parse(smg);//数据以Uri的形式返回给父Activity Intent intent=new Intent(null,data); setResult(RESULT_OK,intent); finish(); } }); btncannel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); } }
AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.pprp.myapplication_lh"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".subactivity"> </activity> </application> </manifest>