Android Programming classic code collection of copy paste browser call Toast display custom Dialog etc

  • 2020-12-18 01:55:14
  • OfStack

The example in this article summarizes classic Android programming snippets. To share for your reference, the details are as follows:

1. Copy and 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("https://www.ofstack.com");
intent.setData(content_url);
// Call the specified browser - UC
intent.setClassName("com.uc.browser", "com.uc.browser.ActivityUpdate");
startActivity(intent);

For detailed steps and implementation code, please refer to the previous article "Android Developed Browser Usage Example Details".

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


Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:com.acp.main"));
startActivity(intent);

4. Toast1 direct display


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. Customize Dialog


final Dialog mInfoDlg = new Dialog(context,R.style.dialog);
// R.style.dialog  �   To get rid of dialog The top of the title Part of the 
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 has been helpful in Android programming.


Related articles: