gpt4 book ai didi

java - 从微调器中为项目设置值

转载 作者:行者123 更新时间:2023-12-01 10:29:29 26 4
gpt4 key购买 nike

所以我创建了一个 Spinner 并在其中设置了 5 个值。我正在构建一个计算器,并希望 Spinner 中的每个项目都有一些值(它们有文本值,但我希望它们有数字值)。在此之前,用户必须输入他的 TDEE,不用管它,它只是一个数字,然后该数字乘以所选微调项目的值。当单击按钮时,这一切都会发生,然后在 TextView 中打印该值。

问题是,我不知道如何将值传递给微调项目。

代码如下:

字符串:

<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>

Java:

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) {

}


});



}

}

有什么想法吗?

最佳答案

使用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 设置为仅 float 时,您的新 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));
}
});

关于java - 从微调器中为项目设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35166767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com