Android开发中15条小经验

Android开发中15条小经验

1. TextView中的getTextSize返回值是以像素(px)为单位的,而setTextSize()是以sp为单位的.所以如果直接用返回的值来设置会出错,解决办法是用setTextSize()的另外一种形式,可以指定单位:setTextSize(intunit,intsize) TypedValue.COMPLEX_UNIT_PX:Pixels TypedValue.COMPLEX_UNIT_SP:ScaledPixels TypedValue.COMPLEX_UNIT_DIP:DeviceIndependentPixels

2. 在继承自View时,绘制bitmap时,需要将图片放到新建的drawable-xdpi中,否则容易出现绘制大小发生改变3. 在文字中加下划线: textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);4. scrollView是继承自frameLayout,所以在使用LayoutParams时需要用frameLayout的5.在Android中几种网络编程的方式:

(1)针对TCP/IP的Socket、ServerSocket

(2)针对UDP的DatagramSocket、DatagramPackage。这里需要注意的是,考虑到Android设备通常是手持终端,IP都是随着上网进行分配的。不是固定的。因此开发也是有 一点与普通互联网应用有所差异的。

(3)针对直接URL的HttpURLConnection

(4)Google集成了Apache HTTP客户端,可使用HTTP进行网络编程。针对HTTP,Google集成了Appache Http core和httpclient 4版本,因此特别注意Android不支持 httpclient 3.x系列,而且目前并不支持Multipart(MIME),需要自行添加httpmime.jar

(5)使用Web Service。Android可以通过开源包如Jackson去支持Xmlrpc和Jsonrpc,另外也可以用Ksoap2去实现Webservice

(6) 直接使用WebView视图组件显示网页。基于WebView 进行开发,,Google已经提供了一个基于chrome-lite的Web浏览器,直接就可以进行上网浏览网页。6. TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)这个是我们最常用的一个构造方法,float fromXDelta:这个参数表示动画开始的点离当前View X坐标上的差值;float toXDelta:这个参数表示动画结束的点离当前View X坐标上的差值;float fromYDelta:这个参数表示动画开始的点离当前View Y坐标上的差值;float toYDelta:这个参数表示动画开始的点离当前View Y坐标上的差值;如果view在A(x,y)点 那么动画就是从B点(x+fromXDelta, y+fromYDelta)点移动到C 点 (x+toXDelta,y+toYDelta)点.7.android提供了几种在其他线程中访问UI线程的方法。

Activity.runOnUiThread( Runnable )

View.post( Runnable )

View.postDelayed( Runnable, long )

从网上获取一个网页,在一个TextView中将其源代码显示出来packageorg.unique.async; importjava.io.ByteArrayOutputStream; importjava.io.InputStream; importjava.util.ArrayList; importorg.apache.http.HttpEntity; importorg.apache.http.HttpResponse; importorg.apache.http.client.HttpClient; importorg.apache.http.client.methods.HttpGet; importorg.apache.http.impl.client.DefaultHttpClient; importandroid.app.Activity; importandroid.app.ProgressDialog; importandroid.content.Context; importandroid.content.DialogInterface; importandroid.os.AsyncTask; importandroid.os.Bundle; importandroid.os.Handler; importandroid.os.Message; importandroid.view.View; importandroid.widget.Button; importandroid.widget.EditText; importandroid.widget.TextView; publicclassNetworkActivityextendsActivity{ privateTextViewmessage; privateButtonopen; privateEditTexturl;@Override publicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.network);message=(TextView)findViewById(R.id.message);url=(EditText)findViewById(R.id.url);open=(Button)findViewById(R.id.open);open.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewarg0){connect();}});}privatevoidconnect(){PageTasktask=newPageTask(this);task.execute(url.getText().toString()); }classPageTaskextendsAsyncTask<String,Integer,String>{//可变长的输入参数,与AsyncTask.exucute()对应ProgressDialogpdialog;publicPageTask(Contextcontext){pdialog=newProgressDialog(context,0);pdialog.setButton("cancel",newDialogInterface.OnClickListener(){publicvoidonClick(DialogInterfacedialog,inti){dialog.cancel();}});pdialog.setOnCancelListener(newDialogInterface.OnCancelListener(){publicvoidonCancel(DialogInterfacedialog){finish();}});pdialog.setCancelable(true);pdialog.setMax(100);pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);pdialog.show();}@OverrideprotectedStringdoInBackground(String…params){try{HttpClientclient=newDefaultHttpClient();//params[0]代表连接的urlHttpGetget=newHttpGet(params[0]);HttpResponseresponse=client.execute(get);HttpEntityentity=response.getEntity();longlength=entity.getContentLength();InputStreamis=entity.getContent();Strings=null;if(is!=null){ByteArrayOutputStreambaos=newByteArrayOutputStream();byte[]buf=newbyte[128];intch=-1;intcount=0;while((ch=is.read(buf))!=-1){baos.write(buf,0,ch);count+=ch;if(length>0){//如果知道响应的长度,调用publishProgress()更新进度publishProgress((int)((count/(float)length)*100));}//让线程休眠100msThread.sleep(100);}s=newString(baos.toByteArray());}//返回结果returns;}catch(Exceptione){e.printStackTrace();}returnnull;}@OverrideprotectedvoidonCancelled(){super.onCancelled();}@OverrideprotectedvoidonPostExecute(Stringresult){//返回HTML页面的内容message.setText(result);pdialog.dismiss();}@OverrideprotectedvoidonPreExecute(){//任务启动,可以在这里显示一个对话框,这里简单处理message.setText(R.string.task_started);}@OverrideprotectedvoidonProgressUpdate(Integer…values){//更新进度System.out.println(""+values[0]);message.setText(""+values[0]);pdialog.setProgress(values[0]);}} }8.Spinner不能用在dialog和tabhost中的解决办法9. Unable to open sync connection!把设置里的USB调试重启10.EditText设置光标位置问题EditText中有一些预置文本的时候,想把光标调到最前面,一开始是使用的setSelection(0),结果发现在三星P1000上面有问题。经过研究发现需要先调用EditText.requestFocus(),再调用setSelection(0)。否则的话,在2.x的机器上有问题,但3.x上面是好着的。11.Android中Home键被系统保留,无法象监听回退键一样用onKeyDown,但是可以根据按下home键时会触发的activity和view的一些事件来添加自己的处理代码.网上有人说可以用onAttachWindow来拦截Home键,遇到可以试试。12.在用surfaceView渲染时,如果要想在需要时其中出现其他View,可以将surfaceView和其他View放在layout中,平常时可以将其他view隐藏勇士面前无险路。

Android开发中15条小经验

相关文章:

你感兴趣的文章:

标签云: