【问题标题】:How can I dismiss the alertbox after clicking back button单击后退按钮后如何关闭警报框
【发布时间】:2017-11-13 09:48:03
【问题描述】:

我有调用适配器类的活动。在适配器类中,单击项目后,将打开对话框。现在我无法在单击后退按钮后关闭此对话框。

这是我的活动课:

public class MainActivity extends AppCompatActivity{
      protected void onCreate(Bundle savedInstanceState) {

         // fetching the required data
         // blogs is recyclerview
         blogs.setAdapter(new BlogAdapter(MainActivity.this,blogArrayList));
      }


@Override
    public void onBackPressed() {
        //not printed
        Log.i("Back button pressed ","MainActivity");
   }

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        //not printed
        Log.i("On key down ","Main Activity");
         return super.onKeyDown(keyCode, event);

    }

}

适配器类

class BlogAdapter(var mcontext: Context, var post:ArrayList<Blog>): RecyclerView.Adapter<RecyclerView.ViewHolder>() {

override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        holder.itemView.setOnClickListener {
            openDialog(post.get(position),mcontext)
        }
}
 private fun  openDialog(post: Blog?,context:Context) {

        val layoutInflaterAndroid = LayoutInflater.from(context)
        val mView = layoutInflaterAndroid.inflate(R.layout.individual_blog, null)

        val alertDialogBuilder = AlertDialog.Builder(ContextThemeWrapper(context, R.style.AppFullScreen_Dialog_Theme))

        alertDialogBuilder.setView(mView)

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            mView.title_blog.text = Html.fromHtml(post?.postTitle,Html.FROM_HTML_MODE_LEGACY)
        } else {
            @Suppress("DEPRECATION")
            mView.title_blog.text=Html.fromHtml(post?.postTitle)
        }


        val alertDialog = alertDialogBuilder.create()
        alertDialog!!.show()

        alertDialogBuilder.setOnKeyListener { dialog, keyCode, event ->

            Log.i("Set On Key Listener ","dialog")
            if (keyCode==KeyEvent.KEYCODE_BACK){
                alertDialog.dismiss()
            }
            true
        }

    }
}

【问题讨论】:

  • 你也可以使用 alertDialogBu​​ilder.setCancelable(true); 让你的对话框可以取消
  • 为什么要用两种语言。你应该只使用 java 或 kotlin。根据您的需要,您必须创建接口并从 Main 调用它。然后显示对话框。你可以在那里随心所欲地制作。
  • 实际上,该项目是用 Java 编写的,但我想弄脏 Kotlin。所以这个项目有两种语言。对于这种情况,首先使用回收器视图和适配器类加载数据。那么在touch上,item(recycler view item)调用主activity中的接口方法呢? @SanKoKo
  • 您的onBackPressedonKeyDown 超出MainActivity 范围。你能重新检查第 8 行的“}”吗?
  • 也尝试在alertDialogBuilder.create()之后添加alertDialog!!.cancelable = true

标签: android dialog kotlin android-alertdialog dismiss


【解决方案1】:

您可以使用适配器中的上下文在您的活动中显示对话框,然后检查 onBackPressed 方法,如果您的对话框可见,则关闭它,否则 super.onBackPressed()

【讨论】:

  • 我试过这种方式,但点击后退按钮后 onBackPressed() 没有调用
【解决方案2】:

这一定有帮助。

  @Override
public void onBackPressed() {
   dialog.dismiss();
}

【讨论】:

  • 我已经覆盖了活动中的 onBackPressed() 但是当我按下返回按钮时 onBackPressed() 没有调用。 @威廉
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-30
  • 1970-01-01
  • 2013-04-27
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多