工具下载、安装、配置
本部分参考网址:juejin.cn/post/709080…
下载
Android Studio开发工具下载
官网下载地址
安装
开始安装
点击下一步,勾选安卓虚拟机。
选择安装目录,点击 Next 下一步
最后点击 Install 进行安装,
配置
因为第一次安装,没有Android Studio的配置文件,所以只能选择“Do not import settings”,然后点击OK。
然后弹出Andriod Studio Sdk缺失提示,因为没有代理,所以选择取消,如下图。
进入Android Studio配置引导界面,点击下一步。
选择标准配置,然后点击下一步。
点击Next
点击“Android Emulator”,然后选择“Accept”。剩下的执行同样的操作。
然后,点击Finish。等待下载选择的SDK
出现上面的图片,则安装完成。点击Finish。
第一个安装应用
选择“New Project”。
选择“Empty Activity”,选择“Next”。
选择4.4的安卓版本,下面显示可以在99.4%的设备上运行。然后点击“Finish”。
等待,红框的下载完成后就可以。
安装模拟器,“点击Device Manager”,然后点击“Create device”
选择手机,点击Next
选择30版本,点击Download。
等待下载完成,点击“Finish”。
选择“Next”。
设置AVD Name,选择Portrait,然后选择“Finish”。
点击红框中的绿三角按钮,然后安心等待。
恭喜,Hello World 完成了。
DEMO案例
包含功能说明:
- 登录页面布局,采用‘RelativeLayout’相对布局;
- 点击登录按钮,toast提示出输入的账号密码;
- 点击登录按钮后,跳转到‘账户中心’;
- 将登录的账号密码作为参数传递到‘账户中心’页面,并toast提示出来;
效果图
项目目录结构
日志打印及查看
核心代码
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myapplication">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HomeActivity" android:label="账户中心"></activity>
</application>
</manifest>
MainActivity.kt
package com.example.myapplication
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
/**
* https://juejin.cn/post/7021682733718077454
*/
class MainActivity : AppCompatActivity(), View.OnClickListener {
private val loginNameText: TextView by lazy { findViewById<TextView>(R.id.login_name_tip) }
private val loginPwdText: TextView by lazy { findViewById<TextView>(R.id.login_pwd_tip) }
private val loginName: TextView by lazy { findViewById<TextView>(R.id.login_name) }
private val loginPwd: TextView by lazy { findViewById<TextView>(R.id.login_pwd) }
/* private val myImg: ImageView by lazy { findViewById<ImageView>(R.id.myImg) }
private val imageView: ImageView by lazy { findViewById<ImageView>(R.id.imageView) }*/
private val logInBtn: Button by lazy { findViewById<Button>(R.id.login_btn) }
private val serverSettingBtn: Button by lazy { findViewById<Button>(R.id.server_setting_btn) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
loginNameText.setOnClickListener(this)
loginPwdText.setOnClickListener(this)
logInBtn.setOnClickListener(this)
serverSettingBtn.setOnClickListener(this)
//设置背景图片
this.window.setBackgroundDrawableResource(R.drawable.login)
}
override fun onClick(p0: View?) {
when (p0?.id) {
R.id.login_btn -> {
Toast.makeText(this,"登录 账号:" + loginName.text.toString() + ",密码:" + loginPwd.text.toString(),Toast.LENGTH_SHORT).show()
val intent = Intent(this@MainActivity,HomeActivity::class.java)
intent.putExtra("login_name",loginName.text.toString())
intent.putExtra("login_pwd",loginPwd.text.toString())
startActivity(intent)
}
R.id.server_setting_btn -> {
Toast.makeText(this, "点击了服务设置按钮" + loginNameText.text, Toast.LENGTH_SHORT).show()
}
}
}
}
HomeActivity.kt
package com.example.myapplication
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
/**
* 登录后
*/
class HomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
val loginName = this.intent.getStringExtra("login_name")
val loginPwd = this.intent.getStringExtra("login_pwd")
Log.i("获取参数值",loginName.toString()+" 密码:"+loginPwd.toString())
Toast.makeText(this, "登录成功!!!账号:$loginName 密码:$loginPwd", Toast.LENGTH_SHORT).show()
}
}
activity_main.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"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/RelativeLayout1"
tools:ignore="MissingConstraints">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/div1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/div2">
<TextView
android:id="@+id/login_name_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="@string/login_name"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="49dp"
tools:layout_editor_absoluteY="349dp" />
<EditText
android:id="@+id/login_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/login_name_tip"
android:ems="10"
android:inputType="textPersonName"
android:text=""
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="140dp"
tools:layout_editor_absoluteY="334dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/div2"
android:id="@+id/div3">
<TextView
android:id="@+id/login_pwd_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login_pwd"
android:layout_centerVertical="true"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="49dp"
tools:layout_editor_absoluteY="349dp" />
<EditText
android:id="@+id/login_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/login_pwd_tip"
android:ems="10"
android:inputType="textPassword"
android:text=""
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="140dp"
tools:layout_editor_absoluteY="334dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/div3"
android:layout_marginTop="25px"
android:id="@+id/div4">
<Button
android:id="@+id/login_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20px"
android:text="@string/login_btn"
android:onClick="mButton3"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="140dp"
tools:layout_editor_absoluteY="475dp"
/>
<Button
android:id="@+id/server_setting_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="200px"
android:layout_toRightOf="@id/login_btn"
android:text="@string/server_setting"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="140dp"
tools:layout_editor_absoluteY="475dp" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_home.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"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/RelativeLayout1"
tools:ignore="MissingConstraints">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/div1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/div2">
<TextView
android:id="@+id/login_name_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="账号中心"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="49dp"
tools:layout_editor_absoluteY="349dp" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
strings.xml
<resources>
<string name="app_name">My Application</string>
<string name="login_name">登录账号:</string>
<string name="login_pwd">登录密码:</string>
<string name="login_btn">登录</string>
<string name="server_setting">服务设置</string>
</resources>