主要为账号密码登录的数据验证与数据回传功能界面设计1.格式文件?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:layout_widthmatch_parent android:layout_heightmatch_parent android:orientationvertical android:padding50dp ImageView android:layout_width150dp android:layout_height200dp android:layout_gravitycenter android:srcdrawable/img / TextView android:idid/tv1 android:layout_widthmatch_parent android:layout_heightwrap_content android:layout_marginLeft70dp android:textSize20dp android:text账号: / TextView android:idid/tv2 android:layout_widthmatch_parent android:layout_heightwrap_content android:layout_marginLeft70dp android:textSize20dp android:text密码: / LinearLayout android:layout_widthmatch_parent android:layout_heightwrap_content android:gravitycenter_horizontal android:layout_marginTop10dp android:orientationhorizontal Button android:idid/bt1 android:layout_widthwrap_content android:layout_heightwrap_content android:text登录 / Button android:idid/bt2 android:layout_widthwrap_content android:layout_heightwrap_content android:text注册 / /LinearLayout /LinearLayout2. 主文件package com.example.datacallback; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; 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 AppCompatActivity { private TextView tv1,tv2; private Button bt1,bt2; Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv1 findViewById(R.id.tv1);//账号 tv2 findViewById(R.id.tv2);//密码 bt1 findViewById(R.id.bt1);//登录按钮 bt2 findViewById(R.id.bt2);//注册按钮 //登录按钮监听事件 bt1.setOnClickListener(new View.OnClickListener() { Override public void onClick(View view) { if (tv1.getText().toString().length()3 tv2.getText().toString().length()3){ Toast.makeText(MainActivity.this,账号不存在请先注册,Toast.LENGTH_LONG).show(); }else { Toast.makeText(MainActivity.this,登录成功,Toast.LENGTH_LONG).show(); } } }); //处理回传数据 ActivityResultLauncherIntent launcher registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallbackActivityResult() { Override public void onActivityResult(ActivityResult result) { Intent data result.getData(); // ... 获取并处理数据 String account data.getStringExtra(账号); String password data.getStringExtra(密码); tv1.setText(tv1.getText().toString() account); tv2.setText(tv2.getText().toString() password); } } ); //注册按钮跳转页面 bt2.setOnClickListener(new View.OnClickListener() { Override public void onClick(View view) { Intent intent new Intent(MainActivity.this, MainActivity2.class); launcher.launch(intent); } }); } }3. 页面2格式文件?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android xmlns:apphttp://schemas.android.com/apk/res-auto xmlns:toolshttp://schemas.android.com/tools android:layout_widthmatch_parent android:layout_heightmatch_parent android:orientationvertical android:layout_margin50dp tools:context.MainActivity2 LinearLayout android:layout_widthmatch_parent android:layout_heightwrap_content android:gravitycenter_horizontal android:orientationhorizontal TextView android:layout_widthwrap_content android:layout_heightwrap_content android:text账号/ EditText android:idid/et1 android:layout_widthwrap_content android:layout_heightwrap_content android:ems10 / /LinearLayout LinearLayout android:layout_widthmatch_parent android:layout_heightwrap_content android:gravitycenter_horizontal android:orientationhorizontal TextView android:layout_widthwrap_content android:layout_heightwrap_content android:text密码 / EditText android:idid/et2 android:layout_widthwrap_content android:layout_heightwrap_content android:ems10 / /LinearLayout Button android:idid/bt android:layout_widthmatch_parent android:layout_heightwrap_content android:layout_marginTop20dp android:text注册 / /LinearLayout4. 页面2主文件package com.example.datacallback; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; 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 MainActivity2 extends AppCompatActivity { private EditText et1,et2; private Button bt; Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); et1findViewById(R.id.et1); et2findViewById(R.id.et2); btfindViewById(R.id.bt); bt.setOnClickListener(new View.OnClickListener() { Override public void onClick(View view) { Intent intent getIntent(); intent.putExtra(账号,et1.getText().toString()); intent.putExtra(密码,et2.getText().toString()); Toast.makeText(MainActivity2.this,注册成功请重新登录, Toast.LENGTH_LONG).show(); setResult(1,intent); finish(); } }); } }5. 修改A文件package com.example.datacallback; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; 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 AppCompatActivity { TextView tv1,tv2,tv3; private EditText et1,et2; private Button bt1,bt2; Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv1 findViewById(R.id.tv1);//账号 tv2 findViewById(R.id.tv2);//密码 tv3 findViewById(R.id.tv3); bt1 findViewById(R.id.bt1); bt2 findViewById(R.id.bt2); et1 findViewById(R.id.et1); et2 findViewById(R.id.et2); //登录按钮 bt1.setOnClickListener(new View.OnClickListener() { Override public void onClick(View view) { if (et1.getText().toString().equals(tv3.getText().toString()) ){ Toast.makeText(MainActivity.this,登录成功,Toast.LENGTH_LONG).show(); }else { Toast.makeText(MainActivity.this,账号不存在请先注册,Toast.LENGTH_LONG).show(); } } }); //处理回传数据 ActivityResultLauncherIntent launcher registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallbackActivityResult() { Override public void onActivityResult(ActivityResult result) { Intent data result.getData(); String account data.getStringExtra(账号); String password data.getStringExtra(密码); tv3.setText(account); tv4.setText(password); } } ); //注册按钮跳转页面 bt2.setOnClickListener(new View.OnClickListener() { Override public void onClick(View view) { Intent intent new Intent(MainActivity.this, MainActivity2.class); launcher.launch(intent); } }); } }6. 最终A文件package com.example.datacallback; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; 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 AppCompatActivity { TextView tv1,tv2,tv3; private EditText et1,et2; private Button bt1,bt2; private String registeredAccount,registeredPassword; // 用于存储注册后返回的账号 Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv1 findViewById(R.id.tv1);//账号 tv2 findViewById(R.id.tv2);//密码 tv3 findViewById(R.id.tv3); bt1 findViewById(R.id.bt1); bt2 findViewById(R.id.bt2); et1 findViewById(R.id.et1); et2 findViewById(R.id.et2); //登录按钮 bt1.setOnClickListener(new View.OnClickListener() { Override public void onClick(View view) { String Account et1.getText().toString(); String Password et2.getText().toString(); if (Account.equals(registeredAccount) Password.equals(registeredPassword)){ Toast.makeText(MainActivity.this,登录成功,Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this,登录失败账号不存在请先注册,Toast.LENGTH_LONG).show(); } } }); //处理回传数据 ActivityResultLauncherIntent launcher registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallbackActivityResult() { Override public void onActivityResult(ActivityResult result) { Intent data result.getData(); String account data.getStringExtra(账号); String password data.getStringExtra(密码); registeredAccount account;// 存储账号到成员变量 registeredPassword password; // 假设你有一个TextView来显示这个账号 tv3.setText(已注册账号account 密码password); } } ); //注册按钮跳转页面 bt2.setOnClickListener(new View.OnClickListener() { Override public void onClick(View view) { Intent intent new Intent(MainActivity.this, MainActivity2.class); launcher.launch(intent); } }); } }7. 格式文件?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:layout_widthmatch_parent android:layout_heightmatch_parent android:orientationvertical android:padding50dp ImageView android:layout_width150dp android:layout_height200dp android:layout_gravitycenter android:srcdrawable/img / LinearLayout android:layout_widthmatch_parent android:layout_heightwrap_content TextView android:idid/tv1 android:layout_widthwrap_content android:layout_heightwrap_content android:layout_marginLeft30dp android:textSize20dp android:text账号: / EditText android:idid/et1 android:layout_widthmatch_parent android:layout_heightwrap_content android:ems10 / /LinearLayout LinearLayout android:layout_widthmatch_parent android:layout_heightwrap_content TextView android:idid/tv2 android:layout_widthwrap_content android:layout_heightwrap_content android:layout_marginLeft30dp android:textSize20dp android:text密码: / EditText android:idid/et2 android:layout_widthmatch_parent android:layout_heightwrap_content android:ems10 / /LinearLayout LinearLayout android:layout_widthmatch_parent android:layout_heightwrap_content android:gravitycenter_horizontal android:layout_marginTop10dp android:orientationhorizontal Button android:idid/bt1 android:layout_widthwrap_content android:layout_heightwrap_content android:text登录 / Button android:idid/bt2 android:layout_widthwrap_content android:layout_heightwrap_content android:text注册 / /LinearLayout TextView android:idid/tv3 android:layout_widthmatch_parent android:layout_heightwrap_content/ /LinearLayout