Android tips2

1 设置模拟器的内存
  对于windows用户,应该在用户目录下的.android目下可以找到类似的内容。

找到config.ini文件,修改(增加)内容:

hw.ramSize=512
vm.heapSize=32


2 获取屏幕大小
   width:this.getWindow().getWindowManager().getDefaultDisplay()
                        .getWidth()
    height:this.getWindow().getWindowManager().getDefaultDisplay()
                        .getHeight());

3 Bitmap转Drawble

   Resources res=getResources();
Bitmap bm=BitmapFactory.decodeResource(res, R.drawable.header);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
Drawable drawable = (Drawable)bitmapDrawable;
Drawable 转Bitmap:

    Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();


4 关于ExpandableListActivity
  public class MainActivity extends ExpandableListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// 准备顶层列表数据
List<Map<String, String>> topList = new ArrayList<Map<String, String>>();

Map<String, String> topMap1 = new HashMap<String, String>();
Map<String, String> topMap2 = new HashMap<String, String>();
topMap1.put("month", "三月测评项");
topMap2.put("month", "四月测评项");
topList.add(topMap1);
topList.add(topMap2);

// 准备二层列表数据
List<List<Map<String, String>>> nestList = new ArrayList<List<Map<String, String>>>();

// 准备二层列表第一个子列表数据
List<Map<String, String>> nestList1 = new ArrayList<Map<String, String>>();
Map<String, String> nestMap1 = new HashMap<String, String>();
Map<String, String> nestMap2 = new HashMap<String, String>();
Map<String, String> nestMap3 = new HashMap<String, String>();
nestMap1.put("test", "看手");
nestMap2.put("test", "吃手");
nestMap3.put("test", "玩手");
nestList1.add(nestMap1);
nestList1.add(nestMap2);
nestList1.add(nestMap3);

// 准备二层列表第二个子列表数据
List<Map<String, String>> nestList2 = new ArrayList<Map<String, String>>();
Map<String, String> nestMap4 = new HashMap<String, String>();
Map<String, String> nestMap5 = new HashMap<String, String>();
nestMap4.put("test", "翻身");
nestMap5.put("test", "辨别声音来源方位");
nestList2.add(nestMap4);
nestList2.add(nestMap5);

// 把子列表数据放入
nestList.add(nestList1);
nestList.add(nestList2);

// 准备数据匹配器
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
this, //1.上下文
topList, //2.顶层数据列表
android.R.layout.simple_expandable_list_item_1, // 3.一层显示样式
new String[]{"month"}, //4.顶层map的键
new int[]{android.R.id.text1}, // 5.顶层数据显示的View ID
nestList, //6.二层数据列表
android.R.layout.simple_list_item_1, //7.二层显示样式
new String[]{"test"}, //8.二层map的键
new int[]{android.R.id.text1} //9.二层数据显示的View ID
);

//设置数据匹配器
this.setListAdapter(adapter);

}

@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int

childPosition, long id) {
Toast.makeText(this, "嵌套列表被点击,顶层列表定位"+groupPosition+"二层列表定

位"+childPosition, Toast.LENGTH_LONG).show();
return super.onChildClick(parent, v, groupPosition, childPosition, id);
}

@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(this, "顶层列表收缩,列表定位"+groupPosition, Toast.LENGTH_LONG).show();
super.onGroupCollapse(groupPosition);
}

@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(this, "顶层列表展开,列表定位"+groupPosition, Toast.LENGTH_LONG).show();
super.onGroupExpand(groupPosition);
}

5 模拟器代理上网
   A emulator -avd your_avd_name -http-proxy http:// username :password @ ip :port


  B sqlite3 /data/data/com.android.providers.settings/databases/settings.db "INSERT INTO system VALUES

(99,'http_proxy',' ip :port')"

  C 启动模拟器,然后进入 settings->Wireless controls->Mobile networks->Access Point Names

然后打开出现在列表中的access point;

然后下面这样设置:

- Proxy : your proxy address 

- Port : your proxy port 

- Username : your username if needed, or <Not set> 

- Password : your password if needed, or <Not set>

6 菜单资源文件
    放在res\menu目录下,比如file_menu.xml
  <?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
    android:title="File"
    android:icon="@drawable/file">
        <menu>
            <group
            android:id="@+id/noncheckable_group"
            android:checkableBehavior="none">
           
                <item
                android:id="@+id/newFile"
                android:title="New"
                android:alphabeticShortcut="n"/>
               
                <item
                android:id="@+id/openFile"
              android:title="Open"
              android:alphabeticShortcut="o"/>
             
                <item
                android:id="@+id/saveFile"
                android:title="Save"
                android:alphabeticShortcut="s"/>
            </group>
        </menu>
    </item>

    <item android:title="Edit" android:icon="@drawable/edit">
        <menu>
            <group android:id="@+id/edit_group"
                    android:checkableBehavior="single">
                   
                <item android:id="@+id/cut"
                      android:title="Cut" />
                     
                <item android:id="@+id/copy"
                      android:title="Copy"/>
                     
                <item android:id="@+id/past"
                      android:title="Past"/>
            </group>
        </menu>
    </item>

主程序中创建菜单
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.test_menu);
       mi = new MenuInflater(this);
    }
   
    /*
     * 创建菜单
     */
    public boolean onCreateOptionsMenu(Menu menu) {
        mi.inflate(R.menu.file_menu, menu);
        return true;
    }


7
  有的时候,在设计了数组后,可以数组变化后,再通知arraydataper:

adapter = new ArrayAdapter<EarthQuakeInfo>(this,android.R.layout.simple_list_item_1,infoList);
    //设置ListView的适配器为adapter
    list.setAdapter(adapter);
  infoList.add(info);
  // 通知array adapter
adapter.notifyDataSetChanged();


8 对话框中有多个可以供选择的项
    myBtn = (Button)findViewById(R.id.Button01);
        final String[] items = {"奥尔良鸡腿堡","麻辣鸡腿堡","咖啡"};
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        myBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//builder.setTitle("请点餐").setItems(items, new

DialogInterface.OnClickListener() {
// 如果改为如下方法,以单项按钮样式显示
builder.setTitle("请点餐").setSingleChoiceItems(items,-1, new

DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myTV.setText(items[which]);
}
});
AlertDialog ad = builder.create();
ad.show();
}
});

9 文件IO的用法
   try {
            //创建文件
            file = new File(FILE_PATH , FILE_NAME);
            file.createNewFile();
           
            //打开文件file的OutputStream
            out = new FileOutputStream(file);
            String infoToWrite = "test";
            //将字符串转换成byte数组写入文件
            out.write(infoToWrite.getBytes());
            //关闭文件file的OutputStream
            out.close();
           
            //打开文件file的InputStream
            in = new FileInputStream(file);
            //将文件内容全部读入到byte数组
            int length = (int)file.length();
            byte[] temp = new byte[length];
            in.read(temp, 0, length);
            //将byte数组用UTF-8编码并存入display字符串中
            display =  EncodingUtils.getString(temp,TEXT_ENCODING);
            //关闭文件file的InputStream
            in.close();

}

10  Android启动飞行模式
  1)检查当前是否用了飞行模式
     import android.content.Context;
import android.provider.Settings;

public static boolean isAirplaneModeOn(Context context){
return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,

0) != 0;
}

   响应广播代码:
   import android.content.Context;
import android.content.Intent;
import android.provider.Settings;

public static void setAirplaneMode(Context context, boolean status){
// 先判斷目前是已否開啟飛航模式
boolean isAirplaneModeOn = isAirplaneModeOn(context);

if((status && isAirplaneModeOn) || (!status && !isAirplaneModeOn)){
return;
}

int mode = status ? 1 : 0;
// 設定飛航模式的狀態並廣播出去
Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, mode);
Intent i = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
i.putExtra("state", mode);
context.sendBroadcast(i);
}
XML配置:
   <uses-permission android:name="android.permission.WRITE_SETTINGS" />

11 取的ANDROID上的系统机器本身信息
  import android.os.Build;

// 主機版名稱
String board = Build.BOARD;

// 品牌名稱
String brand = Build.BRAND;

// CPU + ABI
String cpu = Build.CPU_ABI;

// 設備名稱
String device = Build.DEVICE;

// 版本號碼
String display = Build.DISPLAY;

// 設備識別碼
String fingerprint = Build.FINGERPRINT;

// HOST
String host = Build.HOST;

// 版本號碼
String id = Build.ID;

// 製造商
String manufacturer = Build.MANUFACTURER;

// 模組號碼
String model = Build.MODEL;

// 產品名稱
String product = Build.PRODUCT;

// 設備描述
String tags = Build.TAGS;

// 設備類別; user or eng
String type = Build.TYPE;

// USER
String user = Build.USER;

  

你可能感兴趣的:(设计模式,android,xml,mobile,Windows Mobile)