调用另一个Activity 分类: H1_ANDROID 2013-09-22 14:11 2217人阅读 评论(0) 收藏
参考自Google官方文档Traning/Getting Started/Building a simple user interface, Startinganother activity,http://developer.android.com/training/basics/firstapp/building-ui.html
1、创建主Activity
使用Eclipse新建项目MyFirstApp,UI布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainActivity"> <EditText
android:id="@+id/et_message"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:hint="@string/input_here"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/click"
android:onClick="sendMessage"/> </LinearLayout>
注意通过权重来分配尺寸的方式
组件1:
android:layout_width="0dp"
android:layout_weight="1"
组件2:
android:layout_width="wrap_content"
2、在主类中指定onclick所对应的sendMessage方法
package com.lujinhong.androidtraningmyfirstapp; import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText; public class MainActivity extends Activity{ public final static String EXTRA_MESSAGE="com.lujinhong.myfirstapp.MESSAGE"; @Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} @Override
public boolean onCreateOptionsMenu(Menu menu){
// Inflate themenu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} public void sendMessage(View v){ EditText et_message=(EditText)this.findViewById(R.id.et_message);
String message= et_message.getText().toString().trim(); Intent intent= new Intent(this,DisplayMessageActivity.class);
intent.putExtra(EXTRA_MESSAGE, message); this.startActivity(intent); } }
(1)关于intent
An Intent isan object that provides runtime binding between separate components (such astwo activities). TheIntent representsan
app’s "intent to do something." You can use intents for a widevariety of tasks,
but most often they’re used to startanother activity.
(2)调用另一个activity的步骤:
l 首先取得editText中的文字
EditText et_message = (EditText) this.findViewById(R.id.et_message);
String message = et_message.getText().toString().trim();
l 然后创建一下intent,并把文字作为K-V形式保存到intent中
Intent intent= new Intent(this,DisplayMessageActivity.class);
intent.putExtra(EXTRA_MESSAGE, message);
创建intent时,通过一个类名,指定调用哪个类文件。
l 最后启动一个新的activity.
this.startActivity(intent);
3、显示另一个Activity
package com.lujinhong.androidtraningmyfirstapp; import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView; public class DisplayMessageActivityextends Activity{ @Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message); // Get the messagefrom the intent
Intent intent= getIntent();
String message= intent.getStringExtra(MainActivity.EXTRA_MESSAGE); // Create the textview
TextView textView=new TextView(this);
textView.setTextSize(40);
textView.setText(message); // Set the textview as the activity layout
setContentView(textView); } @Override
public boolean onCreateOptionsMenu(Menu menu){
// Inflate themenu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
} }
版权声明:本文为博主原创文章,未经博主允许不得转载。
调用另一个Activity 分类: H1_ANDROID 2013-09-22 14:11 2217人阅读 评论(0) 收藏的更多相关文章
- iOS正则表达式 分类: ios技术 2015-07-14 14:00 35人阅读 评论(0) 收藏
一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...
- C++实现不能被继承的类——终结类 分类: C/C++ 2015-04-06 14:48 64人阅读 评论(0) 收藏
1. 问题 C++如何实现不能被继承的类,即终结类.Java中有final关键字修饰,C#中有sealed关键字修饰,而C++目前还没有类似的关键字来修饰类实现终结类,需编程人员手动实现. ...
- makefile基础实例讲解 分类: C/C++ 2015-03-16 10:11 66人阅读 评论(0) 收藏
一.makefile简介 定义:makefile定义了软件开发过程中,项目工程编译链.接接的方法和规则. 产生:由IDE自动生成或者开发者手动书写. 作用:Unix(MAC OS.Solars)和Li ...
- iOS中UITextField 使用全面解析 分类: ios技术 2015-04-10 14:37 153人阅读 评论(0) 收藏
//初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 13 ...
- 【Solr专题之九】SolrJ教程 分类: H4_SOLR/LUCENCE 2014-07-28 14:31 2351人阅读 评论(0) 收藏
一.SolrJ基础 1.相关资料 API:http://lucene.apache.org/solr/4_9_0/solr-solrj/ apache_solr_ref_guide_4.9.pdf:C ...
- cubieboard变身AP 分类: ubuntu cubieboard 2014-11-25 14:04 277人阅读 评论(0) 收藏
加载bcmdhd模块:# modprobe bcmdhd 如果你希望开启 AP 模式,那么:# modprobe bcmdhd op_mode=2 在/etc/modules文件内添加bcmdhd o ...
- printf "%.*s" 分类: 小细节 2015-07-04 14:36 2人阅读 评论(0) 收藏
ref : http://www.cnblogs.com/yuaqua/archive/2011/10/21/2219856.html 小数点.后"*"表示输出位数,具体的数据来自 ...
- IIS上虚拟站点的web.config与主站点的web.config冲突解决方法 分类: ASP.NET 2015-06-15 14:07 60人阅读 评论(0) 收藏
IIS上在主站点下搭建虚拟目录后,子站点中的<system.web>节点与主站点的<system.web>冲突解决方法: 在主站点的<system.web>上一级添 ...
- Dungeon Master 分类: 搜索 POJ 2015-08-09 14:25 4人阅读 评论(0) 收藏
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20995 Accepted: 8150 Descr ...
随机推荐
- Android 多种方式正确的载入图像,有效避免oom
图像载入的方式: Android开发中消耗内存较多一般都是在图像上面.本文就主要介绍如何正确的展现图像降低对内存的开销,有效的避免oom现象. 首先我们知道我的获取图像的来源一般有三种源 ...
- Web前端开发实战4:导航菜单(一)
在前面的博文中我们提到横向一级菜单,这里我们来看看导航菜单. 导航菜单种类非常多,可是制作原理都是大同 小异的.这里看的比二级下拉式菜单还简单. 来看一些站点上的导航菜单: 垂直导航菜单: 水平导航菜 ...
- 【单词】常见单词含义的辨异(emulator/simulator、hardware/firmware)
1. emulator 与 simulator The Simulator tries to duplicate the behavior of the device.(仿真的是行为): The Em ...
- JS错误记录 - To-do List
var data = (localStorage.getItem('todolist'))? JSON.parse(localStorage.getItem('todolist')) : { todo ...
- 1.字符设备驱动------Linux中断处理体系结构
一.中断处理体系结构的初始化 Linux内核将所有的中断统一编号,使用一个irq_desc结构数组来描述这些中断;每个数组项对应一个中断,也可能是一组中断,它们共用相同的中断号,里面记录了中断的名称. ...
- ThinkPHP5.0---静态方法
ThinkPHP5大量的使用了这种可以直接使用::调用的方法,它们有一个很响亮的名字:静态方法.静态方法的引用,大幅提升了程序的运行效率,降低了资源的占用. 静态方法(ASK$ANSWER) 为什么要 ...
- Linux启动(续)
runlevel (启动级别): 查看命令 :who -r 或 runlevel 0:halt 关机 1:单用户模式,直接以管理员身份登录,不需要密码 ...
- 洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II
洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II https://www.luogu.org/problemnew/show/P2616 题目描述 Farmer ...
- Debian9 安装后的配置笔记
安装Debian9后,需要做的事,具体如下: 以下内容主要参考:https://www.cnblogs.com/OneFri/p/8308340.html感谢作者的分享. su 输入密码,登录root ...
- 目标跟踪系列十一:Exploiting the Circulant Structure of Tracking-by-detection with Kernels代码思路
Tracking学习系列原创,转载标明出处: http://blog.csdn.net/ikerpeng/article/details/40144497 这篇文章非常赞啊!非常有必要将其好好的学习, ...