android - 在Android的AlertDialog中处理Edittext

标签 android kotlin android-edittext android-alertdialog

我在Alert对话框中有一个edittext .....如果ediitext不为空,我的操作效果很好.....我想在alertdialog中ediitext为空时设置焦点。
需要帮助,在此先感谢
我尝试了以下代码,请看看:---

buynow.setOnClickListener {
                    val mBuild: AlertDialog.Builder = AlertDialog.Builder(this@Product_details)
                    val mView: View = layoutInflater.inflate(R.layout.buynowdialog, null)



                    val titleimage=mView.findViewById<TextView>(R.id.titleimage2)  as TextView
                    val imagerate=mView.findViewById<ImageView>(R.id.imagebuy) as ImageView
                    titleimage.setText(ygd)
                    Glide.with(getApplicationContext()).load(res?.body()!!.data.product_images.get(0).image).into(imagerate);
                  
                  
                  val buynumber = mView.findViewById<EditText>(R.id.editbuy)

                  val btnSubmit =
                        mView.findViewById(R.id.btnbuynow) as Button
                    Log.e("checkid",id.toString())


                    mBuild.setView(mView)
                    val dialog: AlertDialog = mBuild.create()



                  btnSubmit.setOnClickListener(object : View.OnClickListener {
                      override  fun onClick(v: View?) {
                          val value: String = buynumber.getText().toString()

                          val finalValue = value.toInt()
                         if (value.isEmpty()) {
                              Toast.makeText(
                                  applicationContext, "Data is missing",Toast.LENGTH_LONG
                              ).show()
                              editbuy.error = "Email required"
                              editbuy.requestFocus()

                          }
                          val token: String =
                              SharedPrefManager.getInstance(
                                  applicationContext
                              ).user.access_token.toString()
                          RetrofitClient.instancecart.buynow(token,id,finalValue)
                              .enqueue(object : Callback<ResponseBaseCartAdd> {
                                  override fun onFailure(call: Call<ResponseBaseCartAdd>, t: Throwable) {
                                      Log.d("res", "" + t)


                                  }

                                  override fun onResponse(
                                      call: Call<ResponseBaseCartAdd>,
                                      response: Response<ResponseBaseCartAdd>
                                  ) {
                                      Log.e("hi",id)
                                      var res = response
                                      Log.e("checkres",res.toString())
                                      Log.d("response check ", "" + response.body()?.status.toString())
                                      if (res.isSuccessful) {
                                          Toast.makeText(
                                              applicationContext,
                                              res.body()?.user_msg,
                                              Toast.LENGTH_LONG
                                          ).show()
                                      dialog.dismiss()
                                          Log.d("kjsfgxhufb",response.body()?.user_msg.toString())
                                      }
                                      else{
                                          try {
                                              val jObjError =
                                                  JSONObject(response.errorBody()!!.string())
                                              Toast.makeText(
                                                  applicationContext,
                                                  jObjError.getString("message")+jObjError.getString("user_msg"),
                                                  Toast.LENGTH_LONG
                                              ).show()
                                          } catch (e: Exception) {
                                              Toast.makeText(applicationContext, e.message, Toast.LENGTH_LONG).show()
                                              Log.e("errorrr",e.message)
                                          }
                                      }
                                  }
                              })
                      }
                  })
                    dialog.show()
                }
上面的代码导致logtcat错误导致崩溃:-java.lang.NumberFormatException: For input string: ""需要帮助谢谢:)

最佳答案

NumberFormatException is an Exception that might be thrown when you try to convert a String into a number, where that number might be an int , a float , or any other Java numeric type.

 val finalValue = value.toInt() // value is empty. That's why problem
您的 finalValue Empty or null 。您必须检查它。

isNotEmpty() is used to find if the String is not empty/String is length 0 and not null.


DEMO
    if (value.isNotEmpty()) {
    val finalValue = value.toInt()
    val token: String =SharedPrefManager.getInstance(applicationContext).user.access_token.toString()
    RetrofitClient.instancecart.buynow(token,id,finalValue)
    .....your task.....
   }
  else
  {
    Toast.makeText(applicationContext, "Data is missing",Toast.LENGTH_LONG).show()
    editbuy.error = "Email required"
    editbuy.requestFocus()
    
  }

关于android - 在Android的AlertDialog中处理Edittext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63446681/

相关文章:

android - 为 TextView 获取三角形背景

android - 在 KitKat 4.4.4 API 19 上使用改造上传图像时应用程序崩溃

android - 如何在edittext中设置不可编辑的文本

java - 以编程方式向 EditText 添加引力

android - Android 的 MediaRecorder 的 getMaxAmplitude() 函数实际上给了我什么?

android - Butterknife 8.4.0 - 找不到 ID 为 'android-apt' 的插件

android - 通过市场强制更新应用程序,就像用户单击 "Update"

kotlin - 如何使用runBlocking等待CoroutineScope完成

android - 未找到导入 kotlinx.coroutines.flow.*

java - 如何从 Java 类获取结果以显示在 Android Activity 中?