Start a new AndroidStudio Project
创建一个新的安卓项目文件
名字就叫做TestProject
![](https://img.haomeiwen.com/i13568395/11921a3b21641eed.png)
Add No Active 不使用默认的添加活动,自己手动添加活动
在app/src/main/java/com.example.yaomenglong.testproject中右键New->Active->EmptyActive
![](https://img.haomeiwen.com/i13568395/a7bee69c5a6c1152.png)
不勾选General Layout,我们自己添加布局文件
在src/main/res中右键New->Directory创建新的文件夹layout
在新的文件夹layout上右键创建layout_resoursefile
![](https://img.haomeiwen.com/i13568395/220ba2f63f0ab86e.png)
首先将布局文件与我们创建的第一个活动相关联
转到FirstActivity中
在OnCreate中添加代码
setContentView(R.layout.first_activity_layout);
这算是绑定了逻辑与视图文件,FirstActivity处理逻辑,layout处理视图
在总活动中注册FirstActivity
打开AndroidMainifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yaomenglong.testporject">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".FirstActivity"></activity>
</application>
</manifest>
这里是app所有活动注册的地方,我们需要把FirstActivity添加进去,这需要使用activity标签,在activity标签中添加intent-filter标签声明这个活动是主活动,也就是app开启时打开的第一个活动
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yaomenglong.testporject">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".FirstActivity"
android:label="This is FirstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
使用Button与TextView
在布局文件中可创建Button 与 TextView,在first_activity_layout中的Linerlayout添加以下代码
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="go to Second"
android:id="@+id/start_second"
android:layout_margin="10px" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="this is my first textView"/>
效果展示
![](https://img.haomeiwen.com/i13568395/247148777d94a60c.png)
你可能注意到了,在Button中我们加入了这样一行代码
android:id="@+id/start_second"
这是给这个button一个唯一的标识
我们可以在FirstActivity中获取button的引用,就可以创建进行一系列操作了,这里我们使用button点击时间跳到第二个活动中
创建新的活动SecondActivity,这次我们使用他的默认的布局文件GeneralLayout
然后在FirstActivity使用
Button button1 = (Button)findViewById(R.id.start_second);
获取button的引用
然后给button添加点击事件,使其利用intent跳转到第二个活动secondActivity中
button_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
}
});
运行我们的程序,首先是第一个页面
![](https://img.haomeiwen.com/i13568395/0bf695a411b2bc3d.png)
点击Button,转到第二个页面
![](https://img.haomeiwen.com/i13568395/ffb3fc89738896ff.png)
至此,简单的活动创建完毕,下一节将会讲解intent的使用
网友评论