http://wear.techbrood.com/
Android SDK Manager 代理设置:
使用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; } }
主要实现 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; } } }
作者:NewSea 如无特别标记说明,均为NewSea原创,版权私有,翻载必纠。欢迎交流,转载,但要在页面明显位置给出原文连接。谢谢。 |