我是android新手。我正在构建城市公交车票的应用程序。我将使用两个微调器作为开始'点和'目的地'。我想匹配这两个输入并得到结果'票价'在textview中。请告诉我如何实现这一点。
答案 0 :(得分:2)
使用spinner.getSelectedItem()它返回所选对象或spinner.getSelectedItemPosition()它将根据两个微调器的选择返回所选项目位置,你将计算你的票价
答案 1 :(得分:1)
以下是工作示例:
<强> 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.java 强>
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));
}
}
<强>输出:强>
希望这会有所帮助〜