常用控件

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. YII学习(第一天)

    #Apache # 设置文档根目录为 "basic/web" DocumentRoot "path/to/basic/web" <Directory &q ...

  2. Windows7&IIS7.5部署Discuz

    IIS CGI一定要安装 IIS 网站中添加关联程序 ,添加默认文档 http://www.cnblogs.com/ajunForNet/archive/2012/09/12/2682063.html

  3. Change the ball(找规律)

    Change the ball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  4. easy ui 如何单个引用其中某个插件?

    记录一下这个方法,前端时间一直在纠结这个问题,后来听一些前辈讲解后才恍然大悟,要单独引用某个插件,我们需要重视的是:easyloaer.js ,easy ui的下载包中也有easyloader的dem ...

  5. 使用jqMobi开发app基础:弹出内容的设计

    设计APP,因为屏幕非常小.在PC网页山能够放在一体的内容.在APP中就不能放在一起了. 比如例如以下.项目出勤人员非常多,须要弹出一个panel.然后让用户选择,怎样设计呢? 项目出勤panel的内 ...

  6. 设置MySQL数据表主键

    设置MySQL数据表主键: 使用“primary key”关键字创建主键数据列.被设置为主键列不允许出现重复的值,很多情况下与“auto_increment”递增数字相结合.如下SQL语句所示: My ...

  7. vue+webpack项目实战

    概述 -- 项目中会用到的插件 vue-router vue-resource 打包工具 webpack 依赖环境 node.js start 安装vue开发的模板 # 全局安装 vue-cli $ ...

  8. ubuntu WiFi: operation not possible due to RF-kill《转载》

    Some people have been experiencing WiFi problems with Ubuntu 10.10 since an update that happend just ...

  9. windows驱动编程(目录)

    目录 第一章 入门 配置开发环境 第一个程序 应用程序调用内核函数的流程

  10. 研究 Javascript的&&和||的另类用法

    这篇文章主要介绍了Javascript的&&和||的另类用法,需要的朋友可以参考下 最近也没什么心思写文章了,感觉总有忙不完的事情,呵. 不过这些天又开始研究起 Titanium 来, ...