Android開發(fā)之TextView使用intent傳遞信息,實(shí)現(xiàn)注冊界面功能示例

本文實(shí)例講述了Android開發(fā)之TextView使用intent傳遞信息,實(shí)現(xiàn)注冊界面功能。分享給大家供大家參考,具體如下:

10年積累的成都網(wǎng)站制作、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計(jì)制作后付款的網(wǎng)站建設(shè)流程,更有平定免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

使用intent在活動間傳遞值

首先是 MainActuvity 活動(注冊界面 寫完個人信息點(diǎn)擊注冊 )

跳轉(zhuǎn)到 In 活動 (通過 intent 獲得 MainActivity 中的信息 )

效果圖如下:

Android開發(fā)之TextView使用intent傳遞信息,實(shí)現(xiàn)注冊界面功能示例

MainActivity 實(shí)現(xiàn):

Java代碼:

public class Home extends AppCompatActivity {
  //用于存放個人注冊信息
  EditText user_name ;
  EditText user_code ;
  EditText user_year ;
  EditText user_birth ;
  EditText user_phone ;
  //注冊按鈕 點(diǎn)擊跳轉(zhuǎn)
  Button button01 ;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);//顯示manLayout
    user_name = (EditText) findViewById(R.id.ed_name);
    user_code = (EditText) findViewById(R.id.ed_code);
    user_year = (EditText) findViewById(R.id.ed_year);
    user_birth = (EditText) findViewById(R.id.ed_birth);
    user_phone = (EditText) findViewById(R.id.ed_phone);
    button01 = (Button) findViewById(R.id.bn_01);
    //通過 intent 實(shí)現(xiàn)活動間的信息傳遞
    button01.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent01 = new Intent(Home.this,In.class);
        intent01.putExtra("name",user_name.getText().toString());
        intent01.putExtra("code",user_code.getText().toString());
        intent01.putExtra("year",user_year.getText().toString());
        intent01.putExtra("birth",user_birth.getText().toString());
        intent01.putExtra("phone",user_phone.getText().toString());
        startActivity(intent01);
      }
    });
  }
}

XML布局文件:

<TableLayout
  android:id="@+id/root"
  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:stretchColumns="1">
  <TableRow>
    <TextView
      android:id="@+id/tv_name"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="用戶名"
      android:textSize="16sp"/>
    <EditText
      android:id="@+id/ed_name"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="請?zhí)顚懙顷戀~號"
      android:selectAllOnFocus="true"/>
  </TableRow>
  <TableRow>
    <TextView
      android:id="@+id/tv_code"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="密碼"
      android:textSize="16sp"/>
    <!--android:inputType="numberPassword"表示只能接受數(shù)字密碼-->
    <EditText
      android:id="@+id/ed_code"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:inputType="numberPassword"/>
  </TableRow>
  <TableRow>
    <TextView
      android:id="@+id/tv_year"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="年齡"
      android:textSize="16sp"/>
    <!--android:inputType="numberPassword"表示是數(shù)值輸入框-->
    <EditText
      android:id="@+id/ed_year"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:inputType="number"/>
  </TableRow>
  <TableRow>
    <TextView
      android:id="@+id/tv_birth"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="生日"
      android:textSize="16sp"/>
    <!--android:inputType="numberPassword"表示日期輸入框-->
    <EditText
      android:id="@+id/ed_birth"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:inputType="date"/>
  </TableRow>
  <TableRow>
    <TextView
      android:id="@+id/tv_phone"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="電話號碼"
      android:textSize="16sp"/>
    <!--android:inputType="numberPassword"表示電話號碼輸入框-->
    <EditText
      android:id="@+id/ed_phone"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:selectAllOnFocus="true"
      android:inputType="phone"/>
  </TableRow>
  <Button
    android:id="@+id/bn_01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="注冊"/>
</TableLayout>

In 活動:

Java代碼:

public class In extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_in);
    //獲得MainActivity傳進(jìn)來的數(shù)據(jù)
    Intent intent01 = getIntent();
    //放置傳入的信息
    TextView textView01 = (TextView) findViewById(R.id.In_tv_01);
    textView01.setText( intent01.getStringExtra("name") + "\n"
        + intent01.getStringExtra("code") + "\n"
        + intent01.getStringExtra("year") + "\n"
        + intent01.getStringExtra("birth") + "\n"
        + intent01.getStringExtra("phone") );
  }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".In">
  <!--//放置前一個活動傳遞進(jìn)來的信息-->
  <TextView
    android:id="@+id/In_tv_01"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。

網(wǎng)頁名稱:Android開發(fā)之TextView使用intent傳遞信息,實(shí)現(xiàn)注冊界面功能示例
URL網(wǎng)址:http://muchs.cn/article48/ghsehp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站導(dǎo)航網(wǎng)站維護(hù)、商城網(wǎng)站、電子商務(wù)、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)