2011年9月23日星期五

接近傳感器 (Proximity Sensor)

通過接近傳感器 (Proximity Sensor)可以檢測到 Android 手機靠近我的臉, 進而做一些動作, 例如禁用觸摸功能,或關閉屏幕.

public class AndroidProximitySensorActivity extends Activity {
/** Called when the activity is first created. */

SensorManager mySensorManager;
Sensor myProximitySensor;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
myProximitySensor = mySensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

if (myProximitySensor == null){
//No Proximity Sensor!

}else{
mySensorManager.registerListener(proximitySensorEventListener, myProximitySensor,SensorManager.SENSOR_DELAY_NORMAL);
}
}

SensorEventListener proximitySensorEventListener = new SensorEventListener(){

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub

if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){
//do something
}
}
};
}



2011年9月19日星期一

顯示標題進度條, setProgressBarIndeterminateVisibility()

要在標題欄顯示進度條, 首先在 setContentView() 前調用 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS) 函數要求FEATURE_INDETERMINATE_PROGRESS 功能. 再通過調用 setProgressBarIndeterminateVisibility(true/false) 函數顯示/隱藏標題進度條.

顯示標題進度條

package com.AndroidProgressTitle;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;

public class AndroidProgressTitleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);

Button showProgress = (Button)findViewById(R.id.showprogress);
Button hideProgress = (Button)findViewById(R.id.hideprogress);

showProgress.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
setProgressBarIndeterminateVisibility(true);
}});

hideProgress.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
setProgressBarIndeterminateVisibility(false);
}});
}
}


<?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="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/showprogress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="顯示標題進度條"
/>
<Button
android:id="@+id/hideprogress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="隱藏標題進度條"
/>
</LinearLayout>

2011年9月10日星期六

通過 XML 在文本視圖 (TextView) 設定陰影效果 (shadow)

在文本視圖 (TextView) 設定陰影效果


<?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="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:shadowColor="#FFFFFF"
android:shadowRadius="5"
android:shadowDx="10"
android:shadowDy="10"
/>
</LinearLayout>




2011年9月6日星期二

android:onClick - 定義 onClick 事件回調函數

我們可以在佈局文件(main.xml)中通過 android:onClick 屬性, 直接定義 onClick 事件的回調函數.

實例:

<?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="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click Me"
android:onClick="onClick"
/>
</LinearLayout>




package com.AndroidOnClick;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class AndroidOnClickActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

public void onClick(View view){
Toast.makeText(AndroidOnClickActivity.this,
"Button Clicked", Toast.LENGTH_LONG).show();
}
}