继承类是冒号连接,java是extends连接。实现接口是逗号连接,java是implements连接
class MainActivity : AppCompatActivity(),MyKotlinInterface ,MyInterface{
java:public class Main3Activity extends AppCompatActivity implements MyKotlinInterface {
var代表变量,可多次赋值。val代表常量相当于java的final,只能赋值一次
const val相当于java的public final static,可以直接访问。
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
创建:
方法一:类型java的语法
val te3 = findViewById(R.id.onete) as TextView
val bt = findViewById(R.id.onebutton) as Button;
val bt2 = findViewById(R.id.twobutton) as Button;
var image = findViewById(R.id.imagelg) as ImageView
import kotlinx.android.synthetic.main.activity_main.*
注:activity_main是layout文件名。activity与fragment同样适用
如:text控件
直接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(R.id.imagelg) as ImageView
image.setImageBitmap(bitmap)
}
for (a in 0..19) {
barEntityList.add(internentBarEntity)
}
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()
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工程demo链接:https://download.csdn.net/download/meixi_android/11212091