物品介紹:
攻擊者每次攻擊都將根據(jù)造成的傷害回復15%的生命值。
吸血:15%
Github地址
Start
First Blood
-
給Activity添加注解:
@Params( Val("intParam", Int::class), Val("booleanParam", Boolean::class), Val("stringParam", String::class) ) open class TestActivity : AppCompatActivity()
通過Params注解, 告訴該Activity需要的參數(shù)類型及參數(shù)名稱.
例如TestActivity的參數(shù)分別為:
- intParam :Int類型
- booleanParam :Boolean類型
- stringParam :String參數(shù)
-
傳遞參數(shù)給Activity:
Params注解編譯之后,將會生成該Activity對應的Director文件,命名規(guī)則為 Activity名稱+Director后綴,
例如上面的TestActivity將會生成TestActivityDirector文件.btn_activity.setOnClickListener { //通過Director安全快速傳遞參數(shù)給TestActivity TestActivityDirector.of(this) .intParam(1123123123) .booleanParam(true) .stringParam("This is string param") .direct() }
-
獲取傳遞給Activity的參數(shù):
Params注解編譯之后, 除了會生成Director文件, 還會生成對應的Params文件, 命名規(guī)則為 Activity名稱+ Params后綴,
例如上面的TestActivity將會生成TestActivityParams文件.class TestActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_test) //獲取params參數(shù) val params = TestActivityParams.of(this) Log.d("TAG", params.intParam.toString()) Log.d("TAG", params.booleanParam.toString()) Log.d("TAG", params.stringParam) } }
Double Kill
接下來嘗試給Fragment傳遞參數(shù).
-
添加Params注解:
@Params( Val("intParam", Int::class), Val("booleanParam", Boolean::class), Val("stringParam", String::class) ) class TestFragment : Fragment()
-
傳遞參數(shù)給Fragment:
同樣的,Params注解編譯之后,將會生成該Fragment對應的Director文件,命名規(guī)則為 Fragment名稱+Director后綴,
例如上面的TestFragment將會生成TestFragmentDirector文件.btn_fragment.setOnClickListener { TestFragmentDirector.of() .intParam(1123123123) .booleanParam(true) .stringParam("This is string param") .direct { val fragmentTransaction = supportFragmentManager.beginTransaction() fragmentTransaction.add(R.id.fragment_container, it) fragmentTransaction.commit() } }
-
獲取傳遞給Fragment的參數(shù):
Params注解編譯之后, 除了會生成Director文件, 還會生成對應的Params文件, 命名規(guī)則為 Fragment名稱+ Params后綴,
例如上面的TestFragment將會生成TestFragmentParams文件.class TestFragment : Fragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val params = TestFragmentParams.of(this) Log.d("TAG", params.intParam.toString()) Log.d("TAG", params.booleanParam.toString()) Log.d("TAG", params.stringParam) } }
Triple Kill
傳遞自定義數(shù)據(jù)類型
除了上述基本數(shù)據(jù)類型外,還支持自定義的數(shù)據(jù)類型.
-
添加自定義類型參數(shù):
//自定義數(shù)據(jù)類型 class CustomEntity( val id: Int, val content: String ) //使用自定義數(shù)據(jù)類型 @Params( Val("customParam", CustomEntity::class) ) class TestActivity : AppCompatActivity()
-
傳遞自定義類型參數(shù):
btn_activity.setOnClickListener { TestActivityDirector.of(this) .customParam(CustomEntity(123, "Custom entity content")) .direct() }
自定義數(shù)據(jù)類型默認使用Gson進行序列化和反序列化, 因此請在release環(huán)境中Keep自定義的數(shù)據(jù)類型, 以免發(fā)生問題!
Ultra Kill
利用MutableParams注解生成 var 類型參數(shù).
如上所見, 通過Params注解只能定義val不可變類型的參數(shù), 要想定義var可變類型的參數(shù), 可以通過
MutableParams及Var聲明可變類型的參數(shù).
例如:
@Params(
Val("charParam", Char::class),
Val("booleanParam", Boolean::class),
Val("stringParam", String::class)
)
@MutableParams(
Var("test", String::class),
Var("test1", Boolean::class)
)
class TestActivity : AppCompatActivity()
License
Copyright 2019 Season.Zlc Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.