所以我创建了一个Spinner并在其中设置了5个值。 我正在构建一个计算器,并希望Spinner的每个项目都有一些价值(它们有文本值,但我希望它们具有数值)。在此之前,用户必须输入他的TDEE,不要打扰它,它只是一个数字,然后该数字乘以所选的微调项目的值。单击按钮时会发生这种情况,然后在文本视图中打印出值。
问题是,我不知道如何将值传递给微调器项。
以下是代码:
的字符串:
<string-array name="razina_aktivnosti">
<item>Sjedeći (Malo ili bez aktivnosti, pretežno sjedeći posao)</item>
<item>Laka aktivnosti (Lake vježbe/sport 1-3 dana tjedno)</item>
<item>Umjeren (Umjerene vježbe/sport 3-5 dana tjedno)</item>
<item>Teške aktivnosti (Teže vježbe/sport 6-7 dana tjedno)</item>
<item>Ekstremne aktivnosti (Teške dnevne vježbe/sport i fizički posao 2 x dnevno)</item>
</string-array>
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="hr.app.liftme.liftmehr.KalkulatoriTDEE"
tools:showIn="@layout/activity_kalkulatori_tdee">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout22"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Unesite svoj BMR:"
android:id="@+id/textView115" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editBMRInput" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout22"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/linearLayout23"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Odaberite razinu aktivnosti"
android:id="@+id/aktivnost"
android:layout_below="@+id/linearLayout22"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Spinner
android:id="@+id/spinnerTDEE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/aktivnost"
android:layout_alignParentLeft="true">
</Spinner>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/linearLayout23">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Izračunaj"
android:id="@+id/btnIzracunajTDEE"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:background="#424242"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Vaš TDEE je:"
android:id="@+id/textView116"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/textViewRezultatTDEE"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</RelativeLayout>
爪哇:
package hr.app.liftme.liftmehr;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class KalkulatoriTDEE extends AppCompatActivity {
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
Button btnIzracunaj;
EditText editBMRInput;
TextView textViewRezultat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kalkulatori_tdee);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
spinner = (Spinner) findViewById(R.id.spinnerTDEE);
adapter = ArrayAdapter.createFromResource(this, R.array.razina_aktivnosti, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
btnIzracunaj = (Button) findViewById(R.id.btnIzracunajTDEE);
editBMRInput = (EditText) findViewById(R.id.editBMRInput);
textViewRezultat = (TextView) findViewById(R.id.textViewRezultatTDEE);
btnIzracunaj.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
double prvoPolje = Double.parseDouble(editBMRInput.getText().toString());
// HERE'S WHERE I'VE STUCKED!!!
double rezultat = 0;
btnIzracunaj.setText(Double.toString(rezultat));
}
});
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemIdAtPosition(position)+" selected", Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
有什么想法吗?
答案 0 :(得分:1)
使用Spinner中的getSelectedItem()
:
spinner.getSelectedItem().toString()
因此,在您的代码中,将btnIzracunaj
侦听器替换为:
btnIzracunaj.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, spinner.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
}
});
您将需要解析 strings.xml 上的内容。我建议你把它们设置为数字来测试:
<string-array name="razina_aktivnosti">
<item>1.1</item>
<item>2.2</item>
<item>3.3</item>
<item>4.4</item>
<item>5.5</item>
</string-array>
修改强>
将 strings.xml 设置为仅浮动数字,新的onClickListener将如下所示:
btnIzracunaj.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double prvoPolje = Double.parseDouble(editBMRInput.getText().toString());
double rezultat = 0;
Toast.makeText(MainActivity.this, spinner.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
rezultat = Double.valueOf(spinner.getSelectedItem().toString());
btnIzracunaj.setText(String.valueOf(rezultat * prvoPolje));
}
});