A.工程的根目录配置build.gradle
buildscript {
**ext.kotlin_version = '1.3.50'**
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
**classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"**
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
B.moudle中配置build.gradle
apply plugin: 'com.android.application'
**apply plugin: 'kotlin-android'**
**apply plugin: 'kotlin-android-extensions'**
....
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
**implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"**
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
C.比较Activity
kotlin 代码
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.container, MainFragment.newInstance())
.commitNow()
}
}
}
java 代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, MainFragment.newInstance())
.commitNow();
}
}
}
D.比较Fragment
kotlin 代码
class MainFragment : Fragment() {
companion object {
fun newInstance() = MainFragment()
}
private lateinit var viewModel: MainViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.main_fragment, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
// TODO: Use the ViewModel
}
}
java 代码
public class MainFragment extends Fragment {
private MainViewModel mViewModel;
public static MainFragment newInstance() {
return new MainFragment();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_fragment, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
// TODO: Use the ViewModel
}
}
区别:
- 类的修饰符
- 继承方式
- 方法修饰符
- 方法参数(onCreate)
- 一行代码结束(;)
- 单例的定义(MainFragment.newInstance())
- 方法返回值(onCreateView方法)
网友评论