---恢复内容开始---
动态添加应用 具有长按删除功能 功能如下图:
(图片显示功能不是你要的,那就默默关闭页面吧)
设计思路:
页面一:用girdview网格显示图标,在自定义的adapter中为每一个子Item添加内容并且加上点击事件(长按和短按),“+”button跳转到第二个页面。
页面二:用AutoCompeleteTextView带提示的输入框,用adapter为其添加提示字符串组,确认显示所搜索到的应用,这里显示搜索到的应用我不是用ListView,而是在代码中动态添加布局,每一次按“搜索”前先清空里面的子布局。按”添加“后判断该应用是否已经添加,没有的话就在页面一加上,并且返回页面一。
注意:数据我是用常量类Constant,没有介入SQL,只为测试方便。判断当前是否是长按状态下我用Constant.press进行标记。
下面贴上代码童鞋们一起玩玩。
1 package com.example.practice1; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.Timer; 6 7 import android.app.Activity; 8 import android.app.ActionBar; 9 import android.app.Fragment; 10 import android.content.Context; 11 import android.content.Intent; 12 import android.os.Bundle; 13 import android.os.SystemClock; 14 import android.util.Log; 15 import android.view.LayoutInflater; 16 import android.view.Menu; 17 import android.view.MenuItem; 18 import android.view.MotionEvent; 19 import android.view.View; 20 import android.view.View.OnClickListener; 21 import android.view.View.OnLongClickListener; 22 import android.view.View.OnTouchListener; 23 import android.view.ViewGroup; 24 import android.widget.AdapterView; 25 import android.widget.AdapterView.OnItemClickListener; 26 import android.widget.BaseAdapter; 27 import android.widget.Chronometer; 28 import android.widget.GridView; 29 import android.widget.ImageView; 30 import android.widget.SimpleAdapter; 31 import android.widget.TextView; 32 import android.widget.Toast; 33 import android.os.Build; 34 35 public class MainActivity extends Activity { 36 37 Grid_Adapter adp; 38 GridView gridview; 39 long startMili=0; 40 long endMili=0; 41 @Override 42 protected void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 setContentView(R.layout.main); 45 gridview = (GridView) findViewById(R.id.gridview); 46 adp=new Grid_Adapter(this,Constant.num); 47 //添加并且显示 48 gridview.setAdapter(adp); 49 50 } 51 52 53 @Override 54 public void onBackPressed() { //长按后 不想删除 按返回键的触发事件 55 if(!Constant.press) 56 finish(); 57 else { 58 String st; 59 60 for(int k=12;k1000){ //根据时间差来判断是短按还是长按,这里标准时一秒 99 Log.v("long","long_out");appLongClick(position);}100 else {101 Log.v("short","short_out");appShortClick(position);}102 } 103 104 105 106 if(event.getAction() == MotionEvent.ACTION_DOWN){ 107 startMili=System.currentTimeMillis();// 当前时间对应的毫秒数108 Log.v("q","按下"); 109 } 110 return true;111 }112 113 }114 115 private void appShortClick(int p){ //短按处理116 int position=p;117 Log.v("short","short_in");118 if(Constant.press){119 Log.v("ee","eee");120 for(int k=position;k 11&&position<=Constant.num-1){202 203 view.setOnTouchListener(new appClick(position)); 204 205 }206 207 208 209 return view;210 211 }}212 }213 214
MainActivity的XML布局文件就一个GiewView,这里就不贴了。
1 package com.example.practice1; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.text.Editable; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.widget.ArrayAdapter; 10 import android.widget.AutoCompleteTextView; 11 import android.widget.Button; 12 import android.widget.ImageView; 13 import android.widget.LinearLayout; 14 import android.widget.TextView; 15 import android.widget.Toast; 16 17 public class AddApplication extends Activity{ 18 AutoCompleteTextView autotext; 19 Button bu_se; 20 LinearLayout app_add; 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 // TODO Auto-generated method stub 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.addapplication); 26 autotext=(AutoCompleteTextView)findViewById(R.id.autotext); 27 ArrayAdapteradapter=new ArrayAdapter ( 28 this,android.R.layout.simple_dropdown_item_1line,Constant.application); 29 autotext.setAdapter(adapter); 30 31 bu_se=(Button)findViewById(R.id.bu_search); 32 bu_se.setOnClickListener(new View.OnClickListener() { //搜索按钮的触发事件 33 34 @Override 35 public void onClick(View arg0) { 36 // TODO Auto-generated method stub 37 Editable st; 38 st=autotext.getText(); 39 String str=st.toString(); 40 view(str); 41 } 42 }); 43 } 44 @Override 45 public void onBackPressed() { //按返回键回到第一个页面 46 Intent intent =new Intent(AddApplication.this,MainActivity.class); 47 startActivity(intent); 48 finish(); 49 } 50 public void view(String str){ //按下搜索后显示应用 51 LinearLayout app_add=(LinearLayout)findViewById(R.id.list_app); //先清空list_app的子布局 52 int count = app_add.getChildCount(); 53 while(count-1>=0){app_add.removeViewAt(count-1);count--;} 54 55 LayoutInflater inflater = LayoutInflater.from(this); 56 int i=0; 57 for(i=0;i .<", 83 Toast.LENGTH_SHORT).show(); 84 } 85 86 } 87 }); 88 89 app_add.addView(view_0); //往list_app添加子Item 90 } 91 92 } 93 94 } 95 private boolean verification(int p){ 96 for(int i=0;i
Constant,用来保存测试数据
1 package com.example.practice1; 2 3 public class Constant { 4 public static boolean press = false; 5 public static int num=13; 6 public static int[] image={R.drawable.image1, 7 R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5, 8 R.drawable.image6,R.drawable.image7,R.drawable.image8,R.drawable.image9, 9 R.drawable.image10,R.drawable.image11,R.drawable.image12,R.drawable.image13,0,0,0,0,0,0,0};10 11 public static String[] text={"1","2","3","4","5","6","7","8","9","10","11","12","13","0","0","0","0","0","0","0"};12 public static String[] application ={"微信","QQ","华农宝","飞信","微博","酷狗音乐"};13 public static int[] app_icon={R.drawable.weixin,R.drawable.qq,R.drawable.huanongbao,R.drawable.feixin,R.drawable.weibo,R.drawable.kugou};14 15 }
最后是三个XML
grid_adapter.XML
list_view
addapplication.XML
最后附上源码,有需要的请留言留下你的邮箱,供嫌长的童鞋学习学习= = ~
有问题请留言