編輯:關於Android編程
AsyncTask
class MyAsyncTask extends AsyncTask{ //執行後台任務之前 @Override protected void onPreExecute() { super.onPreExecute(); progresssBar.setVisibility(View.VISIBLE); } //執行後台任務之後,在這裡設置UI,更新數據 @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); progressBar.setVisibility(View.GONE); mImageView.setImageBitmap(bitmap); } //在後台執行的任務(耗時操作,網絡加載數據) @Override protected Bitmap doInBackground(String... params) { String url=params[0]; Bitmap bitmap=null; URLConnection conn; InputStream is; try { conn=new URL(url).openConnection(); is=conn.getInputStream(); BufferedInputStream bis=new BufferedInputStream(is); bitmap=BitmapFactory.decodeStream(bis); is.close();bis.close(); } catch (Exception e) { e.printStackTrace(); } return bitmap; } }
在主活動中使用異步加載:
MyAsyncTask task=new MyAsyncTask(); task.execute(URL);//傳進去一個參數url
Looper
內部包含一個消息隊列也就是MessageQueue,所有的Handler發送消息都走向這個消息隊列。 Looper.Looper方法,就是一個死循環,不斷地從MessageQueue中取消息,如有有消息就處理消息,沒有消息就阻塞,直到有消息傳來。Toast.makeText(context,text,duration);//返回值為Toast toast.setDuration(duratin);//設置持續時間 toast.setGravity(gravity,xOffset,yOffset);//設置toast位置,xOffset、yOffset是偏移量。 toast.setText(ts);//設置提示內容 toast.show();//顯示,不調用就不會顯示
private void showToast(){
Toast toast=Toast.makeText(this,"改變位置的Toast",Toast.LENGTH);
toast.setGravity(Gravity.CENTER,0,0);//相對中間偏移量為0,
toast.show();
}
private void showToast(){
Toast toast=Toast.makeText(this,"顯示帶圖片的Toast",Toast.LENGTH_LONG);
LinearLayout toastLayout=(LinearLayout) toast.getView();
ImageView img=new ImageView(this);
img.setImageResource(R.drawable.ic_launcher);
toastLayout.addView(img,0);
toast.show();
}
使用:
public void showToast(){
View view1=View.inflate(this, R.layout.item, null);
Toast toast=new Toast(this);
toast.setView(view1);
toast.show();
}
private void sendNotification(){
int id=100;//用於區分多個通知時
NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent=new Intent(this,MainActivity.class);
PendingIntent pintent=PendingIntent.getActivity(this, 0, intent, 0);
Builder builder=new Notification.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setTicker("hello");//手機狀態欄的提示
builder.setWhen(System.currentTimeMillis());//設置時間
builder.setContentTitle("通知欄通知");//設置標題
builder.setContentText("wo來自廣藥火星");//設置通知欄通知內容
builder.setContentIntent(pintent);//設置點擊後的意圖
//
// builder.setDefaults(Notification.DEFAULT_SOUND);
// builder.setDefaults(Notification.DEFAULT_LIGHTS);
// builder.setDefaults(Notification.DEFAULT_VIBRATE);
//
builder.setDefaults(Notification.DEFAULT_ALL);//包括上面三種
Notification notification=builder.build();//4.1以上才生效
manager.notify(id, notification);
//取消指定id的通知
//manager.cancel(id);
}
創建選項菜單:onCreateOptionsMenu();
設置菜單項可用代碼動態設置menuAdd();
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
//getMenuInflater().inflate(R.menu.main, menu);
menu.add(1, 100, 1, “賦值”);
menu.add(1, 101, 1, “刪除”);
menu.add(1, 102, 1, “粘貼”);
menu.add(1, 103, 1, “設置”);
return true;
}
還可以通過xml設置MenuInflaterinfalte()
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
設置菜單項點擊事件:onOptionsItemSelected()
第一種方法點擊事件
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item)
{
switch(item.getItemId()){
case R.id.action_settings:
Toast.makeText(this, "你點擊了設置", 0).show();
break;
}
return true;
}
第二種方法點擊事件
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item)
{
switch(item.getItemId()){
case 100:
Toast.makeText(this, "你點擊了賦值", 0);
break;
case 101:
Toast.makeText(this, "你點擊了刪除", 0);
break;
case 102:
Toast.makeText(this, "你點擊了粘貼", 0);
break;
case 103:
Toast.makeText(this, "你點擊了設置", 0);
break;
}
return true;
}
菜單跳轉到另一頁面:
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item)
{
switch(item.getItemId()){
case R.id.settings:
Toast.makeText(this, "跳轉頁面", 0).show();
Intent intent=new Intent(this,SecondActivity.class);
item.setIntent(intent);
startActivity(intent);
break;
}
return true;
}
標題以及標題圖標 菜單內容 菜單內容的點擊事件
OptionMenu對應的是activity,一個activity只能擁有一個選項菜單 ContextMenu對應的是View,每個View都可以設置上下文菜單; 一般情況下,ContextMenu常用於ListView或者Gridview
首先給View注冊上下文菜單 registerForContextMenu()
this.registerForContextMenu(listView);
添加上下文菜單內容onCreateContextMenu();
-可以通過代碼動態添加
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("文件操作");
menu.setHeaderIcon(R.drawable.ic_launcher);
menu.add(1, 103, 1, "設置");
menu.add(1, 102, 1, "粘貼");
menu.add(1, 100, 1, "賦值");
menu.add(1, 101, 1, "刪除");
}
-可以加載xml文件的菜單項
設置菜單點擊後的響應事件onContextItemSelect();
改變樣式為無標題模式,可以使子菜單懸浮於屏幕上
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
//加載xml文件添加
// getMenuInflater().inflate(R.menu.main, menu);
//動態添加
SubMenu file=menu.addSubMenu("文件");
SubMenu edit=menu.addSubMenu("編輯");
file.add(1, 100, 1, "新建");
file.add(1, 101, 1, "打開");
file.add(1, 102, 1, "保存");
file.setHeaderTitle("文件操作");
file.setHeaderIcon(R.drawable.ic_launcher);
edit.add(1, 100, 1, "復制");
edit.add(1, 101, 1, "刪除");
edit.add(1, 102, 1, "粘貼");
edit.add(1, 103, 1, "設置");
return true;
}
點擊事件實現:
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item)
{
if(item.getGroupId()==1){
switch(item.getItemId()){
case 100:
Toast.makeText(this, "新建", 0).show();
break;
case 101:
Toast.makeText(this, "打開", 0).show();
break;
case 102:
Toast.makeText(this, "保存", 0).show();
break;
}
}else if(item.getGroupId()==2){
switch(item.getItemId()){
case 100:
Toast.makeText(this, "復制", 0).show();
break;
case 101:
Toast.makeText(this, "刪除", 0).show();
break;
case 102:
Toast.makeText(this, "粘貼", 0).show();
break;
}
}
return true;
}
Android自定義SurfaceView與傳感器的並用(實現自繪的指北針)
概述:SurfaceView是Android中極為重要的繪圖容器,SurfaceView的圖像繪制是放在主線程之外的另一個線程中完成的。除了繪圖,SurfaceView還
自定義圓形ImageView 實現思路 -- Android 學習之路
自定義圓形ImageView圓形ImageView在頭像顯示用的比較普遍了,今天對於實現圓形ImageView做個總結;主要思路是 重寫 onDraw() ;方法有兩個:
Android實現文字和圖片混排(文字環繞圖片)效果
本文實例講述了Android實現文字和圖片混排(文字環繞圖片)效果。分享給大家供大家參考,具體如下:在平時我們做項目中,或許有要對一張圖片或者某一個東西進行文字和圖片說明
Android actionBar與Fragment結合使用Demo
本文介紹ActionBar與Fragment結合使用的一個實例,ActionBar是一個標識應用程序和用戶位置的窗口功能,並且給用戶提供操作和導航模式。 Actio