安卓学习第二十七天: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
原创粉丝点击