Android开发笔记
Android 中国SDK:
http://wear.techbrood.com/
Android SDK Manager 代理设置: http://www.cnblogs.com/sunzn/p/4242131.html
使用Intellij Idea 开发Android:
http://www.tuicool.com/articles/a2MNna
得到当前Activity的RelativeLayout
RelativeLayout root = (RelativeLayout)View.inflate(this, R.layout.activity_main, null);
遍历控件
http://www.open-open.com/lib/view/open1391951537129.html
/**
* @note 获取该activity所有view
* */
public List<View> getAllChildViews() {
View view = this.getWindow().getDecorView();
return getAllChildViews(view);
}
private List<View> getAllChildViews(View view) {
List<View> allchildren = new ArrayList<View>();
if (view instanceof ViewGroup) {
ViewGroup vp = (ViewGroup) view;
for (int i = 0; i < vp.getChildCount(); i++) {
View viewchild = vp.getChildAt(i);
allchildren.add(viewchild);
allchildren.addAll(getAllChildViews(viewchild));
}
}
return allchildren;
}
获取最上层Activity
http://blog.sina.com.cn/s/blog_4c0706560101063n.html
String getTopActivity(Activity context)
{
ActivityManager manager = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE) ;
List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1) ; if(runningTaskInfos != null)
return (runningTaskInfos.get(0).topActivity).toString() ;
else
return null ;
}
最后不要忘记在AndroidManifest.xml中增加权限:
<uses-permission android:name = “android.permission.GET_TASKS”/>
这个方法可能没有用,我所需要的是获取当前程序的当前Activity
网格权限
socket failed:EACCES(Permission denied)
Android净干这脱了裤子放屁的事,写个代码,还要指定权限,就不让程序员省心。
做一个最全的权限,放到 AndroidManifest 里吧。
官方定义:http://developer.android.com/reference/android/Manifest.permission.html
中文:http://blog.sina.com.cn/s/blog_798c1d510101d744.html
<uses-permission android:name="android.permission.INTERNET" />
Ajax助手类
让我们实现一个类似于 jQuery 的Ajax 方法吧。 万恶的Java没有实现这个方法。
package cmn; import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.widget.Toast; import com.google.gson.Gson; import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils; import java.util.ArrayList;
import java.util.List;
import java.util.Map; import dzx.cn.dzx.R; /**
* Created by Administrator on 2015/7/14.
*/
public class NetHelper {
public static void Post(Activity context,String Url, StringDict dict, IAjaxCallback callback) {
new NetExecuter().Post(context,Url, dict, callback);
}
} class NetExecuter {
private String ErrorMsg; public void Post(final Activity context, String Url, final StringDict Dict, final IAjaxCallback Callback) {
ErrorMsg = ""; AsyncTask<String, StringDict, String> task = new AsyncTask<String, StringDict, String>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
} @Override
protected String doInBackground(String... params) {
String MyUrl = params[0];
HttpPost httpRequest = new HttpPost(MyUrl);
HttpResponse httpResponse = null;
List<NameValuePair> param = new ArrayList<NameValuePair>();
if (Dict != null) {
for (final Map.Entry<String, String> entry : Dict.entrySet()) {
param.add(new NameValuePair() {
@Override
public String getName() {
return entry.getKey();
} @Override
public String getValue() {
return entry.getValue();
}
});
}
} try {
httpRequest.setEntity(new UrlEncodedFormEntity(param, HTTP.UTF_8));
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000);
httpResponse = client.execute(httpRequest); int status = httpResponse.getStatusLine().getStatusCode();
String strResult = EntityUtils.toString(httpResponse.getEntity()); return strResult;
} catch (Exception e) {
ErrorMsg = e.getMessage();
return "";
}
} @Override
protected void onPostExecute(String result) {
super.onPostExecute(result); if( ErrorMsg.isEmpty()== false){
Toast.makeText(context.getApplicationContext(), ErrorMsg,
Toast.LENGTH_SHORT).show();
return;
}
Callback.exec(new Gson().fromJson(result, JsonMsg.class));
}
}; task.execute(Url);
}
}
调用:
NetHelper.Post("http://192.168.1.33/dzx/ajax/city/getCityInfo/1", null, new IAjaxCallback() {
@Override
public void exec(JsonMsg jm) {
TextView txt = (TextView) findViewById(R.id.textView);
txt.setText(jm.GetString("Name"));
}
});
ListInt 助手类定义
当我发现 Java 不能定义 List<int>,我疯了。Java 里居然有 int 和 Integer !这是怎样的一群人造出的Java这个杂种!
定义一个ListInt 助手类吧。
package Base; import java.util.ArrayList; /**
* Created by udi on 2015/7/20.
*/
public class ListInt {
private ArrayList<Object> data = new ArrayList<Object>();
public int getLength()
{
return data.size();
} public int Get(int Index){
return (int) data.get(Index);
} public void Add(int Value){
data.add(Value);
} public void AddRange(ListInt Value){
for(int i=0;i< Value.getLength();i++){
data.add( Value.Get(i)) ;
}
} public void Insert(int Index,int Value){
data.add(Index,Value);
} public void RemoveAt(int Index) {
data.remove(Index);
} public void Clear(){
data.clear();
} public int[] ToArray(){
int[] retVal = new int[data.size()];
for(int i=0;i< data.size();i++){
retVal[i] = (int)data.get(i);
}
return retVal;
}
}
ActionBarActivity基类
主要实现 GetJson,SetJson。
package Base; import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBarActivity;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import cmn.StringDict;
import dzx.cn.dzx.R; /**
* Created by Administrator on 2015/7/14.
*/
public class MyActionBarActivity extends ActionBarActivity { protected Activity context = this; protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
} public void SetJson(String Prefix, HashMap<String, String> Model) {
for (Map.Entry<String, String> entry : Model.entrySet()) {
String key = entry.getKey();
if (Prefix != null && key.startsWith(Prefix) == false) {
continue;
} String value = entry.getValue(); int ResID = GetResID(key);
if (ResID <= 0) continue; View view = findViewById(ResID);
if (view == null) continue; if (view instanceof TextView) {
((TextView) view).setText(value);
}
}
}
public RelativeLayout GetRoot(){
return (RelativeLayout)View.inflate(this, getResources().getIdentifier("activity_main", "layout", getPackageName()) , null);
}
public StringDict GetJson(String Prefix) {
StringDict dict = new StringDict(); ListInt views = GetAllTextView(GetRoot()); for (int id : views.ToArray()) {
String key = getResources().getResourceName(id);
if( key.contains("/")){
String[] each = key.split("/") ;
key = each[each.length -1];
} if( Prefix != null && key.startsWith(Prefix) == false)
{
continue;
} String val =((TextView) findViewById(id)).getText().toString();
dict.put(key,val);
}
return dict;
} public int GetResID(String ResKey) {
return getResources().getIdentifier(ResKey, "id", getPackageName());
} private ListInt GetAllTextView(ViewGroup root) {
if (root == null) return new ListInt();
ListInt list = new ListInt(); for (int len = root.getChildCount() - 1; len >= 0; len--) {
View v = root.getChildAt(len);
if (v instanceof ViewGroup) {
list.AddRange(GetAllTextView((ViewGroup) v));
} else if (v instanceof TextView) {
list.Add(v.getId());
}
} return list;
}
}
对应的C# WinForm方法,更优雅。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; using StringDict = System.Collections.Generic.Dictionary<string, string>; namespace WindowsFormsApplication1
{
public class MyForm : Form
{
public void SetJson(string Prefix, StringDict dict)
{
if (Prefix == null)
{
Prefix = "";
} Func<Control, Control[]> GetControl =null; var _GetControl = new Func<Control, Control[]>(c =>
{
var list = new List<Control>();
if (c.HasChildren)
{
foreach (Control con in c.Controls)
{
list.AddRange(GetControl(con));
}
}
else
{
list.Add(c);
}
return list.ToArray();
});
GetControl = _GetControl; foreach (var c in GetControl(this))
{
dict.All(kv =>
{
if (c.Name == Prefix + kv.Key)
{
c.Text = kv.Value;
return false;
}
return true;
});
}
} public StringDict GetJson(string Prefix)
{
if (Prefix == null)
{
Prefix = "";
}
var dict = new StringDict(); Func<Control, Control[]> GetControl = null; var _GetControl = new Func<Control, Control[]>(c =>
{
var list = new List<Control>();
if (c.HasChildren)
{
foreach (Control con in c.Controls)
{
list.AddRange(GetControl(con));
}
}
else
{
if (c is TextBox)
{
list.Add(c);
}
}
return list.ToArray();
});
GetControl = _GetControl; foreach (var c in GetControl(this))
{
if (c.Name.StartsWith(Prefix))
{
dict[c.Name.Substring(Prefix.Length)] = c.Text;
}
}
return dict;
}
}
}
Android开发笔记的更多相关文章
- Android开发笔记:打包数据库
对于数据比较多的控制一般会加入SQLite数据库进行数据存储,在打包时这些数据库是不自动打包到apk中的,如何创建数据库呢 方法1:将创建数据库的sql语句在SQLiteHelper继承类中实现,在第 ...
- Android开发笔记--hello world 和目录结构
原文:Android开发笔记--hello world 和目录结构 每接触一个新东西 都有一个hello world的例子. 1.新建项目 2.配置AVD AVD 没有要新建个,如果不能创建 运行SD ...
- 【转】Android开发笔记(序)写在前面的目录
原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...
- [APP] Android 开发笔记 003-使用Ant Release 打包与keystore加密说明
接上节 [APP] Android 开发笔记 002 5. 使用ant release 打包 1)制作 密钥文件 release.keystore (*.keystore) keytool -genk ...
- [APP] Android 开发笔记 002-命令行创建默认项目结构说明
接上节:[APP] Android 开发笔记 001 4. 默认项目结构说明: 这里我使用Sublime Text 进行加载.
- Android开发笔记——以Volley图片加载、缓存、请求及展示为例理解Volley架构设计
Volley是由Google开源的.用于Android平台上的网络通信库.Volley通过优化Android的网络请求流程,形成了以Request-RequestQueue-Response为主线的网 ...
- Android开发笔记(一百三十四)协调布局CoordinatorLayout
协调布局CoordinatorLayout Android自5.0之后对UI做了较大的提升.一个重大的改进是推出了MaterialDesign库,而该库的基础即为协调布局CoordinatorLayo ...
- 【转】Android开发笔记——圆角和边框们
原文地址:http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ Android开发笔记——圆角和边框们 在做Androi ...
- 《ArcGIS Runtime SDK for Android开发笔记》
开发笔记之基础教程 ArcGIS Runtime SDK for Android 各版本下载地址 <ArcGIS Runtime SDK for Android开发笔记>——(1).And ...
- 《ArcGIS Runtime SDK for Android开发笔记》——离在线一体化技术:概述
1.前言 数据生产和数据展示是常见的两大专业级移动GIS应用场景,这里我们针对数据生产环节的ArcGIS的离在线一体化技术给大家做一个基本的介绍和梳理. 使用ArcGIS离在线一体化技术首先需要以下基 ...
随机推荐
- JSF2 下 taglib 的问题
在jsf1使用 taglib 定义 标签出现 The absolute uri: http://java.sun.com/jsf/core cannot be resolved in either w ...
- wget net-tools
新安装的centos7 minimal 没有安装 wget 需要安装一下,才能安装lnmp yum -y install wget yum -y install net-tools
- The string "--" is not permitted within comments
ibatis中SAXParseException异常:The string "--" is not permitted within comments 这个异常是说sqlmap里面 ...
- C#拼接地图瓦片
为了在AE程序中使用离线的电子地图,思路如下: 利用下载工具下载地图切片,然后利用C#进行切片拼接成一张图片,最后使用ArcMap进行地理配准,然后发布成ArcGIS Server 切片服务供程序使用 ...
- dojo 加载Json数据
1.今天研究了dojo datagrid加载WebService后台传上来的数据.研究来研究去发现他不是很难.用谷歌多调试一下就好了. 2.看很多例子,这个例子能够更好的帮我解决问题:https:// ...
- (引用 )自动化测试报告HTMLtestrunner
1>下载HTMLTestRunner.py文件,地址为: http://tungwaiyip.info/software/HTMLTestRunner.html Windows平台: 将下载 ...
- MVC学习(四)几种分页的实现(2)
在第一种分页方式中,仅仅实现了分页,但并未有体现出MVC的优势,没有体现出泛型编程思想,尤其在数据量很大的时候,分页十分缓慢,除此之外,还没有实现很好的封装,不是一个通用方法. 因此,我希望只要传入数 ...
- 第六周PSP
[week6]psp 工作周期:10.20-10.27 本周PSP C类型 C内容 S开始时间 ST结束时间 I中断时间 T净时间(分) 活动 开事后诸葛亮会议 13:00 14:00 0 ...
- Java Web的开始学习
今天开始学习Web了,需要的前提技能是 XML ,我还不太熟悉,今天的任务需要熟悉一下 XML-- 输入输出流 I/O 序列化反序列化,也需要看一下,我看这两块会有用到. 任务: ---- ...
- shell脚本批量处理字符串
上周五运营那边给了一份手机号码的excle,要求查询出所有对应于用户编号的用户的信息.这个时候遇到了一个问题就是,需要查询的用户数量很多,不可能一个一个去查,而excle中的格式又不符合sqlquer ...