




已阅读5页,还剩14页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
身为新手学习安卓往往会特别的迷茫,不知道该学习什么现在提供一些Android在学习的过程当中经常用到的一些语句,以方便大家学习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);textView.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 android设置输入框为密码框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(OK,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.setAdapter(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 SimpleAdapter(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,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 OnKeyListener并重写以下方法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;/返回是否销毁该事件以接收新的事件,比如返回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 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();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);/or String value = getIntent().getExtras().getString(data);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新建对话框Dialog dialog = new Dialog(this);dialog.setTitle(test);/设置标题dialog.addContentView(button,new LayoutParams(-1,-1);/添加控件,-1是设置高度和宽度充满布局,-2是按照需要设置宽度高度。dialog.show();27 android取消对话框dialog.cancel();28 android对View类刷新显示view.invalidate();/通过这个调用view的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 Threadpublic 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(SurfaceHolder 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中配置的字符串,在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,);/后面的是默认值,没有取到则赋值为,如果不想有默认,可以设置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 EXISTS 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, 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(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,WindowManager.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(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);elseLog.i(test,SDCARD doesnt exist);47 android创建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);radioButton.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();/重置MediaPlayerplayer.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) player.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, 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 CheckBox(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.add(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;case 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配置) 70 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允许程序读取系统日志(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取得远程图片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.test.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配置92 android获得系统感应设备SensorManager sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); 93 android设置控件布局参数/高100,宽60,x=0,y=0;textView01.setLayoutParams(new AbsoluteLayout.LayoutParams(100,60,0,0);94 android创建Drawable对象Resources res = getResources();Drawable drawable = res.getDrawable(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(Intent.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, uri); intent.putExtra(sms_body, The SMS text); startActivity(intent);100 android安装程序Uri installUri = Uri.fromParts(package, xxx, null);Intent intent = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri); startActivity(intent);101 android卸载程序Uri uninstallUri = Uri.fromParts(package, xxx, null);Intent intent = new Intent(Intent.ACTION_DELETE, uninstallUri); startActivity(intent);102 android从xml配置获得控件对象/TestActivity.javatextView = (TextView) findViewById(R.id.TextView01);/main.xml103 android获得触摸屏压力public boolean onTouch(View v, MotionEvent event) float pressure = event.getPressure();return false;104 android给文本加上滚动条TextView textView = new TextView(this);textView.setText(string);ScrollView scrollView = new ScrollView(this);scrollView.addView(textView);setContentView(scrollView);105 android获得正在运行的所有服务public void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState);StringBuffer serviceInfo = new StringBuffer();ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);List services = activityManager.getRunningServices(256);Iterator iterator = services.iterator();while (iterator.hasNext()RunningServiceInfo si = (RunningServiceInfo)iterator.next();serviceInfo.append(pid: ).append(si.pid);serviceInfo.append(process: ).append(cess);TextView textView = new TextView(this);textView.setText(serviceInfo.toString();ScrollView scrollView = new ScrollView(this);scrollView.addView(textView);setContentView(scrollView);106 android使用ContentResolver获得联系人姓名和号码ContentResolver cr = getContentResolver();Cursor cur = cr.query(People.CONTENT_URI, null, null, null, null);cur.moveToFirst();doint nameColumn = cur.getColumnIndex(People.NAME);int phoneColumn = cur.getColumnIndex(People.NUMBER);String name = cur.getString(nameColumn);String phoneNumber = cur.getString(phoneColumn);Toast.makeText(this,name,Toast.LENGTH_LONG).show();Toast.makeText(this,phoneNumber,Toast.LENGTH_LONG).show(); while (cur.moveToNext();107 android创建WebViewWebView webView = new WebView(this);webView.loadData(+test+test+, text/html, utf-8);108 android设置地图是否显示卫星和街道mapView.setSatellite(false); mapView.setStreetView(true);109 android单选框清除radioGroup.clearCheck();110 android给文本增加链接Linkify.addLinks(mTextView01,Linkify.WEB_URLS);Linkify.addLinks(mTextView02,Linkify.EMAIL_ADDRESSES);Linkify.addLinks(mTextView03,Linkify.PHONE_NUMBERS);111 android设置手机震动Vibrator vibrator = (Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);vibrator.vibrate(new long1000,1000,1000,1000,-1);/震动一秒,停一秒,再震一秒112 android创建下拉框Spinner spinner = new Spinner(this);113 android给spinner添加事件spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() public void onItemSelected(AdapterView parent,View view,int position,long id) public void onNo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 广西柳州市融水中学2026届化学高一上期中考试试题含解析
- 江西省崇仁县第二中学2026届高三化学第一学期期中学业质量监测试题含解析
- 山东省微山县第一中学2026届高二化学第一学期期末经典模拟试题含答案
- 2026届河北邯郸化学高三上期末调研模拟试题含解析
- 2026届山东省平邑县曾子学校高一化学第一学期期中学业水平测试试题含解析
- 2026届四川省遂宁市射洪县化学高二上期中复习检测模拟试题含解析
- 2026届江西省南昌市化学高二上期中教学质量检测模拟试题含解析
- 物流企业仓储管理与信息系统应用
- 幼儿园饮食观察与培养习惯记录
- 学校学生管理规章更新方案
- 电信研发工程师L1认证培训考试复习题库(含答案)
- 空气源热泵施工组织设计
- 非战争军事行动中的后勤保障工作
- 金蝶K3供应链操作手册
- 高泌乳素症患者的护理
- 中国慢性阻塞性肺疾病基层诊疗指南(2024年)解读
- 电缆中间接头防火整改方案
- 2025届新高考数学一二轮复习备考建议与做法 课件
- 合作试验协议
- 全国高中生物奥林匹克竞赛试题
- 配电房安全管理培训
评论
0/150
提交评论