【Android】【移动应用开发】APP案列

1. 通讯录功能实现

⚪页面布局代码如下:
activity_main.xml(主界面布局代码)

<?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">
<!--    打电话-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:background="#000">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="通讯录"
            android:textColor="#fff"
            android:textSize="20sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvadd"
            android:layout_alignParentRight="true"
            android:layout_marginRight="20dp"
            android:layout_marginTop="6dp"
            android:text="添加"
            android:textColor="#fff"
            android:textSize="18sp"/>

    </RelativeLayout>

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:dividerHeight="2dp"
        android:id="@+id/lv"/>

</LinearLayout>

在这里插入图片描述

activity_add_layout.xml(添加联系人界面布局代码)

<?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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:background="#000">

        <ImageView
            android:layout_width="30dp"
            android:layout_height="25dp"
            android:layout_marginTop="5dp"
            android:id="@+id/imgreturn"
            android:layout_marginLeft="10dp"
            android:src="@mipmap/row_left"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@+id/imgreturn"
            android:text="返回"
            android:textSize="20sp"
            android:textColor="#fff"/>
    </RelativeLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="vertical">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edtName"
            android:hint="请输入姓名"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edtTel"
            android:hint="请输入电话号码"/>
    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_margin="10dp">

        <ImageView
            android:layout_width="40dp"
            android:layout_height="120dp"
            android:src="@mipmap/row_left"
            android:id="@+id/imgleft"/>

        <ImageView
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:id="@+id/img"
            android:src="@mipmap/liubei"/>

        <ImageView
            android:layout_width="40dp"
            android:layout_height="120dp"
            android:src="@mipmap/row_right"
            android:layout_alignParentRight="true"
            android:id="@+id/imgright"/>
    </RelativeLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <Button
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:id="@+id/btnreset"
            android:text="重置"
            android:textSize="20sp"/>
        <Button
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:id="@+id/btnok"
            android:text="添加"
            android:textSize="20sp"/>
    </LinearLayout>
</LinearLayout>

在这里插入图片描述

item.xml(模板布局代码)

<?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="horizontal">

        <ImageView
            android:layout_width="60dp"
            android:layout_height="80dp"
            android:id="@+id/lxrimg"
            android:src="@mipmap/liubei"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lxrname"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="10dp"
            android:text="刘备"
            android:textSize="20sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lxrtel"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="10dp"
            android:text="12345678"
            android:textSize="20sp"/>
    </LinearLayout>
</LinearLayout>

在这里插入图片描述

⚪Java代码如下:
Main_Activity.java(主界面功能实现代码)

package com.example.ceshi;


import androidx.appcompat.app.AppCompatActivity;
import androidx.collection.ArraySet;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
   
    private ListView lv;
    private TextView add;
    private SimpleAdapter adapter = null;
    private ArrayList<Person> persons = new ArrayList<Person>();

    private boolean flag = false;
    private ArrayList<HashMap<String, Object>> lxrList = new ArrayList<HashMap<String, Object>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
		
        Intent intent = this.getIntent();
        // 从add_layout的intent附加信息中取出添加的联系人信息
        persons = (ArrayList<Person>) intent.getSerializableExtra("persons");
        //标记已经通过add_layout界面添加了联系人信息,即persons不为空指针
        flag = intent.getBooleanExtra("flag", false);
        if (flag) {
   
            for (int i = 0; i < persons.size(); i++) {
   
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("imgid", persons.get(i).getImgid());
                map.put("name", persons.get(i).getName());
                map.put("tel", persons.get(i).getTel());
                lxrList.add(map);
            }
            String[] from = {
   "imgid", "name", "tel"};
            int[] to = {
   R.id.lxrimg, R.id.lxrname, R.id.lxrtel};
            adapter = new SimpleAdapter(this, lxrList, R.layout.item, from, to);
            lv.setAdapter(adapter);
        }
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   
                Intent itdail = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+persons.get(position).getTel()) );
                MainActivity.this.startActivity(itdail);
            }
        });
        // 添加监听事件
        add.setOnClickListener(new View.OnClickListener() {
   
            @Override
            public void onClick(View view) {
   
                Intent intent = new Intent(MainActivity.this, add_layout.class);
                startActivity(intent);
            }
        });
    }

    void initView() {
   
        lv = this.findViewById(R.id.lv);
        add = this.findViewById(R.id.tvadd);
    }
}

add_layout.java(添加联系人功能代码)

package com.example.ceshi;
import android.app.Activity;
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;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class add_layout extends AppCompatActivity {
   
    EditText edtName, edtTel;
    ImageView imgReturn, imgLeft, imgRight, img;
    int imgId[] = {
    R.mipmap.liubei,R.mipmap.caocao,R.mipmap
60个Android开发精典案例 Android软件源码: 2-1(Activity生命周期) 3-1(Button与点击监听器) 3-10-1(列表之ArrayAdapter适配) 3-10-2(列表之SimpleAdapter适配) 3-11(Dialog对话框) 3-12-5(Activity跳转与操作) 3-12-6(横竖屏切换处理) 3-3(ImageButton图片按钮) 3-4(EditText文本编辑) 3-5(CheckBox与监听) 3-6(RadioButton与监听) 3-7(ProgressBar进度条) 3-8(SeekBar 拖动条) 3-9(Tab分页式菜单) 4-10(可视区域) 4-11-1(Animation动画) 4-11-2-1(动态位图) 4-11-2-2(帧动画) 4-11-2-3(剪切图动画) 4-13(操作游戏主角) 4-14-1(矩形碰撞) 4-14-2(圆形碰撞) 4-14-4(多矩形碰撞) 4-14-5(Region碰撞检测) 4-15-1(MediaPlayer音乐) 4-15-2(SoundPool音效) 4-16-1(游戏保存之SharedPreference) 4-16-2(游戏保存之Stream) 4-3(View游戏框架) 4-4(SurfaceView游戏框架) 4-7-1(贝塞尔曲线) 4-7-2(Canvas画布) 4-8(Paint画笔) 4-9(Bitmap位图渲染与操作) 5-1(飞行射击游戏实战) 6-1(360°平滑游戏摇杆) 6-10-1(Socket协议) 6-10-2(Http协议) 6-11(本地化与国际化) 6-2(多触点缩放位图) 6-3(触屏手势识别) 6-4(加速度传感器) 6-5(9patch工具)] 6-6(截屏) 6-8(游戏视图与系统组件) 6-9(蓝牙对战游戏) 7-10-1(遍历Body) 7-10-2(Body的m_userData) 7-11(为Body施加力) 7-12(Body碰撞监听) 7-13-1(距离关节) 7-13-2(旋转关节) 7-13-3(齿轮关节) 7-13-4(滑轮关节) 7-13-5-1(通过移动关节移动Body) 7-13-5-2(通过移动关节绑定两个Body动作) 7-13-6(鼠标关节-拖拽Body) 7-14(AABB获取Body) 7-4(Box2d物理世界) 7-5在物理世界中添加矩形) 7-7(添加自定义多边形) 7-9(在物理世界中添加圆形) 8-1(迷宫小球) 8-2(堆房子)
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码字小萌新♡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值