【发布时间】:2011-07-26 01:00:24
【问题描述】:
我正在尝试在我的 Android 应用程序启动时弹出一个文本框,其中包含一些免责声明和应用程序信息。有谁知道如何实现这个?也可以从txt文件中读取吗?
谢谢
【问题讨论】:
-
试试 AlertDialog:stackoverflow.com/questions/26097513/… - 第一个答案有示例代码。
我正在尝试在我的 Android 应用程序启动时弹出一个文本框,其中包含一些免责声明和应用程序信息。有谁知道如何实现这个?也可以从txt文件中读取吗?
谢谢
【问题讨论】:
在你想要的活动中使用它并在 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();
}
【讨论】:
示例代码在 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()
}
【讨论】:
假设你想设置一个弹出文本框来点击一个按钮让我们说 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)
}
【讨论】:
您可以使用 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
【讨论】: