常用控件

EditView、TextView、Button

设置layout,在fragment_main.xml配置控件

配置可编辑文本控件factorOne、factorTwo,显示文本控件symbol,按钮控件calculate

<RelativeLayout 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"
tools:context="com.example.activity01.MainActivity$PlaceholderFragment" > <EditText
android:id="@+id/factorOne"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/factorTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/sym"/>
<TextView
android:id="@+id/sym"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/factorOne" />
<Button
android:id="@+id/cal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/factorTwo"
/>
</RelativeLayout>

在values下string.xml设置symbol、calculate字符串

<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">Activity01</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="sym">乘以</string>
<string name="cal">计算</string>
</resources>

根据控件id取对象,为symbol和calculate设置显示的值

        private EditText factorOne=null;
private EditText factorTwo=null;
private TextView sym=null;
private Button cal=null;
@Override
public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
factorOne=(EditText)findViewById(R.id.factorOne);
factorTwo=(EditText)findViewById(R.id.factorTwo);
sym=(TextView)findViewById(R.id.sym);
cal=(Button)findViewById(R.id.cal);
sym.setText(R.string.sym);
cal.setText(R.string.cal);
cal.setOnClickListener(new CalListener());//监听器绑定到按钮上
return true;
}

监听器

class CalListener implements OnClickListener{

        @Override
public void onClick(View v) {
// TODO Auto-generated method stub
String factorOneStr=factorOne.getText().toString();
String factorTwoStr=factorTwo.getText().toString();
Intent intent=new Intent();
intent.putExtra("one", factorOneStr);
intent.putExtra("two", factorTwoStr);
intent.setClass(MainActivity.this, ResultActivity.class);
MainActivity.this.startActivity(intent);
}
}

新建result.xml文件

     <TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

新建ResultActivity,获取计算结果

public class ResultActivity extends Activity{
private TextView resultView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
resultView=(TextView)findViewById(R.id.result);
Intent intent=new Intent();
String factorOneStr=intent.getStringExtra("one");
String factorTwoStr=intent.getStringExtra("two");
int factorOneInt=Integer.parseInt(factorOneStr);
int factorTwoInt=Integer.parseInt(factorTwoStr);
int result=factorOneInt * factorTwoInt;
resultView.setText(result+"");
}
}

注册ResultActivity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activity01"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.activity01.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>
<activity
android:name="com.example.activity01.ResultActivity"
android:label="@string/app_name" >
</activity>
</application> </manifest>

设置menu菜单

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.add(0, 1, 1, R.string.exit);
menu.add(0, 2, 3, R.string.about);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==1){
finish();
}
return super.onOptionsItemSelected(item);
}

Copyright © 吴华锦
雅致寓于高阁渔舟唱晚,古典悠然
格调外发园林绿树萦绕,馥郁清香

  

Activity(三)的更多相关文章

  1. android Service Activity三种交互方式(付源码)(转)

    android Service Activity三种交互方式(付源码) Android应用服务器OSBeanthread  android Service Binder交互通信实例 最下边有源代码: ...

  2. Service Activity三种交互方式

    Service Activity三种交互方式 2012-09-09 22:52 4013人阅读 评论(2) 收藏 举报 serviceandroidimportclassthreadjava     ...

  3. android Service Activity三种交互方式(付源码)

    android SDK提供了Service,用于类似Linix守护进程或者windows的服务. Service有两种类型: 本地服务(Local Service):用于应用程序内部 远程服务(Rem ...

  4. Android启动组件的三种主流及若干非主流方式

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处 启动组件的一些方式,今天做一个简要的总结 Service:通过startService()启动,或者写 ...

  5. Android Activity的生命周期详解

    应用程序中,一个Activity通常就是一个单独的屏幕,它上面可以显示一些控件也可以监听并处理用户的事件做出响应. Activity之间通过Intent进行通信.在Intent 的描述结构中,有两个最 ...

  6. Android四大组件——Activity

    Activity作为Android四大组件之一,也是其中最重要的一个组件.作为一个与用户交互的组件,我们可以把Activity比较成为windows系统上的一个文件夹窗口,是一个与用户交互的界面.再进 ...

  7. Android Apk获取包名和Activity名称

    一.使用aapt(Android Asset Packaging Tool)工具获取: 1.配置Android环境: a.添加build-tools/android路径到系统环境变量的中Path中,注 ...

  8. Android四大组件之——Activity(一)定义、状态和后退栈(图文详解)

    什么是Activity 关键字:应用组件.四大组件.用户界面,交互. An Activity is an application component that provides a screen wi ...

  9. android如何快速查看APK包名和activity

    一.通过ADB命令 1.dos进入 2.输入adb shell登录 3.输入dumpsys package | grep eggs(过滤相关包名) 二.通过日志查看包名() 1.连接设备 2.cmd命 ...

  10. Android四大组件全然解析(一)---Activity

    本文參考\android\android\frameworks\base\core\java\android\app\Activity.java文件里的类凝视.以及android/frameworks ...

随机推荐

  1. npm 常用命令详解[转]

    npm是什么 NPM的全称是Node Package Manager,是随同NodeJS一起安装的包管理和分发工具,它很方便让JavaScript开发者下载.安装.上传以及管理已经安装的包. npm ...

  2. Java单例模式的线程安全问题

    单例模式有两种书写模式:饿汉式和懒汉式. 1.饿汉式 class Single{ private final static Single s = new Single(); private Singl ...

  3. Unhandled event loop exception 解决办法

    网上搜索了一下.对其他人有效的办法有两种: 1. 安装了adsafe  .卸载掉就可以了. 2.安装了流氓软件:百度杀毒. 卸载掉也解决问题了 我就是第三种情况.到现在还没解决的热.!. 谁还有更好的 ...

  4. UVA 12125 March of the Penguins

    题意: 给定一些冰块,每个冰块上有一些企鹅,每个冰块有一个可以跳出的次数限制,每个冰块位于一个坐标,现在每个企鹅跳跃力为d,问所有企鹅能否跳到一点上,如果可以输出所有落脚冰块,如果没有方案就打印-1 ...

  5. 输出jq对象

    console.log($()); 效果截图:

  6. [转载]MATLAB中FFT的使用方法

    http://blog.163.com/fei_lai_feng/blog/static/9289962200971751114547/ 说明:以下资源来源于<数字信号处理的MATLAB实现&g ...

  7. hadoop源码编译

    为何要自行编译hadoop源码,往往是由于官方提供的hadoop发行版都是基于32位操作系统,在操作hadoop时会发生warn.   准备软件: 1)JDK 2)Hadoop源码 3)Maven 4 ...

  8. 区分innerHeight与clientHeight、innerWidth与clientWidth、scrollLeft与pageXOffset等属性

    window对象:(1)innerHeight属性:窗口中文档显示区域的高度,不包括菜单栏.工具栏等部分.该属性可读可写.     IE不支持该属性,IE中body元素的clientHeight属性与 ...

  9. 不同分辨率下获取不同js文件

    获取当前网站的目录  //js获取网站根路径(站点及虚拟目录),获得网站的根目录或虚拟目录的根地址 function getRootPath(){ //整个域名(如:http://vc3.cn/ind ...

  10. To and Fro(字符串水题)

    To and Fro 点我 Problem Description Mo and Larry have devised a way of encrypting messages. They first ...