【问题标题】:Android Pop-up message安卓弹出消息
【发布时间】:2011-07-26 01:00:24
【问题描述】:

我正在尝试在我的 Android 应用程序启动时弹出一个文本框,其中包含一些免责声明和应用程序信息。有谁知道如何实现这个?也可以从txt文件中读取吗?

谢谢

【问题讨论】:

标签: android popup


【解决方案1】:

在你想要的活动中使用它并在 OnCreate 方法中调用它

public void popupMessage(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage("No Internet Connection. Check Your Wifi Or enter code hereMobile Data.");
        alertDialogBuilder.setIcon(R.drawable.ic_no_internet);
        alertDialogBuilder.setTitle("Connection Failed");
        alertDialogBuilder.setNegativeButton("ok", new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Log.d("internet","Ok btn pressed");
                // add these two lines, if you wish to close the app:
                finishAffinity();
                System.exit(0);
            }
        });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }

【讨论】:

    【解决方案2】:

    示例代码在 kotlin 中显示自定义对话框:

    fun showDlgFurtherDetails(context: Context,title: String?, details: String?) {
    
        val dialog = Dialog(context)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setCancelable(false)
        dialog.setContentView(R.layout.dlg_further_details)
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        val lblService = dialog.findViewById(R.id.lblService) as TextView
        val lblDetails = dialog.findViewById(R.id.lblDetails) as TextView
        val imgCloseDlg = dialog.findViewById(R.id.imgCloseDlg) as ImageView
    
        lblService.text = title
        lblDetails.text = details
        lblDetails.movementMethod = ScrollingMovementMethod()
        lblDetails.isScrollbarFadingEnabled = false
        imgCloseDlg.setOnClickListener {
            dialog.dismiss()
        }
        dialog.show()
    }
    

    【讨论】:

      【解决方案3】:

      假设你想设置一个弹出文本框来点击一个按钮让我们说 bt 其 id 是 button,然后使用 Toast 编码> 有点像这样:

      Button bt;
      bt = (Button) findViewById(R.id.button);
      bt.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {            
      Toast.makeText(getApplicationContext(),"The text you want to display",Toast.LENGTH_LONG)
      }
      

      【讨论】:

        【解决方案4】:

        您可以使用 Dialog 轻松创建它

        使用上下文创建一个对话框实例

        Dialog dialog = new Dialog(contex);
        

        您可以随意设计布局。

        您可以通过以下方式将此布局添加到您的对话框中 dialog.setContentView(R.layout.popupview);//popup view is the layout you created

        然后您可以使用findViewById 方法访问其内容(文本视图等)

        TextView txt = (TextView)dialog.findViewById(R.id.textbox);
        

        您可以在此处添加任何文本。文本可以存储在 res\values 中的 String.xml 文件中。

        txt.setText(getString(R.string.message));
        

        然后最后显示弹出菜单

        dialog.show();
        

        更多信息 http://developer.android.com/guide/topics/ui/dialogs.html

        http://developer.android.com/reference/android/app/Dialog.html

        【讨论】:

          【解决方案5】:

          如果你想要一个自动关闭的弹出窗口,你应该寻找Toasts。但是,如果您希望用户在继续之前必须先关闭一个对话框,您应该寻找Dialog

          对于这两种方法,都可以读取包含您要显示的文本的文本文件。但您也可以对文本进行硬编码或使用 R.String 设置文本。

          【讨论】:

          • 你的方法非常好,如何为 2 条派上用场的消息(例如 toast)做到这一点
          猜你喜欢
          • 2015-02-26
          • 2011-12-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-28
          • 2013-11-15
          • 1970-01-01
          相关资源
          最近更新 更多