最近有一点空余的时间,写了一个微信登录Fragment界面,目前只有界面,功能要慢慢往里加
1. 首先写一个MainActivity,在里面有登录按钮
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
login.setOnClickListener {
WechatLoginFragment.show(supportFragmentManager)
}
}
}
2. 写一个fragment,承载登录界面的功能
class WechatLoginFragment : DialogFragment() {
companion object {
fun show(fm: FragmentManager) {
var loginFragment = fm.findFragmentByTag(WechatLoginFragment::class.java.simpleName)
if(loginFragment !is WechatLoginFragment) {
loginFragment = WechatLoginFragment()
}
loginFragment.show(fm, WechatLoginFragment::class.java.simpleName)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_wechat_login, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
iv_close.setOnClickListener { close() }
tv_login_by_wechat.setOnClickListener {
}
}
fun close() {
dismiss()
}
override fun onStart() {
super.onStart()
if (dialog != null) {
dialog.window.setGravity(Gravity.BOTTOM)
val height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 296f, context?.resources?.displayMetrics).toInt()
dialog.window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, height)
}
}
override fun getTheme(): Int {
return R.style.TagDialog
}
}
在github上的地址是:https://github.com/mlsnatalie/WechatLoginFragmentDemo.git