码迷,mamicode.com
首页 > 其他好文 > 详细

kotlin编《第一行代码》(1)

时间:2017-07-27 01:00:04      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:override   gradle   style   使用   err   数据保存   inf   start   ase   

一、android studio中使用kotlin扩展

  在build.gradle中添加apply plugin: ‘kotlin-android-extensions‘
  在kt中添加import kotlinx.android.synthetic.main.layout.*
  使用:
  textView.setText("使用kotlin扩展")

二、使用Toast
java:

  public void onClick(View v) {

      switch (v.getId()) {

      case R.id.button1:

        tst = Toast.makeText(this, "111111111", Toast.LENGTH_SHORT);

        tst.show();

        break;

      case R.id.button2:

        tst = Toast.makeText(this, "222222222", Toast.LENGTH_SHORT);

        tst.show();

        break;

      default:

        break;

      } 

  }

 kotlin:

  fun open(v: View?){

          Toast.makeText(this,"toast方法",Toast.LENGTH_SHORT).show();

     }

三、使用menu

  res新建menu文件夹,新建main菜单文件

  main.xml中添加(菜单项)

  <item

    android:id="@+id/add_item"

    android:title="Add"/>

   <item

    android:id="@+id/remove_item"

    android:title="Remove" />

ctrl+o重写onCreateOptionsMenu

Java:

  getMenuInflater().inflate(R.menu.main,menu);

  return true;

kotlin:

  override fun onCreateOptionsMenu(menu: Menu?): Boolean {

    menuInflater.inflate(R.menu.main,menu)

    return super.onCreateOptionsMenu(menu)

  }

重写onOptionsItemSelected

Java:

  switch(item.getItemId()){

  case R.id.add_item:

    Toast.makeText(this,"add",Toast.LENGTH_SHORT).show();

    Break;

  case R.id.remove_item:

    Toast.makeText(this,"remove",Toast.LENGTH_SHORT).show();

    Break;

  default:

  }

  Return true;

}

Kotlin:

  override fun onOptionsItemSelected(item: MenuItem?):Boolean {

    when(item!!.itemId){

    R.id.add_item ->Toast.makeText(this,"ffff",Toast.LENGTH_SHORT).show()

    R.id.remove_item ->Toast.makeText(this,"dddd",Toast.LENGTH_SHORT).show()

    }

    return true

  }

四、intent显式

Java:

  public void onClick(View v){
    Intent intent=new Intent(this,secondactivity.class);
    startActivity(intent);

  }

Kotlin:

  fun change(v:View?){

    val intent=Intent(this,secondactivity::class.java);

    startActivity(intent);

  }

五、向下传递数据

java:

第一活动

  public void onClick(View v){
    String data="Hello SecondActivity";

    Intent intent=new Intent(this,secondactivity.class);

    intent.putExtra("extra_data",data);

    startActivity(intent);

  }

第二活动

  public void onCreate(Bundle saveInstanceState){
    Intent intent=getIntent();

    String data=intent.getStringExtra("extra_data");

    log.d("SecondActivity",data);

  }

kotlin:

第一活动

  fun change(v:View?){
    val data="hello secondactivity";
    val intent=Intent(this,secondactivity::class.java);
    intent.putExtra("extra_data",data);
    startActivity(intent);
  }

第二活动

  public void onCreate(Bundle saveInstanceState){

    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_secondactivity)

    val data=intent.getStringExtra("extra_data");

    Toast.makeText(this,data,Toast.LENGTH_LONG).show();

  }

 六、intent回传

java:

第一活动

  public void onClick(View v){
    String data="Hello SecondActivity";

    Intent intent=new Intent(this,secondactivity.class);

    intent.putExtra("extra_data",data);

    startActivityForResult(intent,1);

  }

重写onActivityResult()方法

  switch(requestCode){

    case 1:

      if (resultCode==RESULT_OK){

        String returnedData=data.getStringExtra("data_return");

      }

      break;

    default:

  }

第二活动

  public void onClick(View v){
    Intent intent=new Intent();

    intent.putExtra("data_return","Hello FirstActivity");

    setResult(RESULT_OK,intent);

    finish();

  }

kotlin:

第一活动

  fun change(v:View?){
    val data="hello secondactivity";
    val intent=Intent(this,secondactivity::class.java);
    intent.putExtra("extra_data",data);
    startActivityForResult(intent,1);
  }

  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    when(requestCode){
    1 -> Toast.makeText(this,data!!.getStringExtra("data_return"),Toast.LENGTH_LONG).show();
    }
    super.onActivityResult(requestCode, resultCode, data)
  }

 

第二活动

  fun back(v: View?){
    val intent=intent.putExtra("data_return","Hello FirstActivity");
    setResult(Activity.RESULT_OK,intent);
    finish();
  }

 七、数据保存

java:

重写onSaveInstanceState()

  void onSaveInstanceState(Bundle outState) {
  super.onSaveInstanceState(outState)

  String tempData="Something you just typed";
  outState.putString("data_key",tempData);
}

改onCreate()

  if (savedInstanceState!=null){
    String tempData=savedInstanceState.getString("data_key");
  }

kotlin:

  override fun onSaveInstanceState(outState: Bundle?, outPersistentState: PersistableBundle?) {
    super.onSaveInstanceState(outState, outPersistentState)
    val tempData="Something you just typed";
    outState!!.putString("data_key",tempData);
  }

onCreate()

  if (savedInstanceState!=null){
  val tempData=savedInstanceState.getString("data_key");
  }

kotlin编《第一行代码》(1)

标签:override   gradle   style   使用   err   数据保存   inf   start   ase   

原文地址:http://www.cnblogs.com/hunter0541/p/7242578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!