-3
我是新來的android。我正在構建一個城市公交車票的應用程序。我會用兩個紡紗廠作爲'起點'和'目的地'。我想匹配這兩個輸入,並在textview中將結果作爲「票價」。請給我一些關於如何實現這一點的簡單想法。如何使用微調項目作爲輸入獲得結果
()返回其選擇的對象或spinner.getSelectedItemPosition()將返回選定的項目位置,並根據雙方的選擇我是新來的android。我正在構建一個城市公交車票的應用程序。我會用兩個紡紗廠作爲'起點'和'目的地'。我想匹配這兩個輸入,並在textview中將結果作爲「票價」。請給我一些關於如何實現這一點的簡單想法。如何使用微調項目作爲輸入獲得結果
()返回其選擇的對象或spinner.getSelectedItemPosition()將返回選定的項目位置,並根據雙方的選擇使用spinner.getSelectedItem微調器,你會計算你車費
這裏是工作示例:
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="24dp">
<TextView
android:id="@+id/text_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="30dp"
android:text="RESULT"/>
<Spinner
android:id="@+id/spinner_source"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
</Spinner>
<Spinner
android:id="@+id/spinner_destination"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
</Spinner>
</LinearLayout>
MainActivity.ja VA
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
TextView textResult;
Spinner spinnerSource;
Spinner spinnerDestination;
String source;
String destination;
int cost = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Views
textResult = (TextView) findViewById(R.id.text_result);
spinnerSource = (Spinner) findViewById(R.id.spinner_source);
spinnerDestination = (Spinner) findViewById(R.id.spinner_destination);
// Spinner Drop down elements
List<String> locations = new ArrayList<String>();
locations.add("Location 1");
locations.add("Location 2");
locations.add("Location 3");
// Initialize
source = locations.get(0);
destination = locations.get(0);
// Creating adapter for spinner
ArrayAdapter<String> adapterSpinnerSource = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, locations);
ArrayAdapter<String> adapterSpinnerDestination = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, locations);
// attaching data adapter to spinners
spinnerSource.setAdapter(adapterSpinnerSource);
spinnerDestination.setAdapter(adapterSpinnerDestination);
// Spinner item select listener
spinnerSource.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Source location
source = parent.getItemAtPosition(position).toString();
calculateCost();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinnerDestination.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Destination location
destination = parent.getItemAtPosition(position).toString();
calculateCost();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void calculateCost() {
if ((source.equals("Location 1") && destination.equals("Location 2"))
|| (source.equals("Location 2") && destination.equals("Location 1"))) {
cost = 100;
} else if ((source.equals("Location 1") && destination.equals("Location 3"))
|| (source.equals("Location 3") && destination.equals("Location 1"))) {
cost = 200;
} else if ((source.equals("Location 2") && destination.equals("Location 3"))
|| (source.equals("Location 3") && destination.equals("Location 2"))) {
cost = 300;
}
// Output
textResult.setText(String.valueOf(cost));
}
}
OUTPUT:
希望這將有助於〜