安卓开发学习之简单的Activity实战输入姓名返回电话号码

简介: 简单的Activity实战输入姓名返回电话号码

# 输入姓名返回电话号码


## 大概作业要求:


### 查询界面:

屏幕截图 2022-04-09 143551.png



### 应用选择界面:


屏幕截图 2022-04-09 143602.png


#### 1.


屏幕截图 2022-04-09 143618.png

#### 2.


屏幕截图 2022-04-09 143626.png

### 返回结果



屏幕截图 2022-04-09 143643.png


# 第一步,实现三个界面布局


## activity_main.xml


```xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout 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="vertical"

   tools:context=".MainActivity">

<RelativeLayout

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   android:orientation="horizontal"

   android:paddingLeft="20dp"

   android:paddingTop="20dp">


   <TextView

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:id="@+id/name"

       android:text="@string/xingming"

       android:textSize="30dp">


   </TextView>


   <EditText

       android:id="@+id/edit"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:layout_marginLeft="10dp"

       android:layout_toRightOf="@id/name"

       android:textSize="20dp">


   </EditText>


   <Button

       android:id="@+id/button"

       android:layout_width="wrap_content"

       android:layout_height="75dp"

       android:layout_below="@id/name"

       android:layout_marginTop="25dp"

       android:onClick="Search"

       android:text="@string/chaxun"

       android:textColor="@color/black"

       android:textSize="20dp"

       app:backgroundTint="#CFCCCC"></Button>

   <TextView

       android:layout_width="wrap_content"

       android:layout_height="50dp"

       android:id="@+id/num"

       android:layout_marginTop="50dp"

       android:layout_below="@id/button"

       android:text="@string/number"

       android:textSize="20dp"></TextView>

   <TextView

       android:layout_width="wrap_content"

       android:layout_height="50dp"

       android:id="@+id/numhao"

       android:layout_marginTop="50dp"

       android:layout_below="@id/button"

       android:layout_toRightOf="@id/num"

       android:textSize="20dp"></TextView>



</RelativeLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

```


## 界面:


屏幕截图 2022-04-09 144904.png


## activity_pickcontact.xml


```xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout

   xmlns:android="http://schemas.android.com/apk/res/android"

   xmlns:app="http://schemas.android.com/apk/res-auto"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   xmlns:tools="http://schemas.android.com/tools"

   tools:context=".PickContact">

   <RelativeLayout

       android:layout_width="match_parent"

       android:layout_height="match_parent">

       <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:id="@+id/xingm"

           android:layout_marginTop="50dp"

           android:text="您要查询的姓名:"

           android:textSize="20dp"></TextView>

       <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:id="@+id/xingming"

           android:layout_marginTop="50dp"

           android:layout_toRightOf="@id/xingm"

           android:textSize="20dp"></TextView>

       <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:id="@+id/dianhua"

           android:layout_below="@id/xingm"

           android:layout_marginTop="20dp"

           android:text="请输入电话号码:"

           android:textSize="20dp"></TextView>

       <EditText

           android:layout_width="match_parent"

           android:layout_height="wrap_content"

           android:id="@+id/number2"

           android:layout_marginTop="70dp"

           android:layout_toRightOf="@id/dianhua"

           android:textSize="30dp"></EditText>


       <Button

           android:layout_width="110dp"

           android:layout_height="wrap_content"

           android:layout_below="@id/dianhua"

           android:layout_marginTop="30dp"

           android:layout_marginLeft="20dp"

           android:onClick="Fanhui"

           android:text="返回"

           android:textColor="@color/black"

           android:textSize="20dp"

           app:backgroundTint="#DAD6D6"></Button>

   </RelativeLayout>



</androidx.constraintlayout.widget.ConstraintLayout>

```


## 界面:


屏幕截图 2022-04-09 144919.png


## activity_anotherpick.xml


```xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

   xmlns:app="http://schemas.android.com/apk/res-auto"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   xmlns:tools="http://schemas.android.com/tools"

   tools:context=".anotherPick">

   <RelativeLayout

       android:layout_width="match_parent"

       android:layout_height="match_parent">

       <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:id="@+id/xingm"

           android:layout_marginTop="50dp"

           android:text="您要查询的姓名:"

           android:textSize="20dp"></TextView>

       <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:id="@+id/xingming"

           android:layout_marginTop="50dp"

           android:layout_toRightOf="@id/xingm"

           android:textSize="20dp"></TextView>

       <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:id="@+id/dianhua"

           android:layout_below="@id/xingm"

           android:layout_marginTop="20dp"

           android:text="请输入电话号码:"

           android:textSize="20dp"></TextView>


       <EditText

           android:id="@+id/number1"

           android:layout_width="match_parent"

           android:layout_height="wrap_content"

           android:layout_marginLeft="0dp"

           android:layout_marginTop="70dp"

           android:layout_toRightOf="@id/dianhua"

           android:textSize="30dp"></EditText>


       <Button

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:layout_below="@id/dianhua"

           android:layout_marginTop="30dp"

           android:layout_marginLeft="20dp"

           android:onClick="Fanhui"

           android:text="返回"

           android:textSize="20dp"

           android:textColor="@color/black"

           app:backgroundTint="#D5D2D2"></Button>

   </RelativeLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

```


## 界面:


屏幕截图 2022-04-09 144930.png


# 第二步,实现具体功能


## MainActivity


```java

package com.example.myapplication;


import androidx.annotation.Nullable;

import androidx.appcompat.app.AppCompatActivity;


import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

   private EditText et_name;


   @Override

   protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

       et_name=(EditText)findViewById(R.id.edit);

   }

   public void Search(View v){

       Intent intent=new Intent("abc");

       intent.putExtra("name",et_name.getText().toString().trim());

       String title="联系人";

       Intent chooser=Intent.createChooser(intent,title);

       if(intent.resolveActivity(getPackageManager())!=null){

           startActivityForResult(chooser,200);

       }else {

           Toast.makeText(this,"没有可以响应action的activity",Toast.LENGTH_SHORT).show();

       }

   }

   @Override

   protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {


       if (data != null) {

           if (requestCode == 200) {

               if (resultCode == 100) {

                   String eid = data.getStringExtra("haoma");

                   TextView view = (TextView) findViewById(R.id.numhao);

                   view.setText(eid);

               }else if (resultCode == 200){

                   String eid = data.getStringExtra("haoma");

                   TextView view = (TextView) findViewById(R.id.numhao);

                   view.setText(eid);

               }

           }

       }

       super.onActivityResult(requestCode, resultCode, data);

   }


}

```


## PickContact


```java

package com.example.myapplication;


import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;

import android.widget.TextView;


import androidx.appcompat.app.AppCompatActivity;


public class PickContact extends AppCompatActivity {

   @Override

   protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_pickcontact);

       Intent intent = getIntent();

       String name = intent.getStringExtra("name");

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

       xingming.setText(name);

   }

   public  void Fanhui(View v){

       EditText number = (EditText) findViewById(R.id.number2);

       Intent intent = new Intent();

       intent.putExtra("haoma", number.getText().toString());

       setResult(200, intent);

       finish();

   }

}

```


## anotherPick


```java

package com.example.myapplication;


import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;

import android.widget.TextView;


import androidx.appcompat.app.AppCompatActivity;


public class anotherPick extends AppCompatActivity {

   @Override

   protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_anotherpick);

       Intent intent = getIntent();

       String name = intent.getStringExtra("name");

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

       xingming.setText(name);


   }


   public void Fanhui(View v) {

       EditText number = (EditText) findViewById(R.id.number1);

       Intent intent = new Intent();

       intent.putExtra("haoma", number.getText().toString());

       setResult(100, intent);

       finish();

   }

}

```


## AndroidManifest.xml


```xml

<?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/Theme.MyApplication">

       <activity

           android:name=".anotherPick"

           android:label="anotherPick">

           <intent-filter android:label="anotherPick">

               <action android:name="abc"></action>

               <category android:name="android.intent.category.DEFAULT"></category>

           </intent-filter>

       </activity>

       <activity android:name=".PickContact"

           android:label="PickContact">

           <intent-filter android:label="PickContact">

               <action android:name="abc"></action>

               <category android:name="android.intent.category.DEFAULT"></category>

           </intent-filter>

       </activity>




       <activity android:name=".MainActivity">

           <intent-filter>

               <action android:name="android.intent.action.MAIN" />


               <category android:name="android.intent.category.LAUNCHER" />

           </intent-filter>

       </activity>

   </application>


</manifest>

```


## 总结:

 通过学习Activity和Intent完成一个简单的案例。

目录
相关文章
|
6天前
|
IDE Android开发 iOS开发
探索Android与iOS开发的差异:平台选择对项目成功的影响
【9月更文挑战第27天】在移动应用开发的世界中,Android和iOS是两个主要的操作系统平台。每个系统都有其独特的开发环境、工具和用户群体。本文将深入探讨这两个平台的关键差异点,并分析这些差异如何影响应用的性能、用户体验和最终的市场表现。通过对比分析,我们将揭示选择正确的开发平台对于确保项目成功的重要作用。
|
19天前
|
Android开发 开发者 Kotlin
探索安卓开发中的新特性
【9月更文挑战第14天】本文将引导你深入理解安卓开发领域的一些最新特性,并为你提供实用的代码示例。无论你是初学者还是经验丰富的开发者,这篇文章都会给你带来新的启示和灵感。让我们一起探索吧!
|
5天前
|
Java Maven 开发工具
第一个安卓项目 | 中国象棋demo学习
本文是作者关于其第一个安卓项目——中国象棋demo的学习记录,展示了demo的运行结果、爬坑记录以及参考资料,包括解决Android Studio和maven相关问题的方法。
第一个安卓项目 | 中国象棋demo学习
|
2天前
|
开发框架 移动开发 Android开发
安卓与iOS开发中的跨平台解决方案:Flutter入门
【9月更文挑战第30天】在移动应用开发的广阔舞台上,安卓和iOS两大操作系统各自占据半壁江山。开发者们常常面临着选择:是专注于单一平台深耕细作,还是寻找一种能够横跨两大系统的开发方案?Flutter,作为一种新兴的跨平台UI工具包,正以其现代、响应式的特点赢得开发者的青睐。本文将带你一探究竟,从Flutter的基础概念到实战应用,深入浅出地介绍这一技术的魅力所在。
18 7
|
6天前
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台解决方案
【9月更文挑战第27天】在移动应用开发的广阔天地中,安卓和iOS两大操作系统如同双子星座般耀眼。开发者们在这两大平台上追逐着创新的梦想,却也面临着选择的难题。如何在保持高效的同时,实现跨平台的开发?本文将带你探索跨平台开发的魅力所在,揭示其背后的技术原理,并通过实际案例展示其应用场景。无论你是安卓的忠实拥趸,还是iOS的狂热粉丝,这篇文章都将为你打开一扇通往跨平台开发新世界的大门。
|
3天前
|
缓存 Java Linux
探索安卓开发:从新手到专家的旅程
【9月更文挑战第30天】在这篇文章中,我们将一起踏上一段激动人心的旅程,探索安卓开发的广阔世界。无论你是刚入门的新手,还是希望提升技能的开发者,本文都将为你提供宝贵的知识和指导。我们将深入探讨安卓开发的基础知识、关键概念、实用工具和最佳实践,帮助你在安卓开发领域取得更大的成功。让我们一起开启这段精彩的旅程吧!
|
3天前
|
监控 安全 Java
Kotlin 在公司上网监控中的安卓开发应用
在数字化办公环境中,公司对员工上网行为的监控日益重要。Kotlin 作为一种基于 JVM 的编程语言,具备简洁、安全、高效的特性,已成为安卓开发的首选语言之一。通过网络请求拦截,Kotlin 可实现网址监控、访问时间记录等功能,满足公司上网监控需求。其简洁性有助于快速构建强大的监控应用,并便于后续维护与扩展。因此,Kotlin 在安卓上网监控应用开发中展现出广阔前景。
7 1
|
12天前
|
Android开发 开发者 索引
Android实战经验之如何使用DiffUtil提升RecyclerView的刷新性能
本文介绍如何使用 `DiffUtil` 实现 `RecyclerView` 数据集的高效更新,避免不必要的全局刷新,尤其适用于处理大量数据场景。通过定义 `DiffUtil.Callback`、计算差异并应用到适配器,可以显著提升性能。同时,文章还列举了常见错误及原因,帮助开发者避免陷阱。
34 9
|
14天前
|
Android开发 开发者
安卓开发中的自定义视图:从入门到精通
【9月更文挑战第19天】在安卓开发的广阔天地中,自定义视图是一块充满魔力的土地。它不仅仅是代码的堆砌,更是艺术与科技的完美结合。通过掌握自定义视图,开发者能够打破常规,创造出独一无二的用户界面。本文将带你走进自定义视图的世界,从基础概念到实战应用,一步步展示如何用代码绘出心中的蓝图。无论你是初学者还是有经验的开发者,这篇文章都将为你打开一扇通往创意和效率的大门。让我们一起探索自定义视图的秘密,将你的应用打造成一件艺术品吧!
38 10
|
7天前
|
存储 开发工具 Android开发
使用.NET MAUI开发第一个安卓APP
【9月更文挑战第24天】使用.NET MAUI开发首个安卓APP需完成以下步骤:首先,安装Visual Studio 2022并勾选“.NET Multi-platform App UI development”工作负载;接着,安装Android SDK。然后,创建新项目时选择“.NET Multi-platform App (MAUI)”模板,并仅针对Android平台进行配置。了解项目结构,包括`.csproj`配置文件、`Properties`配置文件夹、平台特定代码及共享代码等。
下一篇
无影云桌面