参考自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) 收藏的更多相关文章

  1. iOS正则表达式 分类: ios技术 2015-07-14 14:00 35人阅读 评论(0) 收藏

    一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...

  2. C++实现不能被继承的类——终结类 分类: C/C++ 2015-04-06 14:48 64人阅读 评论(0) 收藏

    1.       问题 C++如何实现不能被继承的类,即终结类.Java中有final关键字修饰,C#中有sealed关键字修饰,而C++目前还没有类似的关键字来修饰类实现终结类,需编程人员手动实现. ...

  3. makefile基础实例讲解 分类: C/C++ 2015-03-16 10:11 66人阅读 评论(0) 收藏

    一.makefile简介 定义:makefile定义了软件开发过程中,项目工程编译链.接接的方法和规则. 产生:由IDE自动生成或者开发者手动书写. 作用:Unix(MAC OS.Solars)和Li ...

  4. iOS中UITextField 使用全面解析 分类: ios技术 2015-04-10 14:37 153人阅读 评论(0) 收藏

    //初始化textfield并设置位置及大小   UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 13 ...

  5. 【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 ...

  6. cubieboard变身AP 分类: ubuntu cubieboard 2014-11-25 14:04 277人阅读 评论(0) 收藏

    加载bcmdhd模块:# modprobe bcmdhd 如果你希望开启 AP 模式,那么:# modprobe bcmdhd op_mode=2 在/etc/modules文件内添加bcmdhd o ...

  7. printf "%.*s" 分类: 小细节 2015-07-04 14:36 2人阅读 评论(0) 收藏

    ref : http://www.cnblogs.com/yuaqua/archive/2011/10/21/2219856.html 小数点.后"*"表示输出位数,具体的数据来自 ...

  8. IIS上虚拟站点的web.config与主站点的web.config冲突解决方法 分类: ASP.NET 2015-06-15 14:07 60人阅读 评论(0) 收藏

    IIS上在主站点下搭建虚拟目录后,子站点中的<system.web>节点与主站点的<system.web>冲突解决方法: 在主站点的<system.web>上一级添 ...

  9. Dungeon Master 分类: 搜索 POJ 2015-08-09 14:25 4人阅读 评论(0) 收藏

    Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20995 Accepted: 8150 Descr ...

随机推荐

  1. CSS控制显示超出部分,用省略号显示

    经常使用.可是常忘,我又不是写css的.所以记下来: 先设置一下限制的宽度, display:block; white-space:nowrap; overflow:hidden; text-over ...

  2. 74LS153 选择器 【数字电路】

    74LS153 我用了八个不同频率的方波信号,用153当作信号选择器,控制环节的开关是4通道的选择器,00 01 10 11分别选择通道 0 1 2 3 以下是八选一的demo

  3. TimePickerDialog -下划线颜色修改

    首先就是去framework下去找与之相关的theme属性 最开始的时候,直接找的是<item name="datePickerStyle">@style/Widget ...

  4. spring的BeanWrapper类的原理和使用方法

    转自:http://blog.sina.com.cn/s/blog_79ae79b30100t4hh.html 如果动态设置一个对象属性,可以借助Java的Reflection机制完成: Class ...

  5. 建堆是 O(n) 的时间复杂度证明。

    建堆的复杂度先考虑满二叉树,和计算完全二叉树的建堆复杂度一样. 对满二叉树而言,第 \(i\) 层(根为第 \(0\) 层)有 \(2^i\) 个节点. 由于建堆过程自底向上,以交换作为主要操作,因此 ...

  6. 实际感受美丽的Linux(多组视频)

    1.Fedora 上使用google earth 2.体验桌面-GNOME 2.2.9 3.体验桌面-KDE4.4 永中Office使用演示(和MS Office 2003很相似哦) 4.在Fedor ...

  7. leaf cell

    leaf cell是否可以理解为设计中与或非门等这些基本的单元?

  8. 阅读笔记——Web应用程序

    Web应用程序与DD文件 Web应用程序 web应用程序是一种可以通过Web访问的应用程序.Web应用程序最大的好处是永和很容易访问应用程序.用户只需要有浏览器即可,不需要安装其他任何软件.一个Web ...

  9. MyBatis学习总结(12)——Mybatis+Mysql分页查询

    package cn.tsjinrong.fastfile.util; /**  * @ClassName: Page  * @Description: TODO(分页组件的父类,用来封装分页的 通用 ...

  10. python 命令行參数解析

    本文是从我还有一个博客转载过来的,欢迎大家点击进去看一下,帮我添加点人气^_^ ImPyy 选择模块 依据python參考手冊的提示,optparse 已经废弃,应使用 argparse 教程 概念 ...