一、介绍安卓界面activity
1、创建第一个安卓程序
安装安卓开发环境
已安装完毕AndroidStudio,安装指导可以参考: 《安卓开发入门之开发环境搭建》
创建第一个安卓工程
1. 新建工程
2. 最后一步,点击“Finish”,工程效果图如下。
3. 选择设备,点击绿色箭头运行。
4. App运行效果图:
附录:创建安卓模拟器
-
点击如下图标
-
选择硬件类型
-
选择系统镜像
如果对应的镜像还未下载,旁边会出现Download按钮,需要先点击下载镜像.
-
配置设备相关信息
-
完成后,界面如下:
-
关闭上面的界面,回到运行程序的主界面,就可以选择到新增的模拟器了
2.安卓基本界面:activity
-
app运行效果图
- activity代码
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
- 界面布局xml文件
<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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="程序园中猿"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
- manifest文件
<?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.cxyzy.demo">
<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/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<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>
3.安卓界面跳转
3.1不带数据跳转
-
app运行效果图
- 代码
- 第一个界面Activity
class FirstActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_first)
textView.setOnClickListener {
startActivity(Intent(this, SecondActivity::class.java))
}
}
}
- 第一个界面布局xml文件
<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=".FirstActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击查看第二页"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3.第二个界面Activity
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
}
}
- 第二个界面布局xml文件
<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=".FirstActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="程序园中猿_第二页."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3.2带数据跳转
-
app运行效果图
- 代码
- 第一个界面Activity
class FirstActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_first)
textView.setOnClickListener {
val intent = Intent(this, SecondActivity::class.java)
//传递字符串类型
intent.putExtra("name", "程序园中猿")
//传递整型
intent.putExtra("age", 99)
//传递布尔
intent.putExtra("isOK", true)
startActivity(intent)
}
}
}
- 第一个界面布局xml文件
<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=".FirstActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击查看第二页"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3.第二个界面Activity
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
//获取字符串值
val name = intent.getStringExtra("name")
//获取整型值
val age = intent.getIntExtra("age", 0)
//获取布尔值
val isOk = intent.getBooleanExtra("isOk", true)
showContent(name, age, isOk)
}
private fun showContent(name: String?, age: Int, isOk: Boolean) {
descTextView.text = "名字:$name;年龄:$age;isOk:$isOk"
}
}
- 第二个界面布局xml文件
<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=".FirstActivity">
<TextView
android:id="@+id/descTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="程序园中猿_第二页."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
二、常用控件
文本显示控件:TextView
- 简介
显示文本 -
效果图
- 使用方法
- xml布局文件代码
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="程序园中猿" />
- activity代码
//设置文本
textView.text = "程序园中猿"
//设置文本颜色
textView.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary))
文本输入控件:EditText
普通文本输入
- 简介
用于输入普通文本 -
效果图
- 使用方法
- xml布局文件代码
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Click me to confess" />
- activity代码
//设置输入框内容
editText.setText("程序园中猿")
密码输入
- 简介
用于输入密码 -
效果图
- 使用方法
- xml布局文件代码
<EditText
android:id="@+id/pwdEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
- activity代码
//设置密码输入框内容
pwdEditText.setText("123456")
按钮控件
普通按钮:Button
- 简介
显示按钮 -
效果图
- 使用方法
- xml布局文件代码
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Touch me" />
- activity代码
//设置按钮文字
button.text = "Touch me"
//设置点击事件
button.setOnClickListener {
Toast.makeText(this, "Feels good", Toast.LENGTH_SHORT).show()
}
图片按钮:ImageButton
- 简介
显示带有图片的按钮 -
效果图
- 使用方法
- xml布局文件代码
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/ic_input_add" />
- activity代码
//设置按钮图片
imageButton.setImageResource(R.mipmap.ic_launcher)
//设置点击事件
imageButton.setOnClickListener {
Toast.makeText(this, "Go on", Toast.LENGTH_SHORT).show()
}
单选框:RadioButton
- 简介
实现单选功能 -
效果图
- 使用方法
- xml布局文件代码
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单选框" />
- activity代码
单选框组:RadioGroup
简介
-
效果图
使用方法
- xml布局文件代码
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="单选框1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单选框2" />
</RadioGroup>
- activity代码
//获取选中的单选框
val selectedRadioButton = findViewById<RadioButton>(radioGroup.checkedRadioButtonId)
selectedRadioButton?.let {
//获取选中单选框的文字
val text = selectedRadioButton.text
}
复选框:CheckBox
- 简介
实现多选功能 -
效果图
- 使用方法
- xml布局文件代码
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="复选框" />
- activity代码
//设置复选框显示文字
checkBox.text = "复选框"
//获取是否被选中
val isChecked = checkBox.isChecked
开关:Switch
- 简介
显示开关 -
效果图
- 使用方法
- xml布局文件代码
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开关" />
- activity代码
//设置开关显示文字
switch1.text="开关"
//获取是否被选中
val isChecked = switch1.isChecked
状态开关按钮:ToggleButton
- 简介
具有按下和恢复状态的按钮 -
效果图
- 使用方法
- xml布局文件代码
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:textOff="已关"
android:textOn="已开" />
- activity代码
//获取是否被选中
val isChecked = toggleButton.isChecked
图片显示控件:ImageView
- 简介
显示图片 -
效果图
- 使用方法
- xml布局文件代码
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher" />
- activity代码
//修改图片
imageView.setImageResource(R.mipmap.ic_launcher)
进度条
进度条:ProgressBar
- 简介
执行耗时操作时,给用户等待提示. -
效果图
- 使用方法
- xml布局文件代码
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
- activity代码
//隐藏进度条
progressBar.visibility = View.GONE
//显示进度条
progressBar.visibility = View.VISIBLE
横向进度条:ProgressBar
- 简介
用于显示进度
显示确定耗时的进度条
-
效果图
- 使用方法
- xml布局文件代码
<ProgressBar
android:id="@+id/horizontalProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="30" />
- activity代码
//设置进度
horizontalProgressBar.progress = 50
显示不确定耗时的进度条
-
效果图
- 使用方法
- xml布局文件代码
<ProgressBar
android:id="@+id/horizontalProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true" />
- activity代码
//隐藏进度条
horizontalProgressBar.visibility = View.GONE
//显示进度条
horizontalProgressBar.visibility = View.VISIBLE
可拖动进度条:SeekBar
- 简介
通过拖动可以控制进度,如用于控制播放进度、音量大小等
连续可拖动进度条
-
效果图
- 使用方法
- xml布局文件代码
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="50" />
- activity代码
//设置进度
seekBar.progress = 60
分段可拖动进度条
-
效果图
- 使用方法
- xml布局文件代码
<SeekBar
android:id="@+id/discreteSeekBar"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:progress="3" />
- activity代码
//设置进度
discreteSeekBar.progress = 5
评分控件:RatingBar
- 简介
用于评分 -
效果图
- 使用方法
- xml布局文件代码
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="2" />
- activity代码
//设置评分
ratingBar.rating = 3.0f
搜索框
- 简介
用于搜索 -
效果图
- 使用方法
- xml布局文件代码
<SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
- activity代码
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
// 当点击搜索按钮时触发该方法
override fun onQueryTextSubmit(query: String?): Boolean {
Toast.makeText(mContext, query, Toast.LENGTH_SHORT).show()
return false
}
// 当搜索内容改变时触发该方法
override fun onQueryTextChange(newText: String?): Boolean {
Toast.makeText(mContext, newText, Toast.LENGTH_SHORT).show()
return false
}
})
日历和时间控件
日历控件:CalendarView
- 简介
用于选择日期 -
效果图
- 使用方法
- xml布局文件代码
<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
- activity代码
//设置初始日期
val date = SimpleDateFormat("yyyy/MM/dd").parse("2020/02/02")
date?.let { calendarView.date = it.time }
//获取用户选择的日期
calendarView.setOnDateChangeListener { calendarView, year, month, dayOfMonth ->
Toast.makeText(mContext, "日期:${year}年${month + 1}月${dayOfMonth}日", Toast.LENGTH_SHORT)
.show()
}
日历控件:DatePicker
- 简介
用于选择日期, -
效果图
- 使用方法
- xml布局文件代码
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
- activity代码
//设置初始日期,设置监听获取用户选择的日期
datePicker.init(2020, 2, 2) { datePicker, year, month, dayOfMonth ->
Toast.makeText(mContext, "日期:${year}年${month + 1}月${dayOfMonth}日", Toast.LENGTH_SHORT)
.show()
}
时间控件
简介
用于选择时间效果图
使用方法
- xml布局文件代码
- activity代码
列表
待续
列表
网络访问
三、Fragment
四、广播
五、数据存储
SharedPreferences
SQLite数据库
文件
简易读写样例
- Manifest文件添加权限声明
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- 添加申请权限开源包依赖
implementation 'com.afollestad:assent:2.3.1'
- 申请权限及读写文件代码
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
writeFileBtn.setOnClickListener {
checkPermissionAndWriteFile()
}
readFileBtn.setOnClickListener {
checkPermissionAndReadFile()
}
}
/**
* 检查权限并写文件
*/
private fun checkPermissionAndWriteFile() {
runWithPermissions(Permission.WRITE_EXTERNAL_STORAGE) {
writeFile("test data")
}
}
/**
* 写文件
*/
private fun writeFile(data: String) {
val testFile = getTestFile()
getTestFile().writeText(data)
Toast.makeText(this, "filePath:${testFile.absolutePath}", Toast.LENGTH_SHORT).show()
}
/**
* 检查权限并写文件
*/
private fun checkPermissionAndReadFile() {
runWithPermissions(Permission.WRITE_EXTERNAL_STORAGE) {
readFile()
}
}
/**
* 读文件
*/
private fun readFile() {
val testFile = getTestFile()
val content = testFile.readText()
Toast.makeText(this, content, Toast.LENGTH_SHORT).show()
}
private fun getTestFile(): File {
val dir = File(getExternalFilesDir(null)?.absolutePath, "tmp")
dir.mkdirs()
return File(dir.absolutePath + "/test.txt")
}
}