当前位置:网站首页>Kotlin learning 3: common writing methods of pages
Kotlin learning 3: common writing methods of pages
2022-07-22 20:06:00 【Wu Qingsen】
Here is the writing method
Control initialization and click events
val text: TextView = findViewById(R.id.text);
text.setOnClickListener {
Toast.makeText(this, " Click on ", Toast.LENGTH_LONG).show()
}
Intent Use and value transfer
The first class passes values :
val text: TextView = findViewById(R.id.text);
text.setOnClickListener {
val myIntent = Intent(this,TwoActivity::class.java)
myIntent.putExtra("key"," Hello ")
startActivity(myIntent)
}
The value of the second class :
val value = intent.getStringExtra("key")
Log.w(" Take the value ", "" + value)
Give the last one activity Return the data
be used startActivityForResult, first activity Add jump and receive data :
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val text: TextView = findViewById(R.id.text);
text.setOnClickListener {
val myIntent = Intent(this, TwoActivity::class.java)
startActivityForResult(myIntent, 100)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Log.w(" return ", "requestCode:" + requestCode + ";resultCode" + resultCode)
if (requestCode == 100) {
if (resultCode == Activity.RESULT_OK) {
val returnData = data?.getStringExtra("key1")
Log.w(" Return to get value ", "" + returnData)
}
}
}
}
the second activity Return the data :
class TwoActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textTwo: TextView = findViewById(R.id.text);
textTwo.setOnClickListener {
val intent = Intent()
intent.putExtra("key1", " Return value ")
setResult(Activity.RESULT_OK, intent)
finish()
}
}
}
companion object Use
companion object amount to Java Static method of :
class MyUtils {
companion object {
fun setName(str: String) {
Log.w("setName call ", "" + str)
}
}
}
call :
MyUtils.setName("111")
边栏推荐
猜你喜欢
plsql不能初始化
shell script “<< EOF”我的用途和遇到的问题
Kotlin学习一:变量、函数、条件语句与循环语句
Spark GraphX 中的 pregel函数(转载)
Spark:图(Graph)
pyinstaller打包scrapy
ping: www.baidu.com: 未知的名称或服务原因分析
开幕在即 | “万物互联,使能千行百业”2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛
Pyinstaller packaging scene
When the appsstore transporter uploads, it is stuck in the process of verifying the problem
随机推荐
Spark GraphX 中的 pregel函数(转载)
Flutter开发(三十):Flutter简单app搭建
Spark RDD的依赖于DAG的工作原理
leetcode 300. 最长递增子序列
As a beginner, I don't want to use eslint
NFT挖矿分红系统开发模式定制
ES6 usage
Flutter development (30): simple app building of flutter
定向爬取淘宝商品名称和价格(嵩天老师)
Dynamic memory management and flexible array
Spark:图(Graph)
Xcode11 添加lanuchimage黑屏无法显示问题
pyinstaller打包scrapy
Fluent 2 Advanced (IV): Based on navigator 2.0 packaging
LogBack & MDC & a simple use
Kotlin学习二:类与对象
Flutter 2进阶(九):FijkPlayer播放视频与卡片效果
if else优化
Data warehouse model design and tools
appstore 上传屏幕快照尺寸