Android studio 註冊介面
阿新 • • 發佈:2018-12-20
使用intent在活動間傳遞值
首先是 MainActuvity 活動(註冊介面 寫完個人資訊點選註冊 ) 跳轉到 In 活動 (通過 intent 獲得 MainActivity 中的資訊 )
效果圖如下:
MainActivity 實現: Java程式碼:
public class Home extends AppCompatActivity {
//用於存放個人註冊資訊
EditText user_name ;
EditText user_code ;
EditText user_year ;
EditText user_birth ;
EditText user_phone ;
//註冊按鈕 點選跳轉
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 實現活動間的資訊傳遞
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="請填寫登陸賬號"
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"表示只能接受數字密碼-->
<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"表示是數值輸入框-->
<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傳進來的資料
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">
<!--//放置前一個活動傳遞進來的資訊-->
<TextView
android:id="@+id/In_tv_01"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>