Activity
- 生命周期:
onCreate -> OnStart -> OnResum()-> [Activity is Running] ->onPause() -> onStop() -> onDestory()
当Activity 处于onStop 调用onRestart() 方法 重新进行活动
实例熟悉Activity的调用/**
- 功能: 点击忘记密码 激活一个Activity ,跳转到忘记密码界面
- 点击close 返回主界面
- 自定义的PasswordActivity 继承Android.app.Activty
- 自定义的PasswordActivity 需要在manifiest 目录下面对Activity进行注册
- Activity 、Service 之间通信 主要是通过 Intent 进行通信:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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:background="@drawable/biaoge"
android:stretchColumns="0,3"
tools:context=".MainActivity">
<!--第一行-->
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="200dp"
>
<TextView />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="账 号:"
android:gravity="center_horizontal"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="邮箱或者手机号"
/>
<TextView />
</TableRow>
<!--第二行-->
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
>
<TextView />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="密 码:"
android:gravity="center_horizontal"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="输入6-16位数字或字母"
/>
<TextView />
</TableRow>
<!--第三行-->
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注 册"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF8247"
android:text="登 录"/>
<TextView />
</TableRow>
<!--第四行-->
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
>
<TextView />
<TextView />
<TextView
android:id="@+id/wang_mima"
android:text="忘记密码?"
android:textColor="#FF4500"
android:gravity="right"
/>
<TextView />
</TableRow>
</TableLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:background="@drawable/beijing"
android:padding="5dp"
tools:context="com.example.myapplication.PasswordActivity">
<ImageButton
android:id="@+id/close"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@drawable/guanbi"
android:background="#0000"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="10dp"
android:text="你的邮箱或手机号"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:hint="请输入邮箱或手机号"/>
<Button
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#FF8247"
android:text="提交"/>
</LinearLayout>
<!--manifest.xml 配置activity -->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 自定义Activity 需要在mainifests 配置声明 -->
<activity android:name=".PasswordActivity"></activity>
</application>
</manifest>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
/**
* 功能: 点击忘记密码 激活一个Activity ,跳转到忘记密码界面
* 点击close 返回主界面
*
* */
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1. 忘记密码添加一个点击监听按钮
TextView tv = findViewById(R.id.wang_mima);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Android 通过Intent 机制进行Activity/Service 或者应用程序间通信
Intent passwordIntent = new Intent(MainActivity.this, PasswordActivity.class);
startActivity(passwordIntent);
}
});
}
}
package com.example.myapplication;
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(R.layout.activity_password);
ImageButton imageButton = findViewById(R.id.close);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 返回主界面
finish();
}
});
}
}
多个Activity 之间通信
Intent 可以进行Activity 之间的通信 但是无法进行数据保存
所以在多个Activity 通信当中借助Bundle 容器
Bundle 是一个 容器
data -> putXXX() ->Bundle ->putExtras()-> Intent -> startActivty()->Activity
- 将data 通过putXXX 方法放入到Bundle 包裹中
- 通过 Intnet.putExtras() 将Bundle 对象进行携带
- startActivity()
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.myapplication.AddressActivity">
<ImageView
android:id="@+id/top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/top"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/top"
/>
<TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/site"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
/>
<TextView
android:id="@+id/site"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<!-- 导航条图片-->
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/top"/>
<!-- 地区-->
<EditText
android:id="@+id/et_site1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入所在地区"
android:paddingTop="50dp"/>
<!-- 街道-->
<EditText
android:id="@+id/et_site2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et_site1"
android:hint="请输入街道"/>
<!-- 详细地址-->
<EditText
android:id="@+id/et_site3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et_site2"
android:hint="请输入详细地址"/>
<!--姓名-->
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et_site3"
android:hint="请输入收货人姓名"/>
<!--电话-->
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et_name"
android:hint="请输入收货人联系电话"/>
<!--邮箱-->
<EditText
android:id="@+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et_phone"
android:hint="请输入邮编"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:background="#FF5000"
android:text="保存"
/>
</RelativeLayout>
package com.example.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class AddressActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_address);
// 类加载器this 自动识别哪个 Intnet
Intent intnet = getIntent();
Bundle bundle = intnet.getExtras();
TextView name = (TextView) findViewById(R.id.name);//获取显示姓名的TextView组件
name.setText(bundle.getString("name"));//获取输入的姓名并显示到TextView组件中
TextView phone = (TextView) findViewById(R.id.phone);//获取显示手机号码的TextView组件
phone.setText(bundle.getString("phone"));//获取输入的电话号码并显示到TextView组件中
TextView site = (TextView) findViewById(R.id.site);//获取显示地址的TextView组件
//获取输入的地址并显示到TextView组件中
site.setText(bundle.getString("site1") + bundle.getString("site2") + bundle.get("site3"));
}
}
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* 功能: 收货地址保存的
*
*
* */
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button saveButton = findViewById(R.id.btn);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 获取数据保存
String site1 = ((EditText)findViewById(R.id.et_site1)).getText().toString();
String site2 = ((EditText)findViewById(R.id.et_site2)).getText().toString();
String site3 = ((EditText)findViewById(R.id.et_site3)).getText().toString();
String email = ((EditText)findViewById(R.id.et_email)).getText().toString();
String name = ((EditText)findViewById(R.id.et_name)).getText().toString();
String phone = ((EditText)findViewById(R.id.et_phone)).getText().toString();
if (!"".equals(site1) && !"".equals(site2) && !"".equals(site3)&&
!"".equals(name) && !"".equals(phone) &&!"".equals(email) ) {
//将输入的信息保存到Bundle中,通过Intent传递到另一个Activity当中并显示出来
Intent intent = new Intent(MainActivity.this, AddressActivity.class);
//创建并实例化一个Bundle对象
Bundle bundle = new Bundle();
bundle.putCharSequence("name", name);//保存姓名
bundle.putCharSequence("phone", phone);//保存手机号码
bundle.putCharSequence("site1", site1);//保存所在地区信息
bundle.putCharSequence("site2", site2);//保存所在街道信息
bundle.putCharSequence("site3", site3);//保存详细地址信息
intent.putExtras(bundle);//将Bundle对象添加到Intent对象中
startActivity(intent);//启动Activity
}else {
Toast.makeText(MainActivity.this,"请将收货地址填写完整!",Toast.LENGTH_SHORT).show();
}
}
});
}
}
调用另外一个Activity 并有返回结果
同样也 借助Bundle + Intnet 不同是不是调用startActivity () 方法是通过调用startActivityForResult() 获取其返回值
- mainActivity startActivityForResult 方法启动 带有标志参数requestCode
- HeadActivity setResult(responseCode, Intnet) 返回数据
- MainActivity 中重写onActivityResult 通过对比 requestCode 与responseCode 获取返回结果
public void startActivityForResult(Intnet intnet, int RequestCode)
请求码 表示 这个Activity来源
package com.example.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
/**
* 添加一个Adpater 适配器
*/
public class HeadActivity extends Activity {
// 定义并初始化保存头像id的数组
public int[] imageId = new int[]{R.drawable.touxiang1, R.drawable.touxiang2,
R.drawable.touxiang3, R.drawable.touxiang4, R.drawable.touxiang5
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_head);
// 添加一个网格适配器
GridView gridView = findViewById(R.id.gridView);
BaseAdapter baseAdapter = new BaseAdapter() {
@Override
public int getCount() {
return imageId.length;
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ImageView imageview; //声明ImageView的对象
if (view == null) {
imageview = new ImageView(HeadActivity.this); //实例化ImageView的对象
/*************设置图像的宽度和高度******************/
imageview.setAdjustViewBounds(true);
imageview.setMaxWidth(158);
imageview.setMaxHeight(150);
/**************************************************/
imageview.setPadding(5, 5, 5, 5); //设置ImageView的内边距
} else {
imageview = (ImageView) view;
}
//为ImageView设置要显示的图片
imageview.setImageResource(imageId[i]);
return imageview; //返回ImageView
}
};
gridView.setAdapter(baseAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putInt("imageId", imageId[i]);
intent.putExtras(bundle);
// HeadActivity 设置返回结果 与MainActivity startActivityForResult 相同
setResult(0x11, intent);
System.out.println("头像返回结果");
finish();
}
});
}
}
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
/**
* 功能: 收货地址保存的
*
*
* */
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button= findViewById(R.id.btn_head);//获取选择头像按钮
button.setOnClickListener(new View.OnClickListener() { //为按钮创建单机事件
@Override
public void onClick(View v) {
//创建Intent对象
Intent intent=new Intent(MainActivity.this,HeadActivity.class);
startActivityForResult(intent, 0x11);//启动intent对应的Activity
}
});
}
// mainActtivity 中重写 结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//判断是否为待处理的结果
if(requestCode==0x11 && resultCode==0x11) {
Bundle bundle = data.getExtras();
int imageViewId = bundle.getInt("imageId");
ImageView iv= findViewById(R.id.imageView); //获取布局文件中添加的ImageView组件
iv.setImageResource(imageViewId); //显示选择的头像
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.myapplication.HeadActivity">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:horizontalSpacing="3dp"
android:verticalSpacing="3dp"
android:numColumns="4">
</GridView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:background="@drawable/beijing"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/touxiang"/>
<Button
android:id="@+id/btn_head"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:text="选择头像"
/>
</LinearLayout>
activity 重用Fragment
fragment :碎片, 设置UI 多个Fragment 嵌入Activity 中,生命周期最长与当前Activity 生命周期一样。
- Activity 添加Fragment:
直接在 XML添加Fragment
在 main_activity.xml 中水平放置
<fragment
android:id="@+id/fragment"
android:name="com.mingrisoft.WeChat_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
- 在运行中动态添加Fragment
package com.example.myapplication;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
// 动态加载时候 有事务概念 FragmentTransaction 将Fragment add
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment detailFragment = new Fragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.btn_head, detailFragment);
// 提交事务
ft.commit();
}
}
实例: 通过添加四个不懂Fragment 显示微信上不同界面:
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
/**
* 功能: d点击不同Freagment 显示不同图片
*
*
* */
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取四个不同ImageView
ImageView imageView1 = findViewById(R.id.image1);
ImageView imageView2 = findViewById(R.id.image2);
ImageView imageView3 = findViewById(R.id.image3);
ImageView imageView4 = findViewById(R.id.image4);
imageView1.setOnClickListener(L);//为第一个导航图片添加单机事件
imageView2.setOnClickListener(L);//为第二个导航图片添加单机事件
imageView3.setOnClickListener(L);//为第三个导航图片添加单机事件
imageView4.setOnClickListener(L);//为第四个导航图片添加单机事件
}
View.OnClickListener L = new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment f = null;
switch (view.getId()) { //通过获取点击的id判断点击了哪个张图片
case R.id.image1:
f = new WeChat_Fragment(); //创建第一个Fragment
break;
case R.id.image2:
f = new Message_Fragment();//创建第二个Fragment
break;
case R.id.image3:
f = new Find_Fragment();//创建第三个Fragment
break;
case R.id.image4:
f = new Me_Fragment();//创建第四个Fragment
break;
default:
break;
}
ft.replace(R.id.fragment, f);
ft.commit();
}
};
}
package com.example.myapplication;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
/**
* Created by Administrator on 2016/2/18.
*/
public class Find_Fragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.find_fragment,null);
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<!-- 添加为 WeChat Fragment -->
<fragment
android:id="@+id/fragment"
android:name="com.example.myapplication.WeChat_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<ImageView
android:id="@+id/image1"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/bottom_1"
/>
<ImageView
android:id="@+id/image2"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/bottom_2"
/>
<ImageView
android:id="@+id/image3"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/bottom_3"
/>
<ImageView
android:id="@+id/image4"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/bottom_4"
/>
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/wo"
android:scaleType="fitXY"/>
</RelativeLayout>