Android Classic Code collection (copy, paste, browser call, toast Display, custom dialog, etc.) _android

Source: Internet
Author: User

The examples in this article summarize the classic code snippets for Android programming. Share to everyone for your reference, specific as follows:

1, copy, paste

Clip = (Clipboardmanager) getsystemservice (context.clipboard_service);
Clip.settext ("copy"); Copy
clip.gettext ();//paste

2, call the browser

The core code is as follows:

Intent Intent = new Intent ();
Intent.setaction ("Android.intent.action.VIEW");
Uri Content_url = Uri.parse ("http://www.jb51.net");
Intent.setdata (Content_url);
Invokes the specified browser-uc
intent.setclassname ("Com.uc.browser", "com.uc.browser.ActivityUpdate");
StartActivity (Intent);

Specific steps and implementation code can refer to the previous "Android Development browser usage Examples"

3, according to the package name, to the software market search

Intent Intent = new Intent (Intent.action_view, Uri.parse ("Market://search?q=pname:com.acp.main"));
StartActivity (Intent);

4, Toast has been shown

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, custom Dialog

Final Dialog Minfodlg = new Dialog (context,r.style.dialog);
r.style.dialog– Remove the top title section of the dialog
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 ("return");
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>

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.