安卓开发学习之简单的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完成一个简单的案例。

目录
相关文章
|
2月前
|
存储 消息中间件 人工智能
【03】AI辅助编程完整的安卓二次商业实战-本地构建运行并且调试-二次开发改注册登陆按钮颜色以及整体资源结构熟悉-优雅草伊凡
【03】AI辅助编程完整的安卓二次商业实战-本地构建运行并且调试-二次开发改注册登陆按钮颜色以及整体资源结构熟悉-优雅草伊凡
114 3
|
1月前
|
移动开发 前端开发 Android开发
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
235 12
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
1月前
|
移动开发 JavaScript 应用服务中间件
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
203 5
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
1月前
|
移动开发 Rust JavaScript
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
514 4
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
2月前
|
开发工具 Android开发
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
480 11
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
|
2月前
|
存储 消息中间件 人工智能
【08】AI辅助编程完整的安卓二次商业实战-修改消息聊天框背景色-触发聊天让程序异常终止bug牵涉更多聊天消息发送优化处理-优雅草卓伊凡
【08】AI辅助编程完整的安卓二次商业实战-修改消息聊天框背景色-触发聊天让程序异常终止bug牵涉更多聊天消息发送优化处理-优雅草卓伊凡
227 10
【08】AI辅助编程完整的安卓二次商业实战-修改消息聊天框背景色-触发聊天让程序异常终止bug牵涉更多聊天消息发送优化处理-优雅草卓伊凡
|
2月前
|
存储 消息中间件 人工智能
【05】AI辅助编程完整的安卓二次商业实战-消息页面媒体对象(Media Object)布局实战调整-按钮样式调整实践-优雅草伊凡
【05】AI辅助编程完整的安卓二次商业实战-消息页面媒体对象(Media Object)布局实战调整-按钮样式调整实践-优雅草伊凡
107 11
【05】AI辅助编程完整的安卓二次商业实战-消息页面媒体对象(Media Object)布局实战调整-按钮样式调整实践-优雅草伊凡
|
1月前
|
移动开发 Android开发
【03】建立隐私关于等相关页面和内容-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【03】建立隐私关于等相关页面和内容-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
120 0
|
2月前
|
XML 存储 Java
【06】AI辅助编程完整的安卓二次商业实战-背景布局变更增加背景-二开发现页面跳转逻辑-替换剩余图标-优雅草卓伊凡
【06】AI辅助编程完整的安卓二次商业实战-背景布局变更增加背景-二开发现页面跳转逻辑-替换剩余图标-优雅草卓伊凡
101 3
【06】AI辅助编程完整的安卓二次商业实战-背景布局变更增加背景-二开发现页面跳转逻辑-替换剩余图标-优雅草卓伊凡
|
2月前
|
存储 消息中间件 人工智能
【04】AI辅助编程完整的安卓二次商业实战-寻找修改替换新UI首页图标-菜单图标-消息列表图标-优雅草伊凡
【04】AI辅助编程完整的安卓二次商业实战-寻找修改替换新UI首页图标-菜单图标-消息列表图标-优雅草伊凡
122 4