无法在片段中的按钮上使用setOnClickListener

时间:2018-08-25 20:26:40

标签: android android-fragments kotlin

我试图在片段中添加setOnClickListener。但是我尝试的两种方法都没有用。在应用程序中我的按钮按下没有任何作用。

class LoginFragment: Fragment(), View.OnClickListener {

    //AuthVariable for global use
    private lateinit var myAuth: FirebaseAuth

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        var view = inflater.inflate(R.layout.content_login, container, false)

        view?.login_button?.setOnClickListener(this)
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

    }
    override fun onClick(p0: View?) {
        Log.i("BUTTON123","TEST")
        loginUser()

    }
}

活动:

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

    //AuthVariable for global use
    private lateinit var myAuth : FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)


        val toggle = ActionBarDrawerToggle(
                this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
        drawer_layout.addDrawerListener(toggle)
        toggle.syncState()

        nav_view.setNavigationItemSelectedListener(this)

        //Initializing FirebaseAuth Instance
        myAuth = FirebaseAuth.getInstance()
        var user = myAuth.currentUser
        if(user != null){
            updateUI(user)
        }else{
            val test = supportFragmentManager
            val frag  = LoginFragment()
            test.beginTransaction().replace(R.id.placeholder,frag).addToBackStack(null).commit()
        }
    }

    override fun onBackPressed() {
        if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
            drawer_layout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        when (item.itemId) {
            R.id.action_settings -> return true
            else -> return super.onOptionsItemSelected(item)
        }
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        // Handle navigation view item clicks here.

        when (item.itemId) {
            R.id.nav_addShop -> {
                //Activity to Add your Shop
                val addshopIntent = Intent(this,AddShopActivity::class.java)
                startActivity(addshopIntent)

            }
            R.id.nav_searchShop -> {
                //SearchFragment
                val test = supportFragmentManager
                val frag  = test()
                test.beginTransaction().replace(R.id.placeholder,frag).addToBackStack(null).commit()

            }
            R.id.nav_shopMap -> {
                //MapActivity
                val mapintent = Intent(this, ShopMap::class.java)
                startActivity(mapintent)
            }
            R.id.nav_manage -> {

            }
            R.id.nav_share -> {

            }
            R.id.nav_logout -> {

                myAuth.signOut()
                updateUI(myAuth.currentUser)
            }
        }

        drawer_layout.closeDrawer(GravityCompat.START)
        return true
    }
}

1 个答案:

答案 0 :(得分:1)

您是否忘记了onClick()中的LoginFragment方法?

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view: View = inflater.inflate(R.layout.content_login, container, false)

    view.login_button.setOnClickListener {
        Log.i("BUTTON123","TEST")
        loginUser()
    }

    return view
}