作者:占志杰
-
最近在学习Android开发,和学习其他任何语言一样,不管理论上学得多扎实,最后都一定要付诸实践,不然很容易忘掉,而且也仅仅停留在皮毛上,记录下自己练手的APP的详细实践过程,方便以后忘了回过头来复习。
-
这个APP的内容主要包括:APP图标的设置,名称设置,用户界面简单实现,宠物属性值的进度条展示,宠物装备的购买等等。
-
美中不足的是还没学到数据存储,每次进去都是初始化的数据,只能以后学到了,有时间更新一下。
学习内容:
-
APP图标,名称的设置;
-
intent 在不同Activity之间的跳转与传值
(startActivityForResult()与onActivityResult()与setResult()参数分析,activity带参数的返回); -
简单的线性布局;
-
进度条ProgressBar的使用;
目录表格
目录 |
---|
1.图标设置 |
2.设置APP名称 |
3.用户登录 |
4.信息显示 |
5.装备购买 |
6.颜色素材 |
扩展: startActivityForResult |
扩展: onActivityResult |
扩展: setResult |
所需环境:Android 8.0 开发工具:Android studio 3.0以及以上版本
1.图标设置
- 用Android Studio 3.0来新建一个项目,命名为 MyTest。 创建好项目之后,打开app/build.gradle文件检查一下,确保targetSdkVersion已经指定到了26或者更高
- 如下所示:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.MyTest"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
- 我在创建新项目的时候默认targetSdkVersion就是28,如果你是低于26的话,手动把它改成26就可以了。
- 接下来打开AndroidManifest.xml文件,代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myandroidtest1">
<application
android:allowBackup="true"
android:icon="@drawable/head"
android:label="@string/app_name"
android:roundIcon="@drawable/background2"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".For_Study">
</activity>
<activity android:name=".Shop_Device">
</activity>
<activity android:name=".Show_Message">
</activity>
<activity android:name=".User_Regist">
</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>
- 我们重点看android:icon这个属性,通过这个属性,我们将应用的图标指定为了mipmap目录下的ic_launcher文件。
- 另外android:roundIcon属性,这是一个只适用在Android 7.1系统上的过渡版本,我们不用去管它。
- 我们需要关注的是mipmap-anydpi-v26这个目录,因为Android 8.0或以上系统的手机,都会使用这个目录下的ic_launcher来作为图标。
- mipmap-anydpi-v26目录下的ic_launcher是一个XML文件,打开这个文件,
- 代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>
- 在 adaptive-icon 标签中
- 定义一个标签用于指定图标的背景层,我们可以指定使用普通的PNG、JPG等格式的图片,或者直接指定一个背景色。
- 定义一个标签用于指定图标的前景层,作为APP的Logo,可以使用PNG、JPG等格式的图片
- 接下来开始更改图标:选中我们需要更改的工程,然后new -->Image Asset
- 如下图:
- 进入页面可以看到
- Foreground Layer 是选择设置前景
- Background Layer 是选择设置背景
- 很多情况下,我们都是使用自己的图标,而非系统自带的,我们只需要选中Image单选框即可选择自己的图标,背景可以自己指定颜色或者图片。
注意:Name输入框内的名称不要更改,否则会更改失败,选择好后,点击Next
- 图标的名字红色警告,大家不用在意,直接点击Finish就行了
- 打开mipmap-anydpi-v26目录下的ic_launcher的图标展示已经是更改后的图片
- 至此,我们已经完成了APP的图标修改
-效果如图:
2.设置APP名称
-
到res文件夹下面的values文件夹下修改String.xml文件
-
修改成自己想要的名字就行了
3.用户登录
-
新建一个布局Layout 命名为: activity_user_login.xml
-
为登录按钮 设置监听事件,一旦点击就调用passData()
-
利用 intent 来进行 进行Activity之间的跳转与数据传值的
-
扩展:
-
startActivityForResult(Intent intent, int requestCode);
第一个参数:一个Intent对象,用于携带将跳转至下一个界面中使用的数据,使用putExtra(A,B)方法,此处存储的数据类型特别多,基本类型全部支持。
第二个参数:如果> = 0,当Activity结束时requestCode将归还在onActivityResult()中。以便确定返回的数据是从哪个Activity中返回,用来标识目标activity。
与下面的resultCode功能一致,感觉Android就是为了保证数据的严格一致性特地设置了两把锁,来保证数据的发送,目的地的严格一致性。
-
代码如下:
<?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=".User_Login"
android:background="@drawable/backgroung">
<ImageView
android:layout_width="120dp"
android:layout_height="150dp"
android:id="@+id/iv_head"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:layout_marginBottom="50dp"
android:src="@drawable/head"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/layout"
android:layout_below="@+id/iv_head"
android:layout_margin="10dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/register_username"
android:layout_margin="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_name"
android:layout_centerVertical="true"
android:text="用户名:"
android:textSize="20sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_name"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/tv_name"
android:hint="请输入用户名"
android:textSize="16sp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/register_password"
android:layout_margin="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_psw"
android:layout_centerVertical="true"
android:text="密 码:"
android:textSize="20sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_password"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/tv_psw"
android:hint="请输入密码"
android:inputType="textPassword"
android:textSize="16sp"/>
</RelativeLayout>
</LinearLayout>
<Button
android:layout_width="160dp"
android:layout_height="48dp"
android:id="@+id/btn_send"
android:layout_below="@id/layout"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="登 录"
android:background="@color/darkorchid"
android:textColor="@color/gold"
android:textSize="20sp"
android:textStyle="bold"
/>
</RelativeLayout>
-
对应着,新建一个Activity,命名为: User_Login
-
监听登录按钮
-
获取用户名,密码的数据,并用 intent 跳转Activity和传值
-
扩展: startActivityForResult
-
startActivityForResult(Intent intent, int requestCode);
第一个参数:一个Intent对象,用于携带将跳转至下一个界面中使用的数据,使用putExtra(A,B)方法,此处存储的数据类型特别多,基本类型全部支持。第二个参数:如果 requestCode > = 0,当Activity结束时requestCode将归还在onActivityResult()中。以便确定返回的数据是从哪个Activity中返回,用来标识目标activity。
-
代码如下:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class User_Login extends AppCompatActivity {
private EditText et_password;
private Button btn_send;
private EditText et_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
et_name = (EditText)findViewById(R.id.et_name);
et_password = (EditText)findViewById(R.id.et_password);
btn_send = (Button)findViewById(R.id.btn_send);
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
passData();
}
});
}
public void passData(){
Intent intent=new Intent(this,Show_Message.class);
intent.putExtra("name",et_name.getText().toString().trim());
intent.putExtra("password",et_password.getText().toString().trim());
startActivity(intent);
}
}
效果如图:
4.信息显示
- 新建一个布局Layout 命名为: activity_show_message.xml
- 内容有:用户登录信息展示,进度条展示各种属性,还有跳转商店页面的按钮
- 代码如下:
<?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/backgroung">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:padding="15dp">
<ImageView
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="1"
android:id="@+id/pet_imgv"
android:background="@drawable/pet"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="20dp">
<TextView
android:layout_width="wrap_content"