常用代码

// 获取手机屏幕的宽高
  DisplayMetrics displayMetrics = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
  int screenWidth = displayMetrics.widthPixels;
  int screenheight = displayMetrics.heightPixels;
  // 我的手机是:320*480
  System.out.println(screenWidth + "*" + screenheight);
 
常用的xml配置:

android:cacheColorHint = "#00000000" 解决listview拖动变黑
Socket socket = new Socket("10.0.2.2", 8888);// 创建Socket对象

Toast.makeText(this, "", 2000).show();

android:layout_height="@dimen/topmenu_height"  //应用配置文件
android:background="#FFFF00" 黄色
android:textColor="#FFFFFF"  白色

// 隐藏输入法,用于EditText获得焦点自动弹出
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

android:textAppearance="?android:textAppearanceMedium"
 
android:textColor
android:autoLink

android:singleLine="true"    //一行
android:layout_alignParentTop="true"

android:maxLength="3"   //限制字符数量
android:inputType="number"   //限制输入的类型
android:hint="为空时显示"

android:drawableLeft="@drawable/title"  在文本框里添加图片
android:background=""
跑马灯的设置:单行显示,设置显示的宽度,获取焦点 
    android:ellipsize="marquee"
    android:singleLine="true"
    android:focusable="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:focusableInTouchMode="true"
    android:scrollHorizontally="true"

设置字体大小:sp
设置宽高:dip(dp)

问题解决:
1.自定义ListView无法响应点击:android:descendantFocusability=”blocksDescendant”即可,这样Item Layout就屏蔽了所有子控件获取Focus的权限

权限设置
<uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.WRITE_SETTINGS" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.DELETE_CACHE_FILES" />
 <uses-permission android:name="android.permission.RECORD_AUDIO" />
 <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
 <uses-permission android:name="android.permission.READ_OWNER_DATA" />
 <uses-permission android:name="android.permission.CLEAR_APP_USER_DATA" />
 <uses-permission android:name="android.permission.DELETE_PACKAGES" />
 <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera.autofocus"
  android:required="false" />
 <uses-permission android:name="android.permission.CALL_PHONE" />
 <uses-permission android:name="android.permission.SEND_SMS" />
 <uses-permission android:name="android.permission.RESTART_PACKAGES" />
 <uses-permission android:name="android.permission.GET_TASKS" />
 <uses-permission android:name="android.permission.VIBRATE" />
 <uses-permission android:name="android.permission.READ_LOGS" />
 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
 <uses-permission android:name="android.permission.READ_PHONE_STATE" />
 <uses-feature android:name="android.hardware.camera" />


tMenu_btn.setVisibility(View.INVISIBLE);


google Map key     "0gMFVZaryO4oqMq1rim3nkjEir2FBaZyhwMOrAQ"


requestWindowFeature(Window.FEATURE_NO_TITLE);//无标题
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//无信息栏

设置标题为图片+文字
this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
    R.layout.title);



拍照功能的几点注意:
1.setFixedSize的值不能乱设置,否则会报错,是固定的几个值

2.// param.setPreviewSize(h, w);// 设置预览照片的大小
// param.setPictureSize(h, w);// 设置照片的大小为手机的屏幕
这2个参数不能设置,在模拟器中是可以的,在真机上是不行的,可能是硬件上的差异,具体原因不明,不设置的话会采用系统的默认值
3.摄像头自动平衡的实现,在Activity中配置android:screenOrientation="landscape"这个属性

// 获得SD的路径
 public String getSDPath() {
  File sdPath = null;
  if (Environment.getExternalStorageState().equals(
    android.os.Environment.MEDIA_MOUNTED)) { // sd卡存在
   sdPath = new File(Environment.getExternalStorageDirectory()
     .getAbsolutePath()
     + File.separator + "mobileoa" + File.separator + "photo");// 获得根路径
  }
  return sdPath != null ? sdPath.toString() : "内存卡不存在!";
 }

 // 获得SDCard下图片的路径
 private List<String> getSDCard() {
  imagePaths = new ArrayList<String>();
  try {
   File file = new File(getSDPath());
   File[] files = file.listFiles();
   for (File theFile : files) {
    if (isImageFile(theFile.getPath())) {
     imagePaths.add(theFile.getPath());
    }
   }
  } catch (Exception e) {
   return imagePaths;
  }
  return imagePaths;
 }

 // 判断是否是图片文件
 private boolean isImageFile(String fileName) {
  String extension = fileName.substring(fileName.lastIndexOf(".") + 1,
    fileName.length()).toLowerCase();
  if (extension.equals("jpg") || extension.equals("png")
    || extension.equals("gif") || extension.equals("jpeg")
    || extension.equals("bmp")) {
   return true;
  }
  return false;
 }

        <!-- 登录界面编辑框的样式 -->
 <style name="login_edit_style">
  <item name="android:layout_width">fill_parent</item>
  <item name="android:layout_height">wrap_content</item>
  <item name="android:background">@drawable/username_bg</item>
  <item name="android:textSize">20sp</item>
  <item name="android:paddingLeft">70dp</item>
  <item name="android:saveEnabled">true</item>
  <item name="android:layout_marginLeft">5dp</item>
  <item name="android:layout_marginTop">5dp</item>
  <item name="android:layout_marginRight">5dp</item>
  <item name="android:layout_marginBottom">5dp</item>
  <item name="android:maxLength">15</item>
 </style>


<RelativeLayout android:id="@+id/om_rl_title"
     android:layout_marginTop="2dp" android:layout_width="fill_parent"
     android:layout_height="wrap_content">
 <EditText android:id="@+id/mail_title" style="@style/mail_edit_style"
 android:paddingLeft="50dp" android:hint="请输入邮件标题"
 android:layout_alignParentTop="true" />
 <TextView android:id="@+id/om_tv_title" android:text="标题:"
      android:gravity="center_vertical" android:paddingLeft="7dp"
      android:textSize="18sp" android:textColor="@drawable/black"
      android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:layout_marginRight="15dp" 
      android:layout_alignLeft="@+id/mail_title"
      android:layout_alignTop="@+id/mail_title"
      android:layout_alignBottom="@+id/mail_title" />
</RelativeLayout>


/* 生成Menu菜单 */
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  menu.add(0, 0, 0, "恢复系统默认设置").setIcon(R.drawable.menu_syssettings);
  return true;
 }

 /* Menu菜单事件 */
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
  case 0:
   break;

  }
  return true;
 }


public void setBgColor() {
  // 设置背景色
  SharedPreferences sp = getApplicationContext().getSharedPreferences(
    "mycolor", Context.MODE_PRIVATE); // 获得Preferences

  if (sp.getInt("mycolor", 0) != 0) {
   mainRelativeLayout.setBackgroundColor(sp.getInt("mycolor", 0));// 设置窗体的背景色
  }
 }
 // 销毁
 protected void onDestroy() {
  super.onDestroy();
 }

 // 暂停
 protected void onPause() {
  super.onPause();
 }

 @Override
 protected void onResume() {
  setBgColor();
  super.onResume();
 }

 // 重新启动
 protected void onRestart() {
  setBgColor();
  super.onRestart();
 }
 

 

你可能感兴趣的:(代码)