繁体   English   中英

登录成功后如何转到新活动?

[英]How to go to a new activity after login success?

这是我的登录活动

class MainActivity : AppCompatActivity() {
//edittext
private var edemail: EditText? = null
private var edpassword: EditText? = null
override fun onCreate(savedInstanceState: Bundle?) {


    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)


    //getting it from xml
    edemail = findViewById<EditText>(R.id.edtemail)
    edpassword = findViewById<EditText>(R.id.edtpassword)
    findViewById<Button>(R.id.btn_login).setOnClickListener {
         loginfun()
//            val intent = Intent(applicationContext, HelloActivity::class.java)
//            startActivity(intent)
//            finish()
    }

    findViewById<TextView>(R.id.txtregister).setOnClickListener {
        val intent = Intent(applicationContext, RegisterActivity::class.java)
        startActivity(intent)
        finish()
    }





}

private fun loginfun(){
//getting the record values
val email = edemail?.text.toString()
val password = edpassword?.text.toString()
var test:Boolean=false
//creating volley string request
val stringRequest = object : StringRequest(Request.Method.POST, 
EndPoints.URL_GET_USER,
        Response.Listener<String> { response ->
            try {
                val obj = JSONObject(response)
                  test = obj.getBoolean("error")
                Toast.makeText(applicationContext, obj.getString("message"), 
  Toast.LENGTH_LONG).show()
            } catch (e: JSONException) {
                e.printStackTrace()
            }
        },
        object : Response.ErrorListener {
            override fun onErrorResponse(volleyError: VolleyError) {
                Toast.makeText(applicationContext, volleyError.message, 
 Toast.LENGTH_LONG).show()
            }
        }) {
    @Throws(AuthFailureError::class)
    override fun getParams(): Map<String, String> {
        val params = HashMap<String, String>()
        params.put("email", email)
        params.put("password", password)
        return params
    }
}

 //adding request to queue
 VolleySingleton.instance?.addToRequestQueue(stringRequest)
 go(test)

}
private fun go(state:Boolean)

{if(state == true){val intent = Intent(applicationContext, HelloActivity::class.java)
    startActivity(intent)
    finish() }

}

}

如果登录成功,我想转到HelloActivity。 你能帮我做这个吗?

它对我不起作用,问题是无法在请求中更改 test 的值。

如何使用我从我的 php 文件中得到的错误boolean再次重新分配。 其他一切都运行良好。

1- 您应该从此处删除对go(test)的调用:

//adding request to queue
 VolleySingleton.instance?.addToRequestQueue(stringRequest)
 go(test) // this line should be removed

2- 当您得到响应时,您应该在解析error值后添加调用,如下所示:

Response.Listener<String> { response ->
            try {
                val obj = JSONObject(response)
                test = obj.getBoolean("error")
                go(test) // this line should be here 
                Toast.makeText(applicationContext, obj.getString("message"), 
                Toast.LENGTH_LONG).show()
            } catch (e: JSONException) {
                e.printStackTrace()
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM