Android----对于kotlin的一些认识
2018-07-30 本文已影响30人
pgydbh
目录
无标题.png什么是kotlin?
google出的一门android开发语言,号称简洁,编程效率高,解放程序员。
要不要学习kotlin?
java虽然代码量比较大,但是java简单的写法深入人心,而且对于编程中很多麻烦的点,使用java处理让人更加熟悉,更有信心。
但是kotlin还是自己的优点,下面是最简单的对比,不用看代码只要大概看一下长度就好。你会发现就只是从解决了findViewById的份上我们都应该去看看kotlin。
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn_breast.setOnClickListener { startActivity(0) }
btn_stomach.setOnClickListener { startActivity(1) }
btn_back.setOnClickListener { startActivity(2) }
btn_leg.setOnClickListener { startActivity(3) }
btn_shoulder.setOnClickListener { startActivity(4) }
btn_spine.setOnClickListener { startActivity(5) }
txt_time.setText("今天是你进入里克斯岛监狱的第${(System.currentTimeMillis() - App.start) / (24 * 60 * 60 * 1000) + 1}" + "天")
btn_set.setOnClickListener { startActivity(Intent(this, SetActivity::class.java)) }
}
private fun startActivity(yi:Int){
var intent = Intent(this, ContentActivity::class.java);
intent.putExtra(App.STR_TO_YI, yi);
startActivity(intent)
}
}
public class MainActivity extends Activity {
private TextView txtTime = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_breast).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, ContentActivity.class);
intent.putExtra(App.STR_TO_YI, App.INT_YI_BREAST);
startActivity(intent);
}
});
findViewById(R.id.btn_stomach).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, ContentActivity.class);
intent.putExtra(App.STR_TO_YI, App.INT_YI_STAMACH);
startActivity(intent);
}
});
findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, ContentActivity.class);
intent.putExtra(App.STR_TO_YI, App.INT_YI_BACK);
startActivity(intent);
}
});
findViewById(R.id.btn_leg).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, ContentActivity.class);
intent.putExtra(App.STR_TO_YI, App.INT_YI_LEG);
startActivity(intent);
}
});
findViewById(R.id.btn_shoulder).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, ContentActivity.class);
intent.putExtra(App.STR_TO_YI, App.INT_YI_SHOULDER);
startActivity(intent);
}
});
findViewById(R.id.btn_spine).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, ContentActivity.class);
intent.putExtra(App.STR_TO_YI, App.INT_YI_SPINE);
startActivity(intent);
}
});
findViewById(R.id.btn_set).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SetActivity.class);
startActivity(intent);
}
});
txtTime = findViewById(R.id.txt_time);
txtTime.setText("今天是你进入里克斯岛监狱的第" +
((System.currentTimeMillis() - App.start) / (24 * 60 * 60 * 1000) + 1) +
"天");
}
}