1. 程式人生 > 實用技巧 >Android資料跳轉

Android資料跳轉

<?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"
    android:paddingTop="100dp"
    android:paddingLeft="40dp">

    <
LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="鐢ㄦ埛鍚嶏細" android:textSize
="20sp"/> <EditText android:id="@+id/et_user" android:layout_width="250dp" android:layout_height="wrap_content" android:hint="璇瘋緭鍏ョ敤鎴峰悕" android:textColor="#ccc"/> </LinearLayout> <LinearLayout android:layout_width
="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="瀵? 鐮侊細" android:textSize="20sp"/> <EditText android:id="@+id/et_password" android:layout_width="250dp" android:layout_height="wrap_content" android:hint="璇瘋緭鍏ュ瘑鐮? android:textColor="#ccc"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="鎬? 鍒細" android:textSize="20sp" /> <RadioGroup android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/sex_man" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="鐢? /> <RadioButton android:id="@+id/sex_woman" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="濂? android:layout_marginLeft="20dp"/> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/hobby" android:layout_width="100sp" android:layout_height="wrap_content" android:text="鐖? 濂斤細" android:textSize="20dp" android:layout_marginTop="10dp"/> <CheckBox android:id="@+id/hb_badminton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="緹芥瘺鐞? android:textSize="20sp" android:layout_marginTop="10dp"/> <CheckBox android:id="@+id/hb_basketball" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="綃悆" android:textSize="20sp" android:layout_marginTop="10dp"/> <CheckBox android:id="@+id/hb_baseball" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="鎺掔悆" android:textSize="20sp" android:layout_marginTop="10dp"/> </LinearLayout> <Button android:id="@+id/btn_login" android:layout_width="200dp" android:layout_height="wrap_content" android:text="鐧誨綍" android:textColor="#fff" android:textSize="25sp" android:background="#2283f3" android:layout_marginTop="50dp" android:layout_marginLeft="60dp" android:onClick="onClick"/> </LinearLayout> package com.example.show; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.RadioGroup; public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { private EditText et_user, et_password; //宣告變數 private RadioGroup sex; private CheckBox hb_badminton, hb_basketball, hb_baseball; private Intent intent; private String muser; private String mpassword; private String msex; private String mhobby; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); mhobby = new String();//存放多選框內容 } private void init(){ //繫結控制元件 et_user = findViewById(R.id.et_user); et_password = findViewById(R.id.et_password); hb_badminton = findViewById(R.id.hb_badminton); hb_baseball= findViewById(R.id.hb_baseball); hb_basketball = findViewById(R.id.hb_basketball); hb_badminton.setOnCheckedChangeListener(this); hb_baseball.setOnCheckedChangeListener(this); hb_basketball.setOnCheckedChangeListener(this); sex = findViewById(R.id.sex); //通過匿名內部類的形式為單選框註冊監聽 sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { //判斷性別 if (checkedId==R.id.sex_man){ msex = "男"; }else { msex = "女"; } } }); } //通過實現介面的形式為多選框組註冊監聽 @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { String motion = buttonView.getText().toString(); if (isChecked){ if (!mhobby.contains(motion)){ mhobby = mhobby + motion; } }else { if (mhobby.contains(motion)) { mhobby = mhobby.replace(motion, ""); } } } //通過註冊按鈕的click屬性實現點選事件 public void onClick(View v) { if (v.getId()==R.id.btn_login){ //獲取使用者名稱和密碼 muser = et_user.getText().toString(); mpassword = et_password.getText().toString(); } //通過Intent將使用者名稱和密碼、單選框內容和多選框內容,傳遞給secondActivity intent = new Intent(this,SecondActivity.class); intent.putExtra("muser",muser); intent.putExtra("mpassword",mpassword); intent.putExtra("msex",msex); intent.putExtra("mhobby",mhobby); startActivity(intent); } } ---------------------------------------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="浣犵殑淇℃伅涓猴細" android:textSize="30sp" android:layout_marginLeft="30dp" android:layout_marginTop="50dp" android:textColor="#000"/> <TextView android:id="@+id/tv_outData" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="30dp" android:text="" android:textSize="30sp" /> </LinearLayout> package com.example.show; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; public class SecondActivity extends AppCompatActivity { private TextView tv_outData;//存放傳遞的資料 private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); //接受MainActivity傳遞的資料 Intent intent = getIntent(); String user = intent.getStringExtra("muser"); String password = intent.getStringExtra("mpassword"); String msex = intent.getStringExtra("msex"); String mhobby = intent.getStringExtra("mhobby"); tv_outData = findViewById(R.id.tv_outData); tv_outData.setText("使用者名稱:" + user + "\n" + "密 碼:" + password + "\n" + "性 別:" + msex + "\n" + "愛 好:" + mhobby); } } -------------------------------------------------------------------- <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="" android:onClick="regist"/> </RelativeLayout> ------------------------------------------------------------- <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="databack.RegistActivity"> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView1" android:layout_alignBottom="@+id/textView1" android:layout_alignParentRight="true" android:ems="10" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="74dp" android:text="" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/editText1" android:layout_marginTop="33dp" android:ems="10" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/editText2" android:layout_alignBottom="@+id/editText2" android:layout_alignLeft="@+id/textView1" android:text="" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText2" android:layout_centerHorizontal="true" android:layout_marginTop="91dp" android:onClick="click" android:text="" /> </RelativeLayout>