( http://coderzheaven.com/2011/03/29/using-gestures-in-androida-simple-example )
package pack.GestureSampleThree;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.LinearLayout;
import android.widget.TextView;
public class GestureSampleThreeExample extends Activity implements OnGestureListener {
private LinearLayout main;
private TextView viewA;
private GestureDetector gestureScanner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gestureScanner = new GestureDetector(this);
main = new LinearLayout(this);
main.setBackgroundColor(Color.GRAY);
main.setLayoutParams(new LinearLayout.LayoutParams(320,480));
viewA = new TextView(this);
viewA.setBackgroundColor(Color.YELLOW);
viewA.setTextColor(Color.BLACK);
viewA.setTextSize(16);
viewA.setLayoutParams(new LinearLayout.LayoutParams(320,80));
main.addView(viewA);
setContentView(main);
}
@Override
public boolean onTouchEvent(MotionEvent me) {
return gestureScanner.onTouchEvent(me);
}
public boolean onDown(MotionEvent e) {
viewA.setText("-" + "DOWN" + "-");
return true;
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
viewA.setText("-" + "FLING" + "-");
return true;
}
public void onLongPress(MotionEvent e) {
viewA.setText("-" + "LONG PRESS" + "-");
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
viewA.setText("-" + "SCROLL" + "-");
return true;
}
public void onShowPress(MotionEvent e) {
viewA.setText("-" + "SHOW PRESS" + "-");
}
public boolean onSingleTapUp(MotionEvent e) {
viewA.setText("-" + "SINGLE TAP UP" + "-");
return true;
}
}
=========================================================================
計算BMI
Bm.java
package Bm.i;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.NumberFormat;
public class Bm extends Activity {
/** Called when the activity is first created. */
private EditText et1, et2;
private Button bt1;
private TextView tv1, tv2, tv3, tv4;
public static final String MY_PREFS = "mSharedPreferences01";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1 = (EditText)Bm.this.findViewById(R.id.et1);
et2 = (EditText)Bm.this.findViewById(R.id.et2);
bt1 = (Button)Bm.this.findViewById(R.id.bt1);
tv3 = (TextView)Bm.this.findViewById(R.id.tv3);
tv4 = (TextView)Bm.this.findViewById(R.id.tv4);
bt1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0)
{
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits( 2 );
EditText et1 = (EditText)findViewById(R.id.et1);
EditText et2 = (EditText)findViewById(R.id.et2);
double h = Double.parseDouble(et1.getText().toString());
double w = Double.parseDouble(et2.getText().toString());
double BMI = w / ((h/100) * (h/100));
tv3.setText("BMI:"+nf.format(BMI));
if(BMI<18.5){
tv4.setText("體重過輕");
}
else if(18.5<=BMI & BMI<24){
tv4.setText("體重適中");
}
else if(24<=BMI){
tv4.setText("體重過重");
}
}
});
}
}
main.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="fill_parent"
>
<TextView
android:text="Height"
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<EditText
android:id="@+id/et1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="numberDecimal"
>
<requestFocus></requestFocus>
</EditText>
<TextView
android:text="Weight"
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<EditText
android:id="@+id/et2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="numberDecimal"
></EditText>
<Button
android:text="Count"
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></Button>
<TextView
android:text=" "
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<TextView
android:text=" "
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
</LinearLayout>