Android项目页面跳转小Demo
来源:互联网 发布:centos 配置smtp 编辑:程序博客网 时间:2024/05/22 04:50
最近在做Android项目的开发,刚刚接触会有很多新东西需要学习,从环境的搭建到语言的熟悉都是需要一步步完成的,接下来就拿一个页面跳转的例子来和大家分享一下自己的心得体会。
采用的架构:
Demo中采用的是src/res/Manifest File架构,由于自己是新手,就按照这个传统的架构来做了。
总体结构:
项目中主要需要在src文件中写自己的java类、res的layout文件中写自己页面的xml文件,还有就是在mainifest中完成对java类的配置。
该例子实现的是当单击“helloword”页面中的button按钮时跳转到“second”页面中,效果如下:
跳转前
跳转后
首先我们需要建立两个java类:MainActivity & SecondActivity,代码如下:
【MainActivity】
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example;import android.R.color;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.graphics.Color;import android.graphics.pdf.PdfDocument;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {//定义自己的private Button btnButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnButton = (Button)findViewById(R.id.button1); btnButton.setWidth(200); btnButton.setHeight(50); btnButton.setBackgroundColor(Color.RED); btnButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setClass(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.main, menu); return true; } }</span>
【SecondActivity】
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example;import android.app.Activity;import android.os.Bundle;public class SecondActivity extends Activity{protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); }}</span>
同时在layout文件夹先对应建立xml文件,以完成页面的设计,
【activity_main】
<span style="font-family:KaiTi_GB2312;font-size:18px;"><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=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginTop="119dp" android:layout_toRightOf="@+id/textView1" android:text="Button" /></RelativeLayout></span>
【activity_second】
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?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/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="second" /></LinearLayout></span>
接下来需要我们在manifestfile文件中完成对建立的两个java类的配置:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.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.SecondActivity" android:label="@string/app_name" > <intent-filter> </intent-filter> </activity> </application></manifest></span>
需要强调的是每添加一个java类,都需要在AndroidManifest中完成相应的配置!
这样一个简单的Android小Demo就成型了,例子很简单,更多的是对开发环境的熟悉和了解,希望可以对刚刚接触Android开发的朋友们有所帮助,有问题也可以随时联系我!
0 0
- Android项目页面跳转小Demo
- [note]Android小Demo之Activity跳转
- iOS小demo之跳转到特定页面
- iOS小demo之跳转系统页面和APPStore
- android项目之启动页面跳转
- 小KING教你做android项目(二)---实现登陆页面并跳转和简单的注册页面
- 小KING教你做android项目(二)---实现登陆页面并跳转和简单的注册页面
- 小KING教你做android项目(二)---实现登陆页面并跳转和简单的注册页面
- struct1 页面跳转配置Demo
- 小程序页面跳转
- 小程序页面跳转
- Android入门--项目中添加页面及页面跳转
- web项目页面跳转
- 一天一个小案例android初阶之页面跳转
- android 页面的自动跳转 (android 项目心得一)
- Spark日志分析项目Demo(6)--页面单跳转化率分析
- 页面链接跳转小知识
- 一个页面的小跳转
- ASM(一) 利用Core API 解析和生成字节码
- Merge Two Sorted Lists
- android分辨率
- 【Treap】[BZOJ 1208]宠物收养所
- Yii学习笔记之二(使用gii生成一个简单的例子)
- Android项目页面跳转小Demo
- STL学习笔记之空间配置器
- 黑马程序员——Foundation框架——包装类以及NSDate
- SecurityException: Binder invocation to an incorrect interface using in-app billing
- 14.1.1 小玩文件
- 第七章
- HttpURLConnection访问网络
- (2)LinuxI2C驱动--I2C总线
- LightOJ1020(博弈)