《Android编程代码速查.docx》由会员分享,可在线阅读,更多相关《Android编程代码速查.docx(24页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、0 android 创建按钮Button button = new Button(this);1 android 创建输入框EditText editText = new EditText(this);2 android 创建文本TextView textView = new TextView(this);3 android 设置文本显示内容TextView textView = new TextView(this);textView.setText(hello world!);4 android 设置文本背景色TextView textView = new TextView(this);te
2、xtView.setBackgroundColor(Color.YELLOW);5 android 设置文本颜色TextView textView = new TextView(this);textView.setTextColor(Color.YELLOW);6 android 设置文本文字大小TextView textView = new TextView(this);textView.setTextSize(18);7 android 设置输入框宽度EditText editText = new EditText(this);editText.setWidth(200);8 androi
3、d 设置输入框为密码框EditText editText = new EditText(this);editText.setTransformationMethod(PasswordTransformationMethod.getInstance();9 android 设置输入框为密码框(xml配置)android:password=true10 android 提示对话框的使用AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle(你好);builder.setPositiveButton(O
4、K,this);builder.show()需实现android.content.DialogInterface.OnClickListener接口11 android ListView的使用ListView listView = new ListView(this);ArrayListHashMap list = new ArrayListHashMap();SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.list,new String标题,new intR.id.TextView01);listView.setAda
5、pter(adapter);listView.setOnItemClickListener(this);然后实现OnItemClickListener接口public void onItemClick(AdapterView parent, View view, int position, long id) 12 android 更新ListViewListView listView = new ListView(this);ArrayListHashMap list = new ArrayListHashMap();SimpleAdapter adapter = new SimpleAdap
6、ter(this,list,R.layout.list,new String标题,new intR.id.TextView01);listView.setAdapter(adapter);adapter.notifyDataSetChanged();/通知更新ListView13 android 创建LinearLayoutLinearLayout layoutParant = new LinearLayout(this);14 android 时间设置对话框的使用DatePickerDialog dlg = new DatePickerDialog(this,this,year,month,
7、day);dlg.show();/*year month day 均为int型,第二个参数为this时,该类需要implements OnDateSetListener并重写public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) */15 android 创建FrameLayoutFrameLayout layout = new FrameLayout(this);16 android 触发键盘事件layout.setOnKeyListener(this);/需要implements On
8、KeyListener并重写以下方法public boolean onKey(View v, int keyCode, KeyEvent event) return false;/返回是否销毁该事件以接收新的事件,比如返回true按下时可以不断执行这个方法,返回false则执行一次。17 android 触发鼠标事件layout.OnTouchListener(this);/需要implements OnTouchListener并重写以下方法public boolean onTouch(View v, MotionEvent event) return false;/返回是否销毁该事件以接收
9、新的事件,比如返回true按下时可以不断执行这个方法,返回false则执行一次。18 android 获得屏幕宽度和高度int width = this.getWindow().getWindowManager().getDefaultDisplay().getWidth();int height =this.getWindow().getWindowManager().getDefaultDisplay().getHeight();19 android 布局添加控件LinearLayout layout = new LinearLayout(this);Button button = new
10、 Button(this);layout.addView(button);20 android intent实现activit之间跳转Intent intent = new Intent();intent.setClass(this, DestActivity.class);startActivity(intent);21 android intent设置actionIntent intent = new Intent();intent.setAction(intent.ACTION_DIAL);22 android intent设置dataIntent intent = new Intent
11、();intent.setData(Uri.parse(tel:00000000);23 android intent传数据Intent intent = new Intent();intent.putExtra(data, value);/value可以是很多种类型,在接收activity中取出后强制转换或调用相应类型的get函数。24 android intent取数据String value = (String)getIntent().getExtras().get(data);/orString value = getIntent().getExtras().getString(dat
12、a);25 android 利用paint和canvas画图setContentView(new MyView(this);class MyView extends Viewpublic MyView(Context context)super(context);public void onDraw(Canvas canvas)Paint paint = new Paint();/创建画笔paint.setColor(Color.BLUE);/设置画笔颜色canvas.drawRect(0, 0, 100, 100, paint);/画个正方形,坐标0,0,100,100。26 android
13、 新建对话框Dialog dialog = new Dialog(this);dialog.setTitle(test);/设置标题dialog.addContentView(button,new LayoutParams(-1,-1);/添加控件,-1是设置高度和宽度充满布局,-2是按照需要设置宽度高度。dialog.show();27 android 取消对话框dialog.cancel();28对View类刷新显示view.invalidate();/通过这个调用view的onDraw()函数28 android 对View类刷新显示view.invalidate();/通过这个调用vi
14、ew的onDraw()函数29 android 使用SurfaceView画图setContentView(new MySurfaceView(this);class MySurfaceView extends SurfaceView implements SurfaceHolder.CallbackSurfaceHolder holder;public MySurfaceView(Context context) super(context);holder = getHolder();holder.addCallback(this);class MyThread extends Thread
15、public void run()Canvas canvas = holder.lockCanvas();Paint paint = new Paint();paint.setColor(Color.YELLOW);canvas.drawRect(100, 100, 200, 200, paint);holder.unlockCanvasAndPost(canvas);public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) public void surfaceCreated(Surf
16、aceHolder holder) new MyThread().start();public void surfaceDestroyed(SurfaceHolder holder) 30 android 获得控件findViewByIdTextView textView = (TextView) findViewById(R.id.TextView01);31 android 十六进制设置画笔颜色Paint paint = new Paint();paint.setColor(0xffffffff);/第一个ff是透明度的设置。32 android 获得String.xml中配置的字符串/在
17、activity中直接调用getText(R.string.app_name);33 android 去掉应用程序头部requestWindowFeature(Window.FEATURE_NO_TITLE);34 android 使用SharedPreferences写入数据代码getSharedPreferences(data, 0).edit().putString(aa,bb).commit();35 android 使用SharedPreferences读取数据代码String data = getSharedPreferences(data, 0).getString(item,)
18、;/后面的是默认值,没有取到则赋值为,如果不想有默认,可以设置null。36 android 继承SQLiteOpenHelperclass MyHelper extends SQLiteOpenHelperpublic MyHelper(Context context, String name, CursorFactory factory,int version) super(context, name, factory, version);public void onCreate(SQLiteDatabase db) db.execSQL(CREATE TABLE IF NOT EXIST
19、S testtable ( +cardno integer primary key, +username varchar, + money integer+);public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)db.execSQL(DROP TABLE IF EXISTS testtable);onCreate(db);37 android 利用SQLiteOpenHelper打开数据库MyHelper dbHelper = new MyHelper(this, testtable.db, null,
20、 1);SQLiteDatabase db = dbHelper.getReadableDatabase();/打开只读/或者SQLiteDatabase db = dbHelper.getWritableDatabase();/打开可写38 android 查询数据表并显示结果Cursor cursor = db.query(testtable, null, null, null, null, null, null);/db的获得请参见“利用SQLiteOpenHelper打开数据库”while(!cursor.isAfterLast()Log.i(test,cursor.getString
21、(0);cursor.moveToNext();39 android Logcat输出打印测试信息Log.i(TAG,TEST);40 android 数据表插入数据ContentValues values = new ContentValues();values.put(username,admin);values.put(money,10000);db.insert(testtable, null, values);41 android 使得应用全屏getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN,Window
22、Manager.LayoutParams. FLAG_FULLSCREEN); 42 android 设置LinearLayout方向为竖layoutParant.setOrientation(LinearLayout.VERTICAL);43 android 设置LinearLayout方向为横layoutParant.setOrientation(LinearLayout.HORIZONTAL);44 android 数据库更新数据ContentValues values = new ContentValues();values.put(username,admin);values.put
23、(money,10000);db.update(testtable,values,userno=1,null);45 android 数据库删除数据db.delete(testtable,userno=1,null);46 android 判断sd卡是否存在if (android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED)Log.i(test,SDCARD exists);else Log.i(test,SDCARD doesnt exist);47 android
24、 创建ImageViewImageView view = new ImageView(this); view.setImageResource(R.drawable.icon);48 android 提示信息Toast toast = Toast.makeText(this, hello, Toast.LENGTH_LONG); toast.show();49 android 创建单选框以及单选组RadioButton radioButton = new RadioButton(this);RadioButton radioButton2 = new RadioButton(this);rad
25、ioButton.setText(yes);radioButton2.setText(no);RadioGroup radioGroup = new RadioGroup(this);radioGroup.addView(radioButton);radioGroup.addView(radioButton2);50 android 新建播放器MediaPlayer MediaPlayer = new MediaPlayer();51 android 媒体播放器使用/创建MediaPlayerMediaPlayer player = new MediaPlayer();/重置MediaPlay
26、erplayer.reset();try /设置要播放的文件的路径player.setDataSource(/sdcard/1.mp3);/准备播放player.prepare();catch (Exception e) /开始播放player.start();/设置播放完毕事件player.setOnCompletionListener(new OnCompletionListener()public void onCompletion(MediaPlayer player) /播完一首循环try /再次准备播放player.prepare();catch (Exception e) pla
27、yer.start(););52 android 媒体播放器暂停player.pause();53 android 清空cookiesCookieManager.getInstance().removeAllCookie();54 android 文本设置粗体TextView textView = new TextView(this);TextPaint textPaint = textView.getPaint();textPaint.setFakeBoldText(true);55 android 网络权限配置56 android GL设定背景色gl.glClearColor(0.5f,
28、0.2f, 0.2f, 1.0f);gl.glClear(GL10.GL_COLOR_BUFFER_BIT);57 android 创建GL画布public class My3DView extends GLSurfaceView private GLSurfaceView.Renderer renderer;public My3DView(Context context) super(context);renderer = new My3DRender();setRenderer(renderer);58 android 创建复选框CheckBox checkBox = new CheckB
29、ox(this);59 android 复选框监听选择/取消事件checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) Log.i(QSR,TEST););60 android 创建菜单/重写下面这个函数public boolean onCreateOptionsMenu(Menu menu) super.onCreateOptionsMenu(menu); menu.a
30、dd(0, 1, 1, test1); menu.add(0, 2, 2, test2); menu.add(0, 3, 3, test3); menu.add(0, 4, 4, test4); return true; 61 android 处理菜单选择事件public boolean onOptionsItemSelected(MenuItem item)int id = item.getItemId();switch (id) case 1:Log.i(QSR,1);break;case 2:Log.i(QSR,2);break;case 3:Log.i(QSR,3);break;cas
31、e 4:Log.i(QSR,4);break;default:break;return super.onOptionsItemSelected(item); 62 android 允许程序访问GPS(XML配置)63 android 允许程序访问GSM网络信息(XML配置)64 android 允许程序访问WIFI网络信息(XML配置)65 android 允许程序更新电池状态(XML配置)66 android 允许程序写短信(XML配置)67 android 允许程序设置壁纸(XML配置)68 android 允许程序使用蓝牙(XML配置)69 android 允许程序打电话(XML配置)7
32、0 android 允许程序使用照相设备(XML配置)71 android 允许程序改变网络状态(XML配置)72 android 允许程序改变WIFI状态(XML配置)73 android 允许程序删除缓存文件(XML配置)74 android 允许程序删除包(XML配置)75 android 允许程序禁用键盘锁(XML配置)76 android 允许程序获取任务信息(XML配置)77 android 允许程序截获鼠标或键盘等事件(XML配置)78 android 允许程序使用socket(XML配置)79 android 允许程序读取日历(XML配置)80 android 允许程序读取系统
33、日志(XML配置)81 android 允许程序读取所有者数据(XML配置)82 android 允许程序读取短信(XML配置)83 android 允许程序重启设备(XML配置)84 android 允许程序录制音频(XML配置)85 android 允许程序发送短信(XML配置)86 android 允许程序将自己置为最前(XML配置)87 android 创建图像图片BitmapResources res = getResources();Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.hh);88 android
34、 取得远程图片HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();conn.connect();InputStream is = conn.getInputStream();bitmap = BitmapFactory.decodeStream(is);is.close();89 android 允许程序发送短信(XML配置)90 android 启动和结束服务startService(new Intent(qsr.test.MyService);stopService(new Intent(qsr.te
35、st.MyService);91 android 创建和配置Servicepublic class MyService extends Service public IBinder onBind(Intent arg0) return null;public void onStart(Intent intent,int startId)super.onStart(intent, startId);/to do something when startpublic void onDestroy()super.onDestroy();/to do something when stop/xml配置
36、92 android 获得系统感应设备SensorManager sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); 93 android 设置控件布局参数textView01.setLayoutParams(new AbsoluteLayout.LayoutParams(100,60,0,0);/高100,宽60,x=0,y=0;);94 android 创建Drawable对象Resources res = getResources();Drawable drawable = res.getDra
37、wable(R.drawable.hh);95 android 访问网页Uri uri = Uri.parse();Intent intent = new Intent(Intent.ACTION_VIEW,uri);startActivity(intent);96 android 打电话Uri uri = Uri.parse(tel:00000000);Intent intent = new Intent(Intent.ACTION_DIAL, uri); startActivity(intent); 97 android 播放歌曲Intent intent = new Intent(Int
38、ent.ACTION_VIEW);Uri uri = Uri.parse(file:/sdcard/test.mp3);intent.setDataAndType(uri, audio/mp3);startActivity(intent);98 android 发送邮件Intent intent=new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_TEXT, The email text); intent.setType(text/plain); startActivity(Intent.createChooser(intent, Choose Email Client);99 android 发短信Uri uri = Uri.parse(smsto:123456789); Intent intent = new Intent(Intent.ACTION_SENDTO,