国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 實現頁面跳轉

2019-10-22 18:28:16
字體:
來源:轉載
供稿:網友

android使用Intent來實現頁面跳轉,Intent通過startActivity(Intent intent)或startActivityForResult(Intent intent,int resquestCode)方法來啟動Activity,在新建Intent對象時來指定從A頁面跳到B頁面,

比如:

Intent i = new Intent(A.this,B.class);這就表示從A頁面跳到B頁面, 

Intent對象通過調用putExtra方法來傳遞頁面跳轉時所需要傳遞的信息

比如:

putExtra(“給需要傳遞的信息命名”,需要傳遞的信息的內容)

Intent通過調用getStringExtra方法來接受傳遞過來的信息

getStringExtra(“傳遞過來的信息的名字”);

下面的代碼將實現用戶輸入完信息之后點擊登入按鈕,頁面將跳轉到另一頁面顯示個人信息,然后在這個頁面有一個返回按鈕,點擊返回按鈕,頁面將返回登入頁面再次顯示個人信息。

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("返回結果是:"+"/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="看書";        } 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="用戶名是:"+etName.getText().toString()+"/n"+"密碼是:"+            etPwd.getText().toString()+"/n"+"性別是:"+sex+"/n"+"愛好是:"+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="用戶名:"      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個字符"      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個字符"      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="興趣愛好:"/>    <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="看書"/>  </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="顯示結果"    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="返回結果"    android:id="@+id/btnBack"/></LinearLayout>

總結

以上所述是小編給大家介紹的Android 實現頁面跳轉,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 盐城市| 汶上县| 尉犁县| 河间市| 固阳县| 开原市| 昌江| 从江县| 闽侯县| 宜兰县| 涞水县| 商河县| 梁河县| 荥经县| 喀喇| 边坝县| 连城县| 武功县| 金门县| 台中市| 措勤县| 沾化县| 巴林左旗| 宁津县| 凤冈县| 威信县| 特克斯县| 德安县| 南宫市| 且末县| 盐亭县| 北川| 独山县| 汶上县| 莫力| 五大连池市| 星座| 辽源市| 财经| 壶关县| 颍上县|