Android中Activity组件实例介绍

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Android中Activity组件实例介绍
⽬录
Activity 概述
启动 Activity 的两种情况
关闭 Activity
总结
Activity 概述
在 Android 应⽤中,提供了 4 ⼤基本组件,分别是 Activity、Service、BroadcastReceiver 和 ContentProvider。

⽽ Activity 是Android 应⽤最常见的组件之⼀。

Activity 的中⽂意思是活动。

在 Android 中,Activity 代表⼿机或者平板电脑中的⼀屏,它提供了和⽤户交互的可视化界⾯。

在⼀个 Activity 中,可以添加很多组件,这些组件负责具体的功能。

在⼀个 Android 应⽤中,可以有多个 Activity。

这些 Activity 组成了 Activity 栈(Stack),当前活动的 Activity 位于栈顶,之前的 Activity 被压⼊下⾯,成为⾮活动 Activity,等待是否可能被恢复为活动状态。

启动 Activity 的两种情况
①、在⼀个 Android 应⽤中,只有⼀个 Activity 时,那么只需要在 AndroidManifest.xml ⽂件中对其进⾏备注,并且将其设置为程序的⼊⼝。

这样,当运⾏时,将⾃动启动该 Activity。

②、在⼀个 Android 应⽤中,存在多个 Activity 时,需要应⽤ startActivity() ⽅法来启动需要的 Activity。

关闭 Activity
在 Android 中,如果想要关闭当前的 Activity,可以使⽤ Activity 类提供的 finish()⽅法。

举例说明:启动和关闭 Activity
核⼼代码如下
// MainActivity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(yout.activity_main);
getWindow().setFlags(youtParams.FLAG_FORCE_NOT_FULLSCREEN,
youtParams.FLAG_FORCE_NOT_FULLSCREEN);
TextView password = (TextView) findViewById(R.id.wang_mima);
password.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建 Intent 对象
Intent intent = new Intent(MainActivity.this,PasswordActivity.class);
//启动 PasswordActivity
startActivity(intent);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#CCCC99"
android:stretchColumns="0,3">
<!-- 第⼀⾏ -->
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingTop="200dp"
>
<TextView />
<TextView
android:text="账号:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="15dp"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:hint="邮箱或⼿机号"
/>
<TextView/>
</TableRow>
<!-- 第⼆⾏ -->
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingTop="2dp">
<TextView />
<TextView
android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="15dp"
android:text="密码"
android:gravity="center_horizontal"
/>
<EditText
android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="输⼊ 6-16 位数字或字母" android:textSize="15dp"
/>
<TextView/>
</TableRow>
<!-- 第三⾏ -->
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView/>
<Button
android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="注册"
/>
<Button
android:layout_width="15dp"
android:layout_height="wrap_content" android:text="登录"
/>
<TextView/>
</TableRow>
<!-- 第四⾏ -->
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingTop="15dp"
>
<TextView />
<TextView />
<TextView
android:id="@+id/wang_mima"
android:text="忘记密码?"
android:textColor="#FF4500"
android:gravity="right"
/>
</TableRow>
</TableLayout>
所得主界⾯
//创建新活动 PasswordActivity
package com.example.example61;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class PasswordActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(yout.activity_password);
//获得布局⽂件中的关闭按钮
ImageButton close = (ImageButton) findViewById(R.id.close); close.setOnClickListener(new View.OnClickListener(){
@Override
//关闭当前 Activity
public void onClick(View v) {
finish();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="/apk/res/android" xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PasswordActivity"
android:background="#CCCC99">
<ImageButton
android:id="@+id/close"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:background="#0099CC"
android:padding="5dp"
android:scaleType="centerInside"
android:src="@drawable/a" />
<TextView
android:id="@+id/t1"
android:layout_width="350dp"
android:layout_height="40dp"
android:layout_alignBottom="@+id/close"
android:layout_alignParentRight="true"
android:background="#0099CC"
android:paddingHorizontal="120dp"
android:text="找回密码"
android:textSize="25dp" />
<TextView
android:id="@+id/textview"
android:layout_below="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginLeft="20dp"
android:textSize="25dp"
android:text="邮箱或⼿机号"
/>
<EditText
android:id="@+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:hint="请输⼊邮箱或⼿机号"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edittext"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="#0099C"
android:text="提交" />
</RelativeLayout>
单击找回密码所得界⾯
总结
到此这篇关于Android中Activity组件实例介绍的⽂章就介绍到这了,更多相关Android Activity组件内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!。

相关文档
最新文档