每次查到路线后都想知道打车多少钱,就写了个小小apk,方便些。就是知道从哪到哪多少公里,想知道打车要多少钱。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.taxipricecaculator" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.taxipricecaculator.TaxtPriceCaculator" android:label="@string/app_name" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
package com.example.taxipricecaculator; import android.os.Bundle; import android.app.Activity; import android.text.Editable; import android.text.TextWatcher; import android.text.method.ScrollingMovementMethod; import android.view.Menu; import android.widget.Button; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.EditText; import android.widget.Switch; import android.widget.TextView; public class TaxtPriceCaculator extends Activity { private final int BASE_MILES = 3; private final int BASE_MILES_LONG = 10; private final int BASE_PRICE = 14; private static final float PRICE_PER = (float) 2.4; private static final float PRICE_PER_EXTRA = (float) 3.6; private final int BASE_PRICE_NIGHT = 18; private static final float PRICE_PER_NIGHT = (float) 3.1; private static final float PRICE_PER_EXTRA_NIGHT = (float) 4.7; private EditText mMilesInput; private Button mDisplayPrice; private Switch mAddFeeMode; private TextView mIntroduction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_taxt_price_caculator); findViewById(); addListener(); init(); } private void init() { mIntroduction.setMovementMethod(ScrollingMovementMethod.getInstance()); } private void findViewById() { mMilesInput = (EditText) this.findViewById(R.id.mileage_input); mDisplayPrice = (Button) this.findViewById(R.id.price_display); mAddFeeMode = (Switch) this.findViewById(R.id.switch_day_night_modes); mIntroduction = (TextView) this.findViewById(R.id.info); } private void addListener() { // TODO Auto-generated method stub mMilesInput.addTextChangedListener(new TextWatcher(){ @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub float miles = 0; try { miles = Float.parseFloat(s.toString().trim()); } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } mDisplayPrice.setText(CaculateFee(miles, mAddFeeMode.isChecked()) + ""); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } }); mAddFeeMode.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { int miles = 0; try { miles = Integer.parseInt(mMilesInput.getText().toString().trim()); } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } mDisplayPrice.setText(CaculateFee(miles, mAddFeeMode.isChecked()) + ""); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.taxt_price_caculator, menu); return true; } private float CaculateFee(float miles, boolean night_mode){ if(!night_mode){ if(miles < BASE_MILES){ return BASE_PRICE; }else if (miles > BASE_MILES && miles <= BASE_MILES_LONG){ return (BASE_PRICE + (miles - BASE_MILES) * PRICE_PER); }else if(miles > BASE_MILES_LONG){ return (float) (BASE_PRICE + (BASE_MILES_LONG - BASE_MILES) * PRICE_PER + (miles - BASE_MILES_LONG) * PRICE_PER_EXTRA); } }else{ if(miles <= BASE_MILES){ return BASE_PRICE_NIGHT; }else if (miles > BASE_MILES && miles <= BASE_MILES_LONG){ return (BASE_PRICE_NIGHT + (miles - BASE_MILES) * PRICE_PER_NIGHT); }else if(miles > BASE_MILES_LONG){ return (float) (BASE_PRICE_NIGHT + (BASE_MILES_LONG - BASE_MILES) * PRICE_PER_NIGHT + (miles - BASE_MILES_LONG) * PRICE_PER_EXTRA_NIGHT); } } return -1; } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".TaxtPriceCaculator" > <LinearLayout android:id="@+id/group_mileage" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/mileage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/label_mileage" /> <EditText android:id="@+id/mileage_input" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="number" android:ems="6" android:singleLine="true"/> <TextView android:id="@+id/mileage_unit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/label_mileage_unit" /> </LinearLayout> <LinearLayout android:id="@+id/group_price" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@+id/group_mileage" > <TextView android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/label_price" /> <Button android:id="@+id/price_display" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="6" /> <TextView android:id="@+id/price_unit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/label_price_unit" /> </LinearLayout> <Switch android:id="@+id/switch_day_night_modes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/group_price" android:text="@string/night_mode" android:layout_marginBottom="20dip" /> <TextView android:id="@+id/info_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/intrduction_title" android:layout_below="@+id/switch_day_night_modes"/> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/intrduction" android:layout_below="@+id/info_title" android:maxLines="10" android:scrollbars="vertical"/> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">TaxiPriceCaculator2</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello</string> <string name="label_mileage">"Miles:"</string> <string name="label_mileage_unit">Km</string> <string name="label_price">"Price:"</string> <string name="label_price_unit">RMB</string> <string name="night_mode">"During 23:00–05:00"</string> <string name="intrduction_title">"上海出租车价格计算方法:"</string> <string name="intrduction">"一般状况:车价=起步价【14元】+(里程数 - 起步里程数【3公里】)*每公里单价【2.4元】 (里程数大于3小于10) \n 车价=起步价【14元】 (里程数小于3) \n远程状况:车价=起步价【14元】+(远程里程标准【10公里】 - 起步里程数【3公里】)*每公里单价 + (里程数 - 远程里程标准【10】) *远程每公里单价【3.6元】 (里程数大于10)\n提醒:在5:00---23:00期间,起步费14元(包括一元的燃油费),可运营3公里,超过3公里后每公里2.40元,总里程超过10公里后超过部分按每公里3.60元计算。 \n在23:00---到次日5:00期间,起步费18元(包括一元的燃油费),可运营3公里,超过3公里后每公里3.10元,总里程超过10公里后超过部分按每公里4.70元计算。"</string> </resources>