第四章 基本程序单元Activity

1 Activity概述

Activity的四种状态:

 Activity的生命周期:

2 创建、配置、启动和关闭Activity

启动Activity 

 关闭Activity finish()

刷新Activity():onCreate(null);

    <TableRow
        android:paddingTop="350dp"
        android:paddingLeft="@dimen/paddingLeft">
        <TextView/>
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="账  号:"
            android:textSize="18sp"/>
        <EditText
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:hint="邮箱或者手机号"/>

    </TableRow>

    <TableRow
        android:paddingLeft="@dimen/paddingLeft">
        <TextView/>
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="密  码:"
            android:textSize="18sp"/>
        <EditText
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:hint="输入6-16位数字或字母"/>
    </TableRow>

    <TableRow
        android:paddingLeft="@dimen/paddingLeft">
        <TextView/>
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="注册"
            />
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="登录"/>

    </TableRow>

    <TableRow
        android:paddingLeft="@dimen/paddingLeft">
        <TextView/>
        <TextView/>
        <TextView
            android:id="@+id/forgetPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="忘记密码"
            android:layout_gravity="right"/>
    </TableRow>

    <ImageButton
        android:layout_marginTop="15dp"
        android:id="@+id/guanbi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:src="@drawable/guanbi"/>

    <TextView
        android:paddingTop="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你的邮箱或手机号"
        android:textSize="20sp"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="请输入您的邮箱或手机号"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"
        android:textSize="30sp"/>
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = findViewById(R.id.forgetPassword);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, DetailActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgetpassword);

        ImageButton imageButton = findViewById(R.id.guanbi);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

3 多个Activity的使用

3.1 使用Bundle在Activity之间交换数据

 

 

 

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@mipmap/top"
        android:scaleType="fitXY"/>

    <EditText
        android:id="@+id/city"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入城市"/>

    <EditText
        android:id="@+id/street"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入街道"/>

    <EditText
        android:id="@+id/detailAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入详细地址"/>

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入姓名"/>

    <EditText
        android:id="@+id/phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入电话"/>

    <EditText
        android:id="@+id/postalCode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入邮编"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"/>
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText city = findViewById(R.id.city);
        EditText street = findViewById(R.id.street);
        EditText detail = findViewById(R.id.detailAddress);
        EditText name = findViewById(R.id.name);
        EditText phone = findViewById(R.id.phone);
        EditText code = findViewById(R.id.postalCode);
        Button btn = findViewById(R.id.button);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String city = ((EditText)findViewById(R.id.city)).getText().toString();
                String street = ((EditText)findViewById(R.id.street)).getText().toString();
                String detail = ((EditText)findViewById(R.id.detailAddress)).getText().toString();
                String name = ((EditText)findViewById(R.id.name)).getText().toString();
                String phone = ((EditText)findViewById(R.id.phone)).getText().toString();
                String code = ((EditText)findViewById(R.id.postalCode)).getText().toString();

                if(!"".equals(city) && !"".equals(street) && !"".equals(detail) && !"".equals(name)
                && !"".equals(phone) && !"".equals(code)) {
                    Intent intent = new Intent(MainActivity.this, Display.class);
                    Bundle bundle = new Bundle();
                    bundle.putCharSequence("name", name);
                    bundle.putCharSequence("phone", phone);
                    bundle.putCharSequence("code", code);
                    bundle.putCharSequence("city", city);
                    bundle.putCharSequence("street", street);
                    bundle.putCharSequence("detail", detail);
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    Toast.makeText(MainActivity.this, "请输入完整的信息", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);

        TextView textView = findViewById(R.id.textView);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String name = bundle.getString("name");
        String phone = bundle.getString("phone");
        String code = bundle.getString("code");
        String city = bundle.getString("city");
        String street = bundle.getString("street");
        String detail = bundle.getString("detail");

        textView.setText(name + "  " + phone + "  " + city + street + detail + "  " + code);
    }

3.2 使用startActivityForResult()

 

 

    <ImageView
        android:id="@+id/touxiang"
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:src="@mipmap/touxiang"/>

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:text="选择头像"/>
    <GridView
        android:id="@+id/gridView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:horizontalSpacing="5dp"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"/>
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="90dp"
        android:layout_height="90dp" />

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.touxiang);
        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, HeadActivity.class);
                startActivityForResult(intent, 0x11);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0x11 && resultCode == 0x77) {
            Bundle bundle = data.getExtras();
            int imageId = bundle.getInt("imageId");
            imageView.setImageResource(imageId);
        }
    }
}

public class HeadActivity extends AppCompatActivity {

    private int[] images = {R.mipmap.touxiang1, R.mipmap.touxiang2, R.mipmap.touxiang3,
            R.mipmap.touxiang4, R.mipmap.touxiang5};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_head);

        List<Map<String, Object>> listitem = new ArrayList<>();
        for(int i = 0; i < images.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("image", images[i]);
            listitem.add(map);
        }

        GridView gridView = findViewById(R.id.gridView);
        SimpleAdapter simpleAdapter = new SimpleAdapter(HeadActivity.this, listitem, R.layout.cell,
                new String[]{"image"}, new int[]{R.id.imageView});
        gridView.setAdapter(simpleAdapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = getIntent();
                Bundle bundle = new Bundle();
                bundle.putInt("imageId", images[position]);
                intent.putExtras(bundle);
                setResult(0x77, intent);
                finish();
            }
        });
    }
}

4 使用Fragment

Fragment的生命周期:

xml方式添加Fragment:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".ListFragment">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ListFragment 内容"
        android:textSize="20sp"/>

</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".DetailFragment">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DetailFragment 内容"
        android:textSize="20sp"/>

</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.example.listfragment.ListFragment"/>

    <fragment
        android:id="@+id/detail"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.example.listfragment.DetailFragment"/>

</LinearLayout>
package com.example.listfragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ListFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list, container, false);
        return view;
    }
}
package com.example.listfragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class DetailFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_detail, container, false);
        return view;
    }
}

Java方式添加Fragment:

package com.example.listfragment;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn1 = findViewById(R.id.btn1);
        Button btn2 = findViewById(R.id.btn2);

        DetailFragment detailFragment = new DetailFragment();
        ListFragment listFragment = new ListFragment();

        getSupportFragmentManager().beginTransaction().replace(R.id.fl, listFragment).commit();

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.fl, listFragment).commit();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.fl, detailFragment).commit();
            }
        });

    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom"/>
    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/image1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@mipmap/bottom_1"/>

        <ImageView
            android:id="@+id/image2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@mipmap/bottom_2"/>

        <ImageView
            android:id="@+id/image3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@mipmap/bottom_3"/>

        <ImageView
            android:id="@+id/image4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@mipmap/bottom_4"/>
    </LinearLayout>

</RelativeLayout>
package com.example.fragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView img1 = findViewById(R.id.image1);
        ImageView img2 = findViewById(R.id.image2);
        ImageView img3 = findViewById(R.id.image3);
        ImageView img4 = findViewById(R.id.image4);

        Fragment1 fragment1 = new Fragment1();
        Fragment2 fragment2 = new Fragment2();
        Fragment3 fragment3 = new Fragment3();
        Fragment4 fragment4 = new Fragment4();

        getSupportFragmentManager().beginTransaction().replace(R.id.frame, fragment1).commit();

        img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.frame, fragment1).commit();
            }
        });

        img2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.frame, fragment2).commit();
            }
        });

        img3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.frame, fragment3).commit();
            }
        });

        img4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.frame, fragment4).commit();
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".Fragment1">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@mipmap/weixin"/>

</FrameLayout>
package com.example.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_1, container, false);
        return view;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值