Android常用超给力语句集锦
1.Android默认情况下提供了一些实用的主题样式,比如说Theme.Dialog可以让你的Activity变成一个窗口风格,而Theme.Light则让你的整个Activity具有白色的背景,而不是黑色那么沉闷。具体使用方法很简单在Androidmanifest.xml文件中对你的Activity节点上加入代码:android:theme="@android:style/Theme.Dialog"
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Show_Image"
android:theme="@android:style/Theme.Dialog"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity_Image"
android:theme="@android:style/Theme.Dialog">
</activity>
<activity android:name=".Acitivity_zoom"
android:theme="@android:style/Theme.Dialog">
</activity>
<activity android:name=".Activity_Rotate"
android:theme="@android:style/Theme.Dialog">
</activity>
这里我提醒大家,有关@后面是android:的定义均在Android内部的Framework层由系统自带。
2.android用户界面-提示信息Toast的使用:
在程序中创建toast的步骤说明如下
1、调用toast的静态方法makeText()添加现实文本和时长。
2、调用toast的show()显示。
实例代码:
class ShowImageListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Show_Image.this, Activity_Image.class);
// 使用这个Intent对象来启动Activity_Image;
Show_Image.this.startActivity(intent);
Toast.makeText(Show_Image.this, "Go to Activity_Image",
Toast.LENGTH_SHORT).show();
}
}
}
3.返回上一个Acitvity:
class ReturnListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(Acitivity_zoom.this, "Return to Activity_Image",
Toast.LENGTH_SHORT).show();
finish();
}
}
}
即通过响应Return按钮的Activity返回上一个Activity,轻松实现了Activity的返回。效果如图:
4.今天写了一个测试EditText插件的程序。
显示EditText的Activity命名为 EditText.需要获取EditText插件的View,使用下面的代码:
EditText edittext = (EditText) findViewById(R.id.edittext);
结果出现错误:Connt cast from View to EditText
最后发现错误:将自定义的Activity命名为EditText,EditText是Android库中默认的类,所以会出现重名。
处理方法:把自定的Activity类的类名改掉。
5.android 设置全屏显示和自适应屏幕:
- package xiaohang.zhimeng;
- import android.app.Activity;
- import android.content.pm.ActivityInfo;
- import android.os.Bundle;
- import android.view.Window;
- import android.view.WindowManager;
- public class Activity01 extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //设置为无标题栏
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- //设置为全屏模式
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
- //设置为横屏
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
- setContentView(R.layout.main);
- }
- }
- 完过G1的朋友都知道它内置了加速感应器,可以让应用程序自动适应屏幕的模式,比如当前是竖屏模式,当我们将手机横放时,应用程序会自动变为横向。我们可以很简单地实现这个效果,在Eclipse中双击 AndroidMainfest.xml文件,选择Application选项卡,选中Activity类,这里注意一下大家要选中Activity类在左边不然找不到的。然后再右边就可以看到"Screen orientation" 选项,选择 "sensor",最后保存即可,这时在真机上就能感觉到效果了。
当然也可以直接在AndroidMainfest.xml 文件中修改 "<activity android:name=".Activity01" android:label="@string/app_name" android:screenOrientation="sensor">",可以实现相同的效果,配置文件如下
<activity android:name=".Activity01"
android:label="@string/app_name"
android:screenOrientation="sensor" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
6.你的手机肯定可以震动吧,如果在你的软件中加入了震动的效果,是不是显得很不一般呢?其实一切都是浮云,这个是灰常的简单啦,废话少说,请看题:
private Vibrator mVibrator01; //声明一个振动器对象
mVibrator01 = ( Vibrator ) getApplication().getSystemService(Service.VIBRATOR_SERVICE);
mVibrator01.vibrate( new long[]{100,10,100,1000},-1);
7.你想在打开程序时因为程序难以加载而苦苦等待吗?不用急,ProgressBar就可以实现良好的效果了。。
ProgressBar.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="圆形进度条" />
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水平进度条" />
<ProgressBar
android:id="@+id/progress_horizontal"
style="?android:attr/progressBarStyleHorizontal"//设置ProgressBar的方向为水平方向的。。
android:layout_width="200dip"
android:layout_height="wrap_content"
android:max="100" //设置进度条最大值为100
android:progress="50"
android:secondaryProgress="75" />
</LinearLayout>
在XML布局文件中加入
<RatingBar
android:id="@+id/rating_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ratingBarStyleSmall="true"
/>
9:查看是否有存储卡插入
String status=Environment.getExternalStorageState();
if(status.equals(Enviroment.MEDIA_MOUNTED))
{
说明有SD卡插入
}
2:让某个Activity透明
OnCreate中不设Layout
this.setTheme(R.style.Theme_Transparent);
以下是Theme_Transparent的定义(注意transparent_bg是一副透明的图片)
3:在屏幕元素中设置句柄
使用Activity.findViewById来取得屏幕上的元素的句柄. 使用该句柄您可以设置或获取任何该对象外露的值.
TextView msgTextView = (TextView)findViewById(R.id.msg);
msgTextView.setText(R.string.push_me);
4:发送短信
String body=”this is mms demo”;
Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”smsto”, number, null));
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true);
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true);
startActivity(mmsintent);
5:发送彩信
StringBuilder sb = new StringBuilder();
sb.append(”file://”);
sb.append(fd.getAbsoluteFile());
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”mmsto”, number, null));
// Below extra datas are all optional.
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT, subject);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI, sb.toString());
intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, composeMode);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, exitOnSent);
startActivity(intent);
7:发送Mail
mime = “img/jpg”;
shareIntent.setDataAndType(Uri.fromFile(fd), mime);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fd));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
shareIntent.putExtra(Intent.EXTRA_TEXT, body);
8:注册一个BroadcastReceiver
registerReceiver(mMasterResetReciever, new IntentFilter(”OMS.action.MASTERRESET”));
private BroadcastReceiver mMasterResetReciever = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent){
String action = intent.getAction();
if(”oms.action.MASTERRESET”.equals(action)){
RecoverDefaultConfig();
}
}
};
9:定义ContentObserver,监听某个数据表
private ContentObserver mDownloadsObserver = new DownloadsChangeObserver(Downloads.CONTENT_URI);
private class DownloadsChangeObserver extends ContentObserver {
public DownloadsChangeObserver(Uri uri) {
super(new Handler());
}
@Override
public void onChange(boolean selfChange) {}
}
10:获得 手机UA
public String getUserAgent()
{
String user_agent = ProductProperties.get(ProductProperties.USER_AGENT_KEY, null);
return user_agent;
}
11:清空手机上cookie
CookieSyncManager.createInstance(getApplicationContext());
CookieManager.getInstance().removeAllCookie();
12:建立GPRS连接
//Dial the GPRS link.
private boolean openDataConnection() {
// Set up data connection.
DataConnection conn = DataConnection.getInstance();
if (connectMode == 0) {
ret = conn.openConnection(mContext, “cmwap”, “cmwap”, “cmwap”);
} else {
ret = conn.openConnection(mContext, “cmnet”, “”, “”);
}
}
13:PreferenceActivity 用法
在开发应用程序的过程中我们有很大的机会需要用到参数设置功能,那么在Android应用中,我们如何实现参数设置界面及参数存储呢,下面我们来介绍一下Android中的一个特殊Activity–PreferencesActivity。PreferencesActivity是Android中专门用来实现程序设置界面及参数存储的一个Activity,我们用一个实例来简介如何使用PreferencesActivity。
public class Setting extends PreferenceActivity
{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
Setting.xml:
Android:key=”seting2″
android:title=”@string/seting2″
android:summary=”@string/seting2″/>
android:key=”seting1″
android:title=”@string/seting1″
android:summaryOff=”@string/seting1summaryOff”
android:summaryOn=”@stringseting1summaryOff”/>
14:通过HttpClient从指定server获取数据
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet method = new HttpGet(“http://www.baidu.com/1.html”);
HttpResponse resp;
Reader reader = null;
try {
// AllClientPNames.TIMEOUT
HttpParams params = new BasicHttpParams();
params.setIntParameter(AllClientPNames.CONNECTION_TIMEOUT, 10000);
httpClient.setParams(params);
resp = httpClient.execute(method);
int status = resp.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_OK) return false;
// HttpStatus.SC_OK;
return true;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null) try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
<TextView android:layout_width="100px"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:ellipsize="marquee"
android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:text="这才是真正的文字跑马灯效果"
>
16.通过电话号码查找联系人:
public String getContactIDFromPhoneNum(String phoneNum) {