Android 多個界面(Activity)
阿新 • • 發佈:2019-03-12
edittext public andro xmlns sof rri parent layout instance
1.介紹
2.相關屬性
(1)啟動Activity
(2)Intent介紹
(3)關閉Activity
3.多個頁面之間傳遞數據
(1)相關屬性
註意:data為str的代號
(2)xml文件
第一個界面
<?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" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="50dp" android:text="這是登錄界面" android:textSize="30dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <TextView android:id="@+id/textView_username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用戶名" /> <EditText android:id="@+id/editText_username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <TextView android:id="@+id/textView_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密碼" /> <EditText android:id="@+id/editText_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="textPassword" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/button_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登錄" /> <Button android:id="@+id/button_exit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="退出" /> </LinearLayout> <TextView android:id="@+id/textView2" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="50dp" android:text="這是標準體重計算界面" android:textSize="30dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <TextView android:id="@+id/textView_gender" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請選擇性別" /> <RadioGroup android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal"> <RadioButton android:id="@+id/radioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <TextView android:id="@+id/textView_userheight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請輸入您的身高" /> <EditText android:id="@+id/editText_userheight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/button_calculate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="計算" /> </LinearLayout> </LinearLayout>
第二個界面
<?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" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="50dp" android:text="這是第二個界面" android:textSize="30dp" /> <Button android:id="@+id/button" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" /> <EditText android:id="@+id/editText_message" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:text="Name" /> <EditText android:id="@+id/editText_weight" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:text="Name" /> </LinearLayout>
(3)java後臺代碼
界面1
package com.lucky.test31activity; import android.content.Intent; 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.RadioButton; public class MainActivity extends AppCompatActivity { Button button_login; Button button_calculate; EditText editText_username; EditText editText_password; EditText editText_height; RadioButton radioButton; String gender; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button_login=findViewById(R.id.button_login); button_calculate=findViewById(R.id.button_calculate); editText_username=findViewById(R.id.editText_username); editText_password=findViewById(R.id.editText_password); editText_height=findViewById(R.id.editText_userheight); radioButton=findViewById(R.id.radioButton); button_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //實現頁面之間的跳轉 Intent intent=new Intent(MainActivity.this,SecondActivity.class); //若頁面之間要傳遞數據,可以用putExtra()方法,若不需要傳遞數據則下面兩句可以不寫 //toString()一定不能少 intent.putExtra("001",editText_username.getText().toString()); intent.putExtra("002",editText_password.getText().toString()); if(radioButton.isChecked()){ gender="男"; }else { gender="女"; } intent.putExtra("003",gender); intent.putExtra("004",Integer.parseInt(editText_height.getText().toString())); startActivity(intent); //進行頁面跳轉 } }); button_calculate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //實現頁面之間的跳轉 Intent intent=new Intent(MainActivity.this,SecondActivity.class); //若頁面之間要傳遞數據,可以用putExtra()方法,若不需要傳遞數據則下面兩句可以不寫 //toString()一定不能少 if(radioButton.isChecked()){ gender="男"; }else { gender="女"; } intent.putExtra("003",gender); intent.putExtra("004",Integer.parseInt(editText_height.getText().toString())); startActivity(intent); //進行頁面跳轉 } }); } }
界面2
package com.lucky.test31activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class SecondActivity extends AppCompatActivity { Button button1; EditText editText_message; EditText editText_userweight; int userheight; double userweight; String gender; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); button1=findViewById(R.id.button); editText_message=findViewById(R.id.editText_message); editText_userweight=findViewById(R.id.editText_weight); Intent intent=getIntent();//獲取Intent String name=intent.getStringExtra("001"); String pwd=intent.getStringExtra("002"); gender=intent.getStringExtra("003"); userheight=intent.getIntExtra("004",0); if(gender.equals("男")){ userweight=(userheight-80)*0.7; }else { userweight=(userheight-70)*0.6; } editText_message.setText("用戶名為:"+name+"\n密碼為:"+pwd); editText_userweight.setText("您的體重為:"+userweight); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish();//關閉頁面 } }); } }
Android 多個界面(Activity)