Android 實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
android使用Intent來(lái)實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn),Intent通過(guò)startActivity(Intent intent)或startActivityForResult(Intent intent,int resquestCode)方法來(lái)啟動(dòng)Activity,在新建Intent對(duì)象時(shí)來(lái)指定從A頁(yè)面跳到B頁(yè)面,
比如:
Intent i = new Intent(A.this,B.class);這就表示從A頁(yè)面跳到B頁(yè)面,
Intent對(duì)象通過(guò)調(diào)用putExtra方法來(lái)傳遞頁(yè)面跳轉(zhuǎn)時(shí)所需要傳遞的信息
比如:
putExtra(“給需要傳遞的信息命名”,需要傳遞的信息的內(nèi)容)
Intent通過(guò)調(diào)用getStringExtra方法來(lái)接受傳遞過(guò)來(lái)的信息
getStringExtra(“傳遞過(guò)來(lái)的信息的名字”);
下面的代碼將實(shí)現(xiàn)用戶(hù)輸入完信息之后點(diǎn)擊登入按鈕,頁(yè)面將跳轉(zhuǎn)到另一頁(yè)面顯示個(gè)人信息,然后在這個(gè)頁(yè)面有一個(gè)返回按鈕,點(diǎn)擊返回按鈕,頁(yè)面將返回登入頁(yè)面再次顯示個(gè)人信息。
MainActivity.java
package com.example.hsy.register;
import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText etName,etPwd;
Button btnLogin;
TextView tvShow;
RadioGroup rg;
RadioButton rbMale,rbFelMale;
CheckBox checkbox1,checkbox2,checkbox3;
Spinner spcity;
String sex="";
String hobby1="",hobby2="",hobby3="";
String result="";
String city="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
init();
rglistener();
btnloginlistener();
box1listener();
box2listener();
box3listener();
splistener();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
tvShow.setText("返回結(jié)果是:"+"\n"+data.getStringExtra("result1").toString()+
"\n");
}
private void splistener() {
spcity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
city=(String) spcity.getSelectedItem();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void box3listener() {
checkbox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
hobby3="看書(shū)";
} else {
hobby3="";
}
}
});
}
private void box2listener() {
checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
hobby2="游泳";
} else {
hobby2="";
}
}
});
}
private void box1listener() {
checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
hobby1="唱歌";
} else {
hobby1="";
}
}
});
}
private void btnloginlistener() {
btnLogin.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
result="用戶(hù)名是:"+etName.getText().toString()+"\n"+"密碼是:"+
etPwd.getText().toString()+"\n"+"性別是:"+sex+"\n"+"愛(ài)好是:"+hobby1+" "
+hobby2+" "+hobby3+" "+
"\n"+"所在城市:"+city;
//tvShow.setText(result);
Intent i = new Intent(MainActivity.this,Main2Activity.class);
i.putExtra("data1",result);
startActivityForResult(i,0);
}
});
}
private void rglistener() {
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId){
if(checkedId== R.id.rbMale)
sex="男生";
else
sex="女生";
}
});
}
private void init() {
etName=(EditText) findViewById(R.id.etName);
etPwd=(EditText) findViewById(R.id.etPwd);
btnLogin=(Button) findViewById(R.id.btnLogin);
tvShow=(TextView) findViewById(R.id.tvShow);
rg=(RadioGroup) findViewById(R.id.rg);
rbMale=(RadioButton) findViewById(R.id.rbMale);
rbFelMale=(RadioButton) findViewById(R.id.rbFeMale);
checkbox1=(CheckBox) findViewById(R.id.checkbox1);
checkbox2=(CheckBox) findViewById(R.id.checkbox2);
checkbox3=(CheckBox) findViewById(R.id.checkbox3);
spcity=(Spinner) findViewById(R.id.Spcity);
}
}
MainActivity2.java
package com.example.hsy.register;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
TextView tvShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tvShow=(TextView)findViewById(R.id.tvShow);
Intent intent = getIntent();
tvShow.setText(intent.getStringExtra("data1").toString());
findViewById(R.id.btnBack).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1=new Intent(Main2Activity.this,MainActivity.class);
intent1.putExtra("result1",tvShow.getText().toString());
setResult(1,intent1);
finish();
}
});
}
}
test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<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="用戶(hù)名:"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:textColor="@color/colorPrimaryDark"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="輸入2-10個(gè)字符"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:id="@+id/etName"/>
</LinearLayout>
<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:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:textColor="@color/colorPrimaryDark"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="輸入6-10個(gè)字符"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:id="@+id/etPwd"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="選擇性別:"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:textColor="@color/colorPrimary"
/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/rg">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:textColor="@color/colorAccent"
android:textSize="10dp"
android:text="男"
android:id="@+id/rbMale"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:textColor="@color/colorAccent"
android:textSize="10dp"
android:text="女"
android:id="@+id/rbFeMale"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:textColor="@color/colorAccent"
android:text="興趣愛(ài)好:"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textSize="15dp"
android:textColor="@color/colorAccent"
android:id="@+id/checkbox1"
android:text="唱歌"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textSize="15dp"
android:textColor="@color/colorAccent"
android:id="@+id/checkbox2"
android:text="游泳"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textSize="15dp"
android:textColor="@color/colorAccent"
android:id="@+id/checkbox3"
android:text="看書(shū)"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="6dp"
android:textSize="15dp"
android:text="所在地"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_marginLeft="10dp"
android:entries="@array/citys"
android:id="@+id/Spcity">
</Spinner>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登錄"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:id="@+id/btnLogin"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="顯示信息"
android:id="@+id/tvShow"/>
</LinearLayout>
activity_main2.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="com.example.hsy.register.Main2Activity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="顯示結(jié)果"
android:id="@+id/tvShow"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="返回結(jié)果"
android:id="@+id/btnBack"/>
</LinearLayout>
總結(jié)
以上所述是小編給大家介紹的Android 實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android百度地圖實(shí)現(xiàn)搜索和定位及自定義圖標(biāo)繪制并點(diǎn)擊時(shí)彈出泡泡
這篇文章主要介紹了Android百度地圖實(shí)現(xiàn)搜索和定位及自定義圖標(biāo)繪制并點(diǎn)擊時(shí)彈出泡泡的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android實(shí)現(xiàn)將一個(gè)Activity設(shè)置成窗口樣式的方法
這篇文章主要介紹了Android實(shí)現(xiàn)將一個(gè)Activity設(shè)置成窗口樣式的方法,涉及Android的窗口樣式設(shè)置與布局技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-02-02
Android 實(shí)現(xiàn)沉浸式狀態(tài)欄的方法
沉浸式狀態(tài)欄的來(lái)源就是很多手機(jī)用的是實(shí)體按鍵,沒(méi)有虛擬鍵,于是開(kāi)了沉浸模式就只有狀態(tài)欄消失了。下面腳本之家小編給大家介紹Android 實(shí)現(xiàn)沉浸式狀態(tài)欄,需要的朋友可以參考下2015-09-09
Android中ViewFlipper和AdapterViewFlipper使用的方法實(shí)例
ViewFlipper和AdapterViewFlipper是Android自帶的一個(gè)多頁(yè)面管理控件,下面這篇文章主要給大家介紹了關(guān)于Android中ViewFlipper和AdapterViewFlipper使用的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
Android 快速使用正則表達(dá)式,校驗(yàn)身份證號(hào)的實(shí)例
下面小編就為大家分享一篇Android 快速使用正則表達(dá)式,校驗(yàn)身份證號(hào)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android編程開(kāi)發(fā)之TextView單擊鏈接彈出Activity的方法
這篇文章主要介紹了Android編程開(kāi)發(fā)之TextView單擊鏈接彈出Activity的方法,涉及Android中TextView控件的相關(guān)操作技巧,需要的朋友可以參考下2016-01-01
創(chuàng)建Android庫(kù)的方法及Android .aar文件用法小結(jié)
本文給大家介紹了創(chuàng)建Android庫(kù)的方法及Android中 .aar文件生成方法與用法詳解,涉及到創(chuàng)建庫(kù)模塊操作步驟及開(kāi)發(fā)注意事項(xiàng),需要的朋友參考下吧2017-12-12
Android變形(Transform)之Camera使用介紹
Camera主要實(shí)現(xiàn)3D的變形,有轉(zhuǎn)動(dòng),旋轉(zhuǎn)等,Camera的源碼是由Native(本地代碼)實(shí)現(xiàn),提供的接口也比較簡(jiǎn)單,感興趣的朋友可以參考下,或許對(duì)你學(xué)習(xí)有所幫助2013-02-02

