Android實(shí)現(xiàn)界面跳轉(zhuǎn)功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)界面跳轉(zhuǎn)的具體代碼,供大家參考,具體內(nèi)容如下
布局
<?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=".MainActivity">
<!-- 線性布局 、垂直排列 -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 編輯框
@string/hint 表示 res/values/strings.xml下名為hint的標(biāo)簽
strings.xml用于字符串資源及其格式化
-->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/input" android:hint="@string/hint"/>
<Button
android:text="@string/send"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/button2" android:onClick="send"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
或者點(diǎn)擊text左邊的design進(jìn)行布局
響應(yīng)onclick
package com.android02;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
//定義常量,作為消息的key
public final static String MESSAGE_KEY="com.android2";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* 指定布局(res下的activity_main.xml編譯成r.java)
*/
setContentView(R.layout.activity_main);
}
/**
*響應(yīng)onclick事件
*/
public void send(View button){
//以id獲取EditText
EditText editText = findViewById(R.id.input);
//獲取編輯框文字
String message = editText.getText().toString();
//activity、service和broadcast receiver通過(guò)Intent進(jìn)行交互
Intent intent = new Intent(this,ReceiveActivity.class);
//在intent中附加信息
intent.putExtra(MESSAGE_KEY,message);
startActivity(intent);
}
}
創(chuàng)建ReceiveActivity
右擊java>>new>>Activity>>Empty Activity
然后進(jìn)入創(chuàng)建頁(yè)面指定Activity的名字…
然后它在AndroidManifest.xml中自動(dòng)注冊(cè)
package com.android02;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class ReceiveActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive);
//獲取intent引用
Intent intent = getIntent();
//以MESSAGE_KEY獲取獲取編輯框文字
String message = intent.getStringExtra(MainActivity.MESSAGE_KEY);
//以id獲取TextView
TextView textView = findViewById(R.id.output);
//顯示message
textView.setText(message);
}
}
測(cè)試
通過(guò)AVD manager 創(chuàng)建虛擬手機(jī)或使用真機(jī)測(cè)試


完成。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用Intent的Action和Data屬性實(shí)現(xiàn)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面
- Android倒計(jì)時(shí)控件 Splash界面5秒自動(dòng)跳轉(zhuǎn)
- Android 6.0動(dòng)態(tài)權(quán)限及跳轉(zhuǎn)GPS設(shè)置界面的方法
- android 跳轉(zhuǎn)到應(yīng)用通知設(shè)置界面的示例
- Android如何通過(guò)scheme跳轉(zhuǎn)界面
- Android中檢查網(wǎng)絡(luò)連接狀態(tài)的變化無(wú)網(wǎng)絡(luò)時(shí)跳轉(zhuǎn)到設(shè)置界面
- Android 中按home鍵和跳轉(zhuǎn)到主界面的實(shí)例代碼
- Android跳轉(zhuǎn)到系統(tǒng)聯(lián)系人及撥號(hào)或短信界面
- Android編程使用Fragment界面向下跳轉(zhuǎn)并一級(jí)級(jí)返回的實(shí)現(xiàn)方法
- Android中應(yīng)用界面主題Theme使用方法和頁(yè)面定時(shí)跳轉(zhuǎn)應(yīng)用
相關(guān)文章
Android中Activity的四種啟動(dòng)模式和onNewIntent()
android 中activity的啟動(dòng)模式分為四種,(standard、singleTop、singTask、singleInstance),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-08-08
Android對(duì)sdcard擴(kuò)展卡文件操作實(shí)例詳解
這篇文章主要介紹了Android對(duì)sdcard擴(kuò)展卡文件操作,非常實(shí)用的技術(shù),需要的朋友可以參考下2014-07-07
Android AlertDialog六種創(chuàng)建方式案例詳解
這篇文章主要介紹了Android AlertDialog六種創(chuàng)建方式案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android實(shí)現(xiàn)用代碼簡(jiǎn)單安裝和卸載APK的方法
這篇文章主要介紹了Android實(shí)現(xiàn)用代碼簡(jiǎn)單安裝和卸載APK的方法,涉及Android針對(duì)APK文件及package的相關(guān)操作技巧,需要的朋友可以參考下2016-08-08
Android基礎(chǔ)知識(shí)之frame動(dòng)畫效果
Android基礎(chǔ)知識(shí)之tween動(dòng)畫效果,Android一共提供了兩種動(dòng)畫,這篇文章主要介紹了Android動(dòng)畫效果之frame動(dòng)畫,感興趣的小伙伴們可以參考一下2016-06-06
Android?十六進(jìn)制狀態(tài)管理實(shí)例詳解
這篇文章主要為大家介紹了Android?十六進(jìn)制狀態(tài)管理實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android進(jìn)階Handler應(yīng)用線上卡頓監(jiān)控詳解
這篇文章主要為大家介紹了Android進(jìn)階Handler應(yīng)用線上卡頓監(jiān)控詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android開(kāi)發(fā)之毛玻璃效果實(shí)例代碼
這篇文章主要給大家分享android開(kāi)發(fā)之毛玻璃效果的實(shí)例代碼,非常具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-05-05

