好记性不如烂笔杆-android学习笔记<一>
1,//每个apk文件中都要有的,manifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
//每个Activity需要在minafest中注册一些,否则出错,可以更改android:name改变启动的Activity
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="zjq.broadcast" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 <uses-sdk android:minSdkVersion="10" /> 7 <application 8 android:icon="@drawable/ic_launcher" 9 android:label="@string/app_name" > 10 <activity 11 android:name=".TestBCActivity" 12 android:label="@string/app_name" > 13 <intent-filter> 14 <action android:name="android.intent.action.MAIN" /> 15 <category android:name="android.intent.category.LAUNCHER" /> 16 </intent-filter> 17 </activity> 18 </application> 19 </manifest>
2,//android页面跳转Intent
1 Intent intent = new Intent(); 2 intent.putExtra("testIntent", "123"); 3 intent.setClass(Activity02.this, otherActivity.class); 4 Activity02.this.startActivity(intent);
//多个Activity调用同一个activity时,可以通过Intent传参数
1 Intent intent = new Intent(); 2 intent.setClass(mContext, summaryActivity.class); 3 intent.putExtra("Click", "There is the ImageView onClick method----------------------->" + 4 "test,test,test,test,test,test,test,test,test,test,test,test,test,test,test,test," +"); 5 mContext.startActivity(intent);
3,//接收Intent传来的文件
1 Intent intent = getIntent(); 2 String value = intent.getStringExtra("testIntent");
4,//得到Intent对象中的值,转换类型
1 Intent intent = getIntent(); 2 String factorOneStr = intent.getStringExtra("one"); 3 String factorTwoStr = intent.getStringExtra("two");
5,//转换为整型
1 int factorOneInt = Integer.parseInt(factorOneStr); 2 int factorTwoInt = Integer.parseInt(factorTwoStr); 3 4 int week = ((Integer)map.get("bottom_week")).intValue();//Integer 类型转换为int类型
6,/**创建menu菜单
不需要在manifest文件中注册,
也可以在代码中使用registerForContextMenu(view);进行视图注册
*/
<1> //复写这个函数,回调函数,创建菜单选项:menu,不需要在Manifest中配置,当客户点击MENU按钮时调用该方法
1 public boolean onCreateOptionsMenu(Menu menu){ 2 menu.add(0,1,1,R.string.exit); 3 menu.add(0, 2, 2, R.string.about); 4 return super.onCreateOptionsMenu(menu); 5 }
//复写一个函数,显示menu菜单,当客户点击菜单中的某一个按钮时,调用该方法
1 public boolean onOptionsItemSelected(MenuItem item){ 2 if(item.getItemId()== 1){ 3 finish(); 4 } 5 return super.onOptionsItemSelected(item); 6 }
<2>//创建上下文菜单
1 @Override 2 public void onCreateContextMenu(ContextMenu conMenu, View v, 3 ContextMenuInfo menuInfo) { 4 super.onCreateContextMenu(conMenu, v, menuInfo); 5 conMenu.setHeaderTitle("ContextMenu"); 6 conMenu.add(0, 0, 0, "Delete"); 7 conMenu.add(0, 1, 1, "copy"); 8 conMenu.add(0, 2, 2, "rename"); 9 conMenu.add(0, 3, 3, "move"); 10 conMenu.add(0, 4, 4, "others"); 11 }
//当用户点击菜单中的某一个按钮时,调用该方法
1 @Override 2 public boolean onContextItemSelected(MenuItem item) { 3 System.out.println("OnContextItemSelected------------>"); 4 return super.onContextItemSelected(item); 5 }
6 public class OnCreateContextMenuListener implements OnItemLongClickListener{ 7 @Override 8 public boolean onItemLongClick(AdapterView<?> arg0, View arg1, 9 int arg2, long arg3) { 10 return false; 11 } 12 }
19,//获取随机数
Random random = new Random();
int index = random.nextInt(9);