Intent跳转布局及传递数据
来源:互联网 发布:金融数据公司 编辑:程序博客网 时间:2024/05/19 14:17
首先在mian.xml中添加按钮控件,我需要点击按钮就能从一个Activity跳转到另一个Activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <Button android:text="跳转" android:layout_width="wrap_content" android:layout_height="wrap_content"android:onClick="buttonListener"/></LinearLayout>
添加xml文件,命名为other,在里面添加TextView控件:
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"android:gravity="center"><TextViewandroid:layout_height="wrap_content"android:text="另一个Activity"android:layout_width="wrap_content"android:id="@+id/otherTextView"/></LinearLayout>
添加java文件,命名OtherActivity:
package com.intent1;import android.app.*;import android.os.*;public class OtherActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.other);}}
在AndroidManifest.xml中注册OtherActivity:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.intent1" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity><activityandroid:name=".OtherActivity"/> </application></manifest>
在MainActivity.java中:
package com.intent1;import android.app.*;import android.os.*;import android.view.*;import android.content.*;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }public void buttonListener(View view){//声明intent变量,从MainActivity跳转到OtherActivityIntent intent = new Intent(MainActivity.this, OtherActivity.class);//传递姓名和年龄intent.putExtra("姓名", "小明");intent.putExtra("年龄", 20);startActivity(intent);}}
在OtherActivity.java中:
package com.intent1;import android.app.*;import android.content.*;import android.os.*;import android.widget.*;public class OtherActivity extends Activity{//声明变量private TextView text;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.other);text = (TextView)findViewById(R.id.otherTextView);Intent intent = getIntent();//获取姓名和年龄String name = intent.getStringExtra("姓名");int age = intent.getIntExtra("年龄", 1); //如果没有年龄这个参数,就返回1text.setText("姓名:"+name+"\n年龄:"+age);}}
效果图:
阅读全文
0 0
- Intent跳转布局及传递数据
- Intent界面跳转,数据传递 初级
- Intent隐式跳转及参数传递
- Intent传递数据及类型
- [Android] Intent及Bundle传递数据
- bundle及intent传递接收数据
- Android 使用Intent实现Activity跳转和数据传递
- 使用Intent实现活动间的跳转和数据传递
- Android实现intent跳转界面传递数据(1)
- Android---8---Intent及使用Intent传递数据
- activity之间的跳转及传递数据
- iOS页面跳转及数据传递
- iOS页面跳转及数据传递
- iOS页面跳转及数据传递
- iOS页面跳转及数据传递
- iOS页面跳转及数据传递
- iOS页面跳转及数据传递
- iOS页面跳转及数据传递
- Android studio http 代理设置
- Java EE开发环境——MyEclipse2017破解 和 Tomcat服务器配置
- C++中的lambda
- C#初识1 进程
- MyEclipse/Eclipse中Debug调试(断点调试)
- Intent跳转布局及传递数据
- java 反射机制 之 newInstance 执行无参数构造函数
- NOIP2010提高组T2:乌龟棋
- Coreldraw绘制出一条鱼儿可是一件很特别的效果吧
- JDBC插入数据使用索引与否的差别
- F
- 堆的简单实现
- 移动端开发通用坑以解决方案
- Git几个好用的进阶命令