集合转数组
var list_alert: ArrayList<String>? = null
list_alert = ArrayList()
list_alert!!.add(LangComm.getString("advance_no"))
lateinit var alert: Array<String?>
alert = arrayOfNulls<String>(list_alert!!.size) list_alert!!.toArray(alert)
1、继承类和实现接口
继承类是冒号连接,java是extends连接。实现接口是逗号连接,java是implements连接
class MainActivity : AppCompatActivity(),MyKotlinInterface ,MyInterface{
java:public class Main3Activity extends AppCompatActivity implements MyKotlinInterface {
2、变量修饰符var和val,const val
var代表变量,可多次赋值。val代表常量相当于java的final,只能赋值一次
const val相当于java的public final static,可以直接访问。
3、常用数据类型创建
private var ab: Int = 0 //java是int
private var `object`: Any? = null//java是Object
private var bitmap: Bitmap? = null
var buff: ByteArray? = null //java是byte
private var string: String? = null
4、创建基本控件对象,控件赋值,添加事件,activity跳转
创建:
方法一:类型java的语法
val te3 = findViewById<View>(R.id.onete) as TextView
val bt = findViewById<View>(R.id.onebutton) as Button;
val bt2 = findViewById<View>(R.id.twobutton) as Button;
var image = findViewById<View>(R.id.imagelg) as ImageView
方法二:直接导入xml文件 即可直接根据控件ID调用控件对象
import kotlinx.android.synthetic.main.activity_main.*
注:activity_main是layout文件名。activity与fragment同样适用
如:text控件
<TextView
android:id="@+id/mText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
直接ID调用
mText.text = "ss22"
mText.setOnClickListener {
Log.i("lgq","fff");
}
赋值:
te3.text = "aabb"
te3.setTextColor(resources.getColor(R.color.colorPrimary))
bt.setOnClickListener {
startActivity(Intent(this@MainActivity, Main2Activity::class.java))
}
public fun setImage(bitmap: Bitmap){ //java的void这里是fun
var image = findViewById<View>(R.id.imagelg) as ImageView
image.setImageBitmap(bitmap)
}
5、kotlin循环语句
for (a in 0..19) {
barEntityList.add(internentBarEntity)
}
6、onActivityResult,requestCode请求码,resultCode返回码
java是:
put
Intent intent = new Intent(MainActivity.this,TwoActivity.class);
startActivityForResult(intent,66);//66是请求码
get
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String result = data.getExtras().getString("result");//得到新Activity 关闭后返回的数据
}
kotlin是:
put
startActivityForResult(Intent(this@MainActivity, Main2Activity::class.java),54)
get
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (resultCode) {
3 -> {
val result = data!!.getExtras().getString("result")//得到新Activity 关闭后返回的数据
}
}
}
java返回数据:
Intent intent = new Intent();
//把返回数据存入Intent
intent.putExtra("result", "My name is meixi");
//设置返回数据
setResult(33, intent);
//关闭Activity
finish();
kotlin返回数据:
val intent = Intent()
//把返回数据存入Intent
intent.putExtra("result", "My name is meixi")
//设置返回数据
setResult(3, intent)
//关闭Activity
finish()
注:如果是在fragment调用startActivityForResult跳转。则请求码返回时是随机数,而不是原请求码
7、activity嵌套fragment.kt
1、创建fragment.kt
class BlankFragment : Fragment() {
private var mTitle: String? = null
companion object {
fun getInstance(title: String): BlankFragment {
val fragment = BlankFragment()
val bundle = Bundle()
fragment.arguments = bundle
fragment.mTitle = title
return fragment
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_blank, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
LgqLogutil.e("11")
mText.text = "ss22"
mText.setOnClickListener {
Log.i("lgq", "fff");
}
}
}
2、activity加载fragment
private var mBlankFragment: BlankFragment? = null
val transaction = supportFragmentManager.beginTransaction()
BlankFragment.getInstance("title").let {
mBlankFragment = it
transaction.add(R.id.frame,it,"tag")}
transaction.commitAllowingStateLoss()
Kotlin中@JvmOverloads的使用方法
Overload翻译为重载,JvmOverloads即重载方法
在Kotlin中@JvmOverloads注解的作用就是:生成多个重载方法,这样方便我们调用,可以省掉一些不关心的参数可以修饰方法或者类
代码解析:
普通kotlin语法
class Mytext : TextView {
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
)
}
等同于如下@JvmOverloads语法
class Mytext @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = com.google.android.material.R.attr.materialCardViewStyle
) : TextView(context, attrs, defStyleAttr)
实例2
JvmOverloads修饰方法
class Overloads {
@JvmOverloads
fun overloaded(a: Int, b: Int = 0, c: Int = 1){
println("$a, $b, $c")
}
}
调用JvmOverloads修饰的方法(如果 Int没赋默认值,调用时,必须传入实参)
Overloads overloads = new Overloads();
overloads.overloaded(1, 2, 3);
overloads.overloaded(1);
overloads.overloaded(1,3);
运行结果
2019-11-06 13:47:29.379 15658-15658/com.tianxin.mykotlin I/System.out: 1, 2, 3
2019-11-06 13:47:29.379 15658-15658/com.tianxin.mykotlin I/System.out: 1, 0, 1
2019-11-06 13:47:29.379 15658-15658/com.tianxin.mykotlin I/System.out: 1, 3, 1
kotlin工程demo链接:https://download.csdn.net/download/meixi_android/11212091