|
1、获取资源: Resources res = getBaseContext().getResources(); Drawable draw=res.getDrawable(R.drawable.icon); 2、获得数组: String[] ary = getResources().getStringArray(R.array.ary); 3、自动提示框:
String[] arrays=new String[]{"a","ab","abc","bc","bcde","ee"}; actalert=(AutoCompleteTextView)findViewById(R.id.actalert); ArrayAdapter<String> adapter=new ArrayAdapter<String>( this, android.R.layout.simple_dropdown_item_1line, arrays); actalert.setAdapter(adapter); mactalert=(MultiAutoCompleteTextView)findViewById(R.id.mactvalert); mactalert.setAdapter(adapter); // 设置多个值之间的分隔符,此处为逗号 mactalert.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
4、spinner设置数据源
spncolor.setPrompt("请选择"); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.attr, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spncolor.setAdapter(adapter);
5、显示当前时间:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); // 将当前时间显示在TextView组件中 tvTime.setText("当前时间:" + sdf.format(new Date()));
6、代码添加布局文件
AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 64); TextView text = new TextView(activity); text.setLayoutParams(lp); text.setTextSize(20); text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); text.setPadding(36, 0, 0, 0); text.setText(s);
7、自定义窗口标题栏代码 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);//自定义标题栏 setContentView(R.layout.main); //为标题栏设置一个xml布局 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_custom);
custom_title.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/tvtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true"></TextView> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/tvtile" android:orientation="horizontal" android:layout_alignParentRight="true" android:gravity="center"> <Button android:text="@string/answer" android:id="@+id/btnanswer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/btn_inter" android:layout_marginRight="8dp" android:gravity="center" android:paddingTop="2dp"></Button> <Button android:text="@string/submit" android:id="@+id/btnsubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/btn_inter" android:layout_marginRight="8dp" android:gravity="center" android:paddingTop="2dp"></Button> </LinearLayout>
</RelativeLayout>
8、Activity仿Dialog Theme(加图标和标题其实就是Activity的icon和titile)(1)、自定义样式 <style name="Theme.CustomDialog" parent="android:style/Theme.Dialog"> <item name="android:windowBackground">@drawable/filled_box</item> </style>
@drawable/filled_box:资源文件中新建drawable文件夹,新建filled_box样式xml <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#f0600000"/> <stroke android:width="3dp" color="#ffff8080"/> <corners android:radius="3dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape>
(2)Manifest.xml中配置Acitivity样式 <activity android:name=".Main" android:label="@string/app_name" android:theme="@android:style/Theme.Dialog"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
(3)为Activity设置图标 //设定窗口模式(仿Dialog中的icon,带有一个左图标) requestWindowFeature(Window.FEATURE_LEFT_ICON); setContentView(R.layout.main); //设置图片资源 getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON , R.drawable.icon);
9、从一个Activity1到另一个Activity2,当在第二个Activity2按返回键不出现Activity1的做法是在打开Activity2的同时关闭Activity1 Intent intent = new Intent(Main.this, fowardwidget.class); startActivity(intent); finish();
10、允许TextView的文本值拼接 (1)允许在TextView的文本值后添加buffer text tv.setText(tv.getText(),TextView.BufferType.EDITABLE);
(2)使用Editable对象添加buffer text Editable text=(Editable)tv.getText();
text.append("editable");
text.append("\n"):
11、如果历史栈中包含Activity,打开此Activity从栈中放到栈顶层而不是从新打开Activity Intent intent = new Intent(ReorderFour.this, ReorderTwo.class); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(intent);
12、避免输入法面板遮挡,在manifest.xml中activity中设置android:windowSoftInputMode android:windowSoftInputMode="stateVisible|adjustResize"
13、获取当前手机壁纸和设置手机壁纸(wallpaper)(1)获取当前壁纸 WallpaperManager wm=WallpaperManager.getInstance(this); Drawable wallpaper=wpm.getDrawable();
(2)设置当前壁纸,同时要添加壁纸设置权限 imapaper.setDrawingCacheEnabled(true); Drawable drawale=this.getResources().getDrawable(R.drawable.bg); imapaper.setImageDrawable(drawale); wpm.setBitmap(imapaper.getDrawingCache());
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
14、常见通过系统服务得到的实例化对象 NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
15、检查网络是否连接 //检查网络是否连接 public boolean checkIntent(){ ConnectivityManager mannager=(ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE); NetworkInfo info=mannager.getActiveNetworkInfo(); if(info==null || !info.isConnected()){ return false; } if(info.isRoaming()){ return true; } return true; }
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
16、从资源文件中(asset)读取文本文档 //获得输入流 InputStream in=getAssets().open("read_asset.txt"); int size=in.available(); //将输入流读到字节数组中(内存) byte[] buffer=new byte[size]; in.read(buffer); in.close(); String text=new String(buffer);
17、TextView、Button等设置文本滚动(跑马灯效果),控件必须获得焦点才能有滚动效果,并且文字长度大于控件长度 android:singleLine="true" android:ellipsize="marquee" android:focusable="true" android:marqueeRepeatLimit="marquee_forever" //marquee_forever:一直滚动下去,n(整数):滚动n次
18、TextView文本添加下划线 TextView textView = (TextView)findViewById(R.id.testView); textView.setText(Html.fromHtml("<u>"+"hahaha"+"</u>"));
19、TextView添加图片 String src=""+R.drawable.qq; Html.ImageGetter imageGetter =new ImageGetter() { Drawable draw=null; @Override public Drawable getDrawable(String source) { // TODO Auto-generated method stub int id=Integer.parseInt(source.trim()); draw=getResources().getDrawable(id); draw.setBounds(0, 0, draw.getIntrinsicWidth(), draw.getIntrinsicHeight()); return draw; } }; tvquestion.append(Html.fromHtml("<img src="+src+"", imageGetter, null));
20、秒转成00:00:00 int hour=time/60/60%60; int minute = time/60%60; int second = time%60; String strhour=hour<10 ? "0" + hour : "" + hour; String strminutes=minute<10 ? "0" + minute : "" + minute; String strseconds=second < 10 ? "0" + second : "" + second; tvtimer.setText(strhour+":"+strminutes+":"+strseconds);
21、TextView添加滚动条 <ScrollView android:id="@+id/sv_e8_question" android:layout_width="fill_parent" android:layout_height="180dp" android:scrollbarStyle="outsideOverlay"> <TextView android:text="@string/question" android:id="@+id/tv_e8_question" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="5dp" android:paddingLeft="5dp"></TextView> </ScrollView>
22、Activity自动销毁时调用onSaveInstanceState()方法保存配置信息:可多Activity共享 @Override public void onSaveInstanceState(Bundle outState){ SharedPreferences.Editor editor=getSharedPreferences("sp_timer", 0).edit(); editor.putInt(arg0, arg1); super.onSaveInstanceState(outState); }
单个Activity保存 @Override public void onSaveInstanceState(Bundle outState){ SharedPreferences.Editor editor=this.getPreferences(0).edit(); editor.putInt(arg0, arg1); super.onSaveInstanceState(outState); }
|