Android kotlin基本语法

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(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

方法二:直接导入xml文件 即可直接根据控件ID调用控件对象

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)
}

 

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工程demo链接:https://download.csdn.net/download/meixi_android/11212091

你可能感兴趣的:(移动开发)