安卓学习第二十七天:Activity与Intent
来源:互联网 发布:怎么学习云计算 编辑:程序博客网 时间:2024/06/05 02:30
直接上代码,该代码实现了点击一个button,然后由监听器监听到点击
然后发送Toast通知,然后跳转到SecondActivity
这里要说一下今天学习的显式Intent,Intent 是 Android 程序中各组件之间进行交互的一种重要方式,它不仅可以指明当前组
件想要执行的动作,还可以在不同组件之间传递数据。Intent 一般可被用于启动活动、启动
服务、以及发送广播等场景,由于服务、广播等概念你暂时还未涉及,那么本章我们的目光
无疑就锁定在了启动活动上面。
package com.android.cris.myapplication;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.Toast;import static android.view.View.*;public class MainActivity extends Activity { private Button button1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1=(Button)findViewById(R.id.button); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,"你点击了按钮",Toast.LENGTH_LONG).show(); Intent intent=new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. switch (item.getItemId()) { case R.id.add: Toast.makeText(MainActivity.this, "你点击了add", Toast.LENGTH_LONG).show(); break; case R.id.back: Toast.makeText(MainActivity.this, "你点击了back", Toast.LENGTH_LONG).show(); break; default: } return true; }}
这里要说一下定义的menu,以及对应的通知;
<pre name="code" class="java"> onOptionsItemSelected方法中,可以检测到选择的菜单menu的Item,然后通过getItemId()方法获取对应的id
来判断是点击的哪一个按钮,之后通过switch方法,选择对应点击的处理操作。
下面式xml源码:
<pre name="code" class="html"><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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按钮1"/></RelativeLayout>下面是第二个activity
<pre name="code" class="java">package com.android.cris.myapplication;import android.app.Activity;import android.os.Bundle;import android.view.Window;/** * Created by cris on 15-5-10. */public class SecondActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.second_layout); }}
下面是第二个xml
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按钮2" /></LinearLayout>
0 0
- 安卓学习第二十七天:Activity与Intent
- 第二十七天学习笔记
- 安卓学习第七天
- 安卓学习笔记(九)intent——安卓第七天
- 安卓学习: Intent
- 第二十七天
- java学习第二十七天html&css、xml
- 安卓学习第七天:View
- [安卓基础]学习第七天
- 安卓·使用intent切换Activity
- 安卓学习第十七天:相对布局的初步使用与换源学习
- 安卓学习(一) 安卓组件与Activity生命周期
- 安卓学习140502:Android Activity和Intent机制学习笔记
- 安卓学习2-Intent
- 安卓学习-activity
- 安卓学习笔记---Intent的使用,如何改变Activity在当前任务堆栈中的顺序
- 安卓学习笔记(一)显式Intent与隐式Intent
- 菜鸟学习日志4.Intent与Activity
- alsa-lib, alsa-utils交叉编译及在嵌入式上使用
- 搜索小赛的酱油
- Python 第一天(2)
- Demo02:创建Option菜单
- js的style.width和offsetWidth问题
- 安卓学习第二十七天:Activity与Intent
- 关于如何整理句料和词库
- Java编程思想重点笔记(Java开发必看)
- 2.29
- 我的Ubuntu vim配置文件vimrc(无编程插件版)
- Netty Reactor模型
- 有了Prezi,你还在用PPT?
- 在JSP中调用Java Class的方法
- Codeforces Round #168 (Div. 1)B 树形dp