调用另一个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 ...
随机推荐
- php函数按地址传递参数(php引用)
php函数按地址传递参数(php引用) 一.总结 1.php引用:php引用和c++一样,都是在变量前加&(取地址符号) 2.php函数按地址传递参数:php函数按地址传递参数(php引用)也 ...
- 求第k大的数(用到快速排序算法的思想)
//下面两种part效率比较:相同运算量下part比part2快5倍左右,part2写法简单但是效率低 #include "stdafx.h" #include <iostr ...
- IK分词器插件elasticsearch-analysis-ik 6.1.1
http://88250.b3log.org/full-text-search-elasticsearch#b3_solo_h3_0 IK分词器插件 (1)源码 https://github.com/ ...
- erroe:plot.new() : figure margins too large
使用R时多次出现这个错误,plot.new() : figure margins too large,提示图片边界太大 解决方法,win.graph(width=4.875, height=2.5,p ...
- js上传文件(图片)的格式和大小限制
如果你想快速解决这个问题,看本文就够了.查了好多资料,终于解决了,太耗时间了,本文留给给后来者,希望你们工作的更轻松 本文保存为.html文件用浏览器打开即可测试功能 <form id=&quo ...
- 用vue.js的v-for,v-if,computed写一个分页样式
在学Vue,总想写个分页,先写了一个样式. 主要看思路: 思路简单,得到总页数,判断总页数,循环. 先判断总页数是否需要分页,总页数==1页就不分了. 再判断总页数<11就不用--. 总页数&g ...
- Python之字符串切片
切片操作(slice)可以从一个字符串中获取子字符串(字符串的一部分).我们使用一对方括号.起始偏移量start.终止偏移量end 以及可选的步长step 来定义一个分片. 格式: [start:en ...
- java中那些已经有的好用轮子
后续补充 IOUtils http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/IOUtils.html ...
- ZOJ 2723 Semi-Prime ||ZOJ 2060 Fibonacci Again 水水水!
两题水题: 1.如果一个数能被分解为两个素数的乘积,则称为Semi-Prime,给你一个数,让你判断是不是Semi-Prime数. 2.定义F(0) = 7, F(1) = 11, F(n) = F( ...
- 9、str类型和byte类型转换、列表拾遗、元组拾遗、字典拾遗、如何判断对象是否可迭代
str(字节类型,编码) 可用于创建字符串,或者将其他的转换成字符串 a= ‘李露’ #将字符串转换成字节流 b = bytes(a,encoding = 'utf-8') #将字节转换成 ...