一、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");
}