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: android:hint='@string/hint'/> <Button android:text='@string/send' android:layout_width='match_parent' android:layout_height='wrap_content' android: 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通過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è)試
通過AVD manager 創(chuàng)建虛擬手機(jī)或使用真機(jī)測(cè)試
完成。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. XML入門精解之結(jié)構(gòu)與語法2. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)3. Echarts自定義圖形的方法參考4. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享5. Xml簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理6. ASP基礎(chǔ)入門第二篇(ASP基礎(chǔ)知識(shí))7. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享8. ASP實(shí)現(xiàn)加法驗(yàn)證碼9. 解析原生JS getComputedStyle10. css代碼優(yōu)化的12個(gè)技巧
