android 常用代码备份
android 全屏代码
//无title requestWindowFeature(Window.FEATURE_NO_TITLE); //全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);或使用配置。在当前Activity中使用主题
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
屏目横竖切换、
//如果是竖排,则改为横排 if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } //如果是横排,则改为竖排 else if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); }
给Activity 添加menu.
public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 0, 0, "关于"); //参数1:组名,参数2:item的值,参数3:排序,参数4:menu展示的值 menu.add(0, 1, 1, "退出"); menu.add(1, 0, 0, "退出"); menu.add(1, 1, 1, "退出"); return super.onCreateOptionsMenu(menu); } //菜单响应 public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); if(item.getGroupId()==1){ switch (item.getItemId()) { case 0: this.finish(); break; case 1: this.finish(); break; default: break; } }else{ switch (item.getItemId()) { case 0: Toast.makeText(this, "欢迎", Toast.LENGTH_LONG).show(); break; case 1: this.finish(); break; case 2: this.finish(); break; } } return true; }
RatingBar 的使用
<RatingBar android:id="@+id/ratingBar" android:numStars="5" android:rating="1.5" android:layout_width="wrap_content" android:layout_height="wrap_content"> </RatingBar>
RatingBar bar = (RatingBar) this.findViewById(R.id.ratingBar); bar.setRating(2.5f); bar.getRating(); bar.setOnRatingBarChangeListener(new OnRatingBarChangeListener(){ @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { System.out.println("ratingBar:"+ratingBar.getRating()); System.out.println("rating:"+rating); } });效果图:
Tosat 弹出带图片的提示。
Toast toast = Toast.makeText(getApplicationContext(), "用户名不能为空", 0); toast.setGravity(Gravity.CENTER, 0, 0); View toastView = toast.getView(); ImageView image = new ImageView(LayoutDemoActivity.this); image.setImageResource(R.drawable.icon1); LinearLayout ll = new LinearLayout(LayoutDemoActivity.this); ll.addView(image); ll.addView(toastView); toast.setView(ll); toast.show();效果图:
通知:
Intent intent = new Intent(ImageViewActivity.this,OtherActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(ImageViewActivity.this, 0, intent, 0);
Notification notify = new Notification();
notify.icon=R.drawable.icon1;
notify.tickerText="启动otherActivity通知";
notify.when=System.currentTimeMillis();
notify.defaults=Notification.DEFAULT_SOUND;
notify.defaults=Notification.DEFAULT_ALL;
notify.setLatestEventInfo(ImageViewActivity.this, "普通通知", "点击查看", contentIntent);
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, notify);
检测是否安装内存卡:
private boolean checkSdcard() { if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ return true; } return false; }
弹出对话框:
ProgressDialog proDialog = ProgressDialog.show(LoginUserActivity.this,"请稍候","正在登陆...", true, true);proDialog = new ProgressDialog(LoginUserActivity.this); proDialog.setIcon(R.drawable.icon); proDialog.setTitle("ProgressDialog"); proDialog.setMessage("Please wait while loading application list..."); proDialog.setCancelable(false); proDialog.show(); proDialog.dismiss();当程序退出时。结束自己的进程。
android.os.Process.killProcess(android.os.Process.myPid());