Android编程经典代码集锦(复制,粘贴,浏览器调用,Toast显示,自定义Dialog等)
程序员文章站
2023-12-19 17:25:28
本文实例总结了android编程经典代码段。分享给大家供大家参考,具体如下:
1、 复制,粘贴
clip = (clipboardmanager)getsyst...
本文实例总结了android编程经典代码段。分享给大家供大家参考,具体如下:
1、 复制,粘贴
clip = (clipboardmanager)getsystemservice(context.clipboard_service); clip.settext("copy"); // 复制 clip.gettext(); // 粘贴
2、调用浏览器
核心代码如下:
intent intent = new intent(); intent.setaction("android.intent.action.view"); uri content_url = uri.parse("//www.jb51.net"); intent.setdata(content_url); //调用指定浏览器—uc intent.setclassname("com.uc.browser", "com.uc.browser.activityupdate"); startactivity(intent);
具体步骤与实现代码可参考前面一篇《android开发之浏览器用法实例详解》
3、根据包名,去软件市场查找
intent intent = new intent(intent.action_view, uri.parse("market://search?q=pname:com.acp.main")); startactivity(intent);
4、toast一直显示
final toast toast = toast.maketext(context,"toast", toast.length_long); timer timer = new timer(); timer.schedule(new timertask() { @override public void run() { // todo auto-generated methodstub while (flag) { toast.show(); } } }, 10);
5、自定义dialog
final dialog minfodlg = new dialog(context,r.style.dialog); // r.style.dialog – 去掉dialog的最上面title部分 layoutinflater factory = layoutinflater.from(context); view ncurrview = factory.inflate(r.layout.mydialog, null); ncurrview.setbackgroundresource(r.drawable.bgline); linearlayout nparentlayout = (linearlayout)ncurrview.findviewbyid(r.id.iparents); nparentlayout.setbackgroundresource(r.drawable.titlebgline); minfodlg.setcontentview(ncurrview); button nbt1 = (button)ncurrview.findviewbyid(r.id.button1); nbt1.settext("返回"); nbt1.settypeface(typeface.create(typeface.serif, typeface.bold)); nbt1.setonclicklistener(new button.onclicklistener() { @override public void onclick(view v) { // todo auto-generated methodstub minfodlg.dismiss(); } }); minfodlg.show();
r.style.dialog:
<?xml version="1.0"encoding="utf-8"?> <resources> <style name="dialog"parent="@android:style/theme.dialog"> <itemname="android:windownotitle">true</item> </style> </resources>
r.drawable.titlebgline:
<?xml version="1.0"encoding="utf-8"?> <shapexmlns:android="http://schemas.android.com/apk/res/android"> <gradientandroid:startcolor="#ff9911" android:endcolor="#ff9911"/> <strokeandroid:width="1dp" android:color="@color/inputtxt" /> <cornersandroid:radius="5dp" /> </shape>
希望本文所述对大家android程序设计有所帮助。