菜鸟学习日志4.Intent与Activity
来源:互联网 发布:wms软件 编辑:程序博客网 时间:2024/05/16 01:57
作为一个Android端的应用,多个Activity当然是避免不了的,我们该如何在多个Activity中跳转呢,这里就用到了Intent
下面我用一个按钮的响应跳转为例
先新建一个工程,这里我命名为IntentTest
在布局文件main里添加一个Button控件
代码如下
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
然后在主程序上设置按键的触发
完整代码为
MainActivity.java
package com.example.intenttest;import android.support.v7.app.ActionBarActivity;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.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity { private Button myButton = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myButton = (Button)findViewById(R.id.myButton); myButton.setText("启动开关"); myButton.setOnClickListener(new MyButtonListener()); } class MyButtonListener implements OnClickListener{ public void onClick(View v) { // TODO Auto-generated method stub //生成一Intent对象 Intent intent = new Intent(); intent.setClass(MainActivity.this,otherActivity.class); MainActivity.this.startActivity(intent); } } }
package com.example.intenttest;import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class otherActivity extends Activity{ private TextView myTextView = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); myTextView = (TextView)findViewById(R.id.myTextView); myTextView.setText(R.string.other); } }
对otherActivity的布局设计
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/myTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
当编写一个新的Activity 需在AndroidManifest中进行注册:
位置在</activity>和</application>之间
代码如下
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.intenttest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".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=".otherActivity" android:label="@string/other"/> </application><pre name="code" class="html"></manifest>
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">IntentTest</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="other">otherActivity</string></resources>
结果
0 0
- 菜鸟学习日志4.Intent与Activity
- Activity与Intent——android学习
- Intent与Activity介绍
- Activity与Intent
- Intent与Activity
- Activity与Intent
- Activity与Intent
- Activity与Intent
- Intent与Activity
- Activity与Intent
- Activity与intent
- Intent 与 Activity 相互关系
- Android入门学习笔记(五):Activity与Intent
- 安卓学习第二十七天:Activity与Intent
- Android学习:Activity和Intent
- ndroid学习笔记-Activity&Intent
- Android学习笔记-Activity&Intent
- android 开发 Activity 与intent
- Qt获取组合键
- maven pom.xml详解
- 视图缩放动画
- HTML 标签列表(3)
- 010Editor
- 菜鸟学习日志4.Intent与Activity
- Android API Guides 阅读笔记(8)----Service
- Android Studio添加网络权限
- Nginx+Tomcat+Memcached集群
- c++ 计蒜客第21题最大子阵列
- send , recv , sendto , recvfrom
- 编译性语言和解释性语言
- php中文字段转换成拼音
- CF_405B_DominoEffect