Android實(shí)現(xiàn)注冊(cè)頁(yè)面(攜帶數(shù)據(jù)包跳轉(zhuǎn))
安卓學(xué)習(xí):實(shí)現(xiàn)注冊(cè)頁(yè)面輸入數(shù)據(jù),點(diǎn)擊注冊(cè)按鈕跳轉(zhuǎn)到另一個(gè)頁(yè)面并顯示輸入信息
效果:
實(shí)現(xiàn)
1.創(chuàng)建安卓文件
2.創(chuàng)建注冊(cè)界面,勾選為啟動(dòng)頁(yè)
3.編寫代碼
啟動(dòng)界面activity_register11.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:background="@drawable/bk024" ? ? android:gravity="center" ? ? android:padding="15sp" ? ? android:orientation="vertical" ? ? tools:context=".register11"> ? ? <LinearLayout ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_gravity="center_horizontal"> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/tvName" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:textColor="#FFFF00" ? ? ? ? ? ? android:text="@string/name" ? ? ? ? ? ? android:textSize="25sp"/> ? ? ? ? <EditText ? ? ? ? ? ? android:id="@+id/edtName" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:ems="12" ? ? ? ? ? ? android:singleLine="true"/> ? ? </LinearLayout> ? ? <LinearLayout ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_gravity="center_horizontal"> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/tvSex" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:textColor="#FFFF00" ? ? ? ? ? ? android:text="@string/sex" ? ? ? ? ? ? android:textSize="25sp"/> ? ? ? ? <EditText ? ? ? ? ? ? android:id="@+id/edtSex" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:ems="12" ? ? ? ? ? ? android:singleLine="true"/> ? ? </LinearLayout> ? ? <LinearLayout ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_gravity="center_horizontal"> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/tvAge" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:textColor="#FFFF00" ? ? ? ? ? ? android:text="@string/age" ? ? ? ? ? ? android:textSize="25sp"/> ? ? ? ? <EditText ? ? ? ? ? ? android:id="@+id/edtAge" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:ems="12" ? ? ? ? ? ? android:singleLine="true"/> ? ? </LinearLayout> ? ? <LinearLayout ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_gravity="center_horizontal"> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/tvPhone" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:textColor="#FFFF00" ? ? ? ? ? ? android:text="@string/phone" ? ? ? ? ? ? android:textSize="25sp"/> ? ? ? ? <EditText ? ? ? ? ? ? android:id="@+id/edtPhone" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:ems="12" ? ? ? ? ? ? android:singleLine="true"/> ? ? </LinearLayout> ? ? <LinearLayout ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_gravity="center_horizontal"> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/tvEmail" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:textColor="#FFFF00" ? ? ? ? ? ? android:text="@string/email" ? ? ? ? ? ? android:textSize="25sp"/> ? ? ? ? <EditText ? ? ? ? ? ? android:id="@+id/edtEmail" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:ems="12" ? ? ? ? ? ? android:singleLine="true"/> ? ? </LinearLayout> ? ? <LinearLayout ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_gravity="center_horizontal"> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/tvHomepage" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="@string/homepage" ? ? ? ? ? ? android:textColor="#FFFF00" ? ? ? ? ? ? android:textSize="25sp"/> ? ? ? ? <EditText ? ? ? ? ? ? android:id="@+id/edtHomepage" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:ems="12" ? ? ? ? ? ? android:singleLine="true"/> ? ? </LinearLayout> ? ? <LinearLayout ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_gravity="center_horizontal"> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/tvRemark" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:textColor="#FFFF00" ? ? ? ? ? ? android:text="@string/remark" ? ? ? ? ? ? android:textSize="25sp"/> ? ? ? ? <EditText ? ? ? ? ? ? android:id="@+id/edtRemark" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:ems="12" ? ? ? ? ? ? android:singleLine="true"/> ? ? </LinearLayout> ? ? <LinearLayout ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:gravity="center_horizontal" ? ? ? ? android:orientation="horizontal"> ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/btnRegister" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:paddingLeft="30dp" ? ? ? ? ? ? android:paddingRight="30dp" ? ? ? ? ? ? android:background="@color/danlv" ? ? ? ? ? ? android:textColor="#ffffff" ? ? ? ? ? ? android:text="@string/register" ? ? ? ? ? ? android:layout_marginRight="10dp" ? ? ? ? ? ? android:textSize="20sp"/> ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/btnCancel" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:paddingLeft="30dp" ? ? ? ? ? ? android:paddingRight="30dp" ? ? ? ? ? ? android:background="@color/danlv" ? ? ? ? ? ? android:text="@string/cancel" ? ? ? ? ? ? android:textSize="20sp" ? ? ? ? ? ? android:textColor="#ffffff"/> ? ? </LinearLayout> </LinearLayout>
register11.java
import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; public class register11 extends AppCompatActivity { ? ? private EditText edtName; ? ? private EditText edtSex; ? ? private EditText edtAge; ? ? private EditText edtPhone; ? ? private EditText edtEmail; ? ? private EditText edtHomepage; ? ? private EditText edtRemark; ? ? private Button btnRegister; ? ? private Button btnCancel; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_register11); ? ? ? ? //活動(dòng)欄圖標(biāo) ? ? ? ? ActionBar actionBar = getSupportActionBar(); ? ? ? ? actionBar.setDisplayShowHomeEnabled(true); ? ? ? ? actionBar.setDisplayUseLogoEnabled(true); ? ? ? ? actionBar.setLogo(R.mipmap.icon); ? ? ? ? //獲取控件實(shí)例 ? ? ? ? edtName = findViewById(R.id.edtName); ? ? ? ? edtSex = findViewById(R.id.edtSex); ? ? ? ? edtAge = findViewById(R.id.edtAge); ? ? ? ? edtPhone = findViewById(R.id.edtPhone); ? ? ? ? edtEmail = findViewById(R.id.edtEmail); ? ? ? ? edtHomepage = findViewById(R.id.edtHomepage); ? ? ? ? edtRemark = findViewById(R.id.edtRemark); ? ? ? ? btnRegister = findViewById(R.id.btnRegister); ? ? ? ? btnCancel = findViewById(R.id.btnCancel); ? ? ? ? //給注冊(cè)按鈕注冊(cè)監(jiān)聽器 ? ? ? ? btnRegister.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? //保存用戶輸入數(shù)據(jù) ? ? ? ? ? ? ? ? String strName=edtName.getText().toString().trim();//trim()去除用戶在前面輸入的空格 ? ? ? ? ? ? ? ? String strSex=edtSex.getText().toString().trim(); ? ? ? ? ? ? ? ? String strAge=edtAge.getText().toString().trim();//trim()去除用戶在前面輸入的空格 ? ? ? ? ? ? ? ? String strPhone=edtPhone.getText().toString().trim();//trim()去除用戶在前面輸入的空格 ? ? ? ? ? ? ? ? String strEmail=edtEmail.getText().toString().trim(); ? ? ? ? ? ? ? ? String strHomepage=edtHomepage.getText().toString().trim();//trim()去除用戶在前面輸入的空格 ? ? ? ? ? ? ? ? String strRemark=edtRemark.getText().toString().trim(); ? ? ? ? ? ? ? ? //利用吐司提示用戶注冊(cè)情況 ? ? ? ? ? ? ? ? Toast.makeText(register11.this, "注冊(cè)成功", Toast.LENGTH_SHORT).show();//LENGTH_SHORT表示登錄時(shí)間長(zhǎng)度 ? ? ? ? ? ? ? ? //創(chuàng)建顯示意圖 ? ? ? ? ? ? ? ? Intent intent = new Intent(register11.this, MainActivity.class); ? ? ? ? ? ? ? ? //創(chuàng)建數(shù)據(jù)包封裝數(shù)據(jù)(封裝) ? ? ? ? ? ? ? ? Bundle date = new Bundle(); ? ? ? ? ? ? ? ? date.putString("name", strName); ? ? ? ? ? ? ? ? date.putString("sex", strSex); ? ? ? ? ? ? ? ? date.putString("age", strAge); ? ? ? ? ? ? ? ? date.putString("phone", strPhone); ? ? ? ? ? ? ? ? date.putString("email", strEmail); ? ? ? ? ? ? ? ? date.putString("homepage", strHomepage); ? ? ? ? ? ? ? ? date.putString("remark", strRemark); ? ? ? ? ? ? ? ? //通過(guò)意圖攜帶數(shù)據(jù)包 ? ? ? ? ? ? ? ? intent.putExtras(date); ? ? ? ? ? ? ? ? //按照意圖啟動(dòng)目標(biāo)組件 ? ? ? ? ? ? ? ? startActivity(intent); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? btnCancel.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? //關(guān)閉窗口 ? ? ? ? ? ? ? ? finish(); ? ? ? ? ? ? } ? ? ? ? }); ? ? } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="@drawable/bk028" tools:context=".MainActivity"> <TextView ? ? android:id="@+id/tvMessage" ? ? android:layout_width="wrap_content" ? ? android:layout_height="wrap_content" ? ? android:text=" " ? ? android:textSize="25dp" ? ? android:textColor="#ff2233" ? ? /> </LinearLayout>
ActivityMain.java
import android.content.Intent; import android.os.Bundle; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { ? ? protected TextView tvMessage; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? //通過(guò)資源標(biāo)識(shí)符獲取控件實(shí)例 ? ? ? ? tvMessage = findViewById(R.id.tvMessage); ? ? ? ? //獲取意圖 ? ? ? ? Intent intent = getIntent(); ? ? ? ? //判斷意圖是否為空 ? ? ? ? if (intent != null) { ? ? ? ? ? ? //獲取意圖攜帶數(shù)據(jù)包(封裝) ? ? ? ? ? ? Bundle date = intent.getExtras(); ? ? ? ? ? ? String name = date.getString("name"); ? ? ? ? ? ? String sex = date.getString("sex"); ? ? ? ? ? ? String age = date.getString("age"); ? ? ? ? ? ? String phone = date.getString("phone"); ? ? ? ? ? ? String email = date.getString("email"); ? ? ? ? ? ? String homepage = date.getString("homepage"); ? ? ? ? ? ? String remark = date.getString("remark"); ? ? ? ? ? ? //拼接用戶信息 ? ? ? ? ? ? String message = "注冊(cè)成功!\n姓名:" + name + "\n性別:" + sex + "\n年齡:" + age + "\n電話:" + phone + "\n郵箱:" + email + "\n主頁(yè):" + homepage + "\n備注:" + remark; ? ? ? ? ? ? //設(shè)置標(biāo)簽屬性,顯示用戶信息 ? ? ? ? ? ? tvMessage.setText(message); ? ? ? ? } ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android使用Flutter實(shí)現(xiàn)錄音插件
這篇文章主要介紹了基于flutter實(shí)現(xiàn)錄音功能,介紹了如何錄音,如何把文件存放到本地,這些都是我們平常使用這個(gè)功能會(huì)遇到的問(wèn)題。在使用的過(guò)程中遇到的問(wèn)題也有列出,需要的朋友可以參考下2022-08-08Android通過(guò)代碼控制ListView上下滾動(dòng)的方法
今天小編就為大家分享一篇關(guān)于Android通過(guò)代碼控制ListView上下滾動(dòng)的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12Android使用listview實(shí)現(xiàn)分頁(yè)刷新(線程休眠模擬)
這篇文章主要為大家詳細(xì)介紹了Android使用listview實(shí)現(xiàn)分頁(yè)刷新,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11android 更改TextView中任意位置字體大小和顏色的方法
下面小編就為大家分享一篇android 更改TextView中任意位置字體大小和顏色的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01android 拷貝sqlite數(shù)據(jù)庫(kù)到本地sd卡的方法
下面小編就為大家?guī)?lái)一篇android 拷貝sqlite數(shù)據(jù)庫(kù)到本地sd卡的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03Kotlin中的惰性操作容器Sequence序列使用原理詳解
這篇文章主要為大家介紹了Kotlin中的惰性操作容器Sequence序列使用原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android Studio無(wú)法打開得問(wèn)題解決方法
這篇文章主要詳細(xì)介紹了Android Studio無(wú)法打開問(wèn)題解決辦法,文中有詳細(xì)的解決步驟和圖文介紹,對(duì)我們解決這個(gè)問(wèn)題有一定的幫助,需要的朋友可以參考下2023-07-07Android對(duì)EditTex的圖片實(shí)現(xiàn)監(jiān)聽
這篇文章主要為大家詳細(xì)介紹了Android如何對(duì)EditTex的圖片實(shí)現(xiàn)監(jiān)聽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10android 點(diǎn)擊EditText始終不彈出軟件鍵盤實(shí)現(xiàn)代碼
這篇文章主要介紹了android 點(diǎn)擊EditText始終不彈出軟件鍵盤實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11Android開發(fā)實(shí)現(xiàn)抽屜菜單
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)實(shí)現(xiàn)抽屜菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11