gpt4 book ai didi

android - 无法读取 android 微调器值

转载 作者:行者123 更新时间:2023-11-29 00:23:05 25 4
gpt4 key购买 nike

我是 Android 开发的新手,只是尝试各种示例来熟悉。我无法读取和显示微调器工具中的值。

检查了来自 Stackoverflow 和其他网站的各种示例,但仍然得到空白值。

请让我知道我错过了什么。

public void onClick(View view) {
EditText weight = (EditText)findViewById(R.id.editText1);
EditText height = (EditText)findViewById(R.id.editText2);

TextView output = (TextView)findViewById(R.id.textView3);

Spinner wtype = (Spinner)findViewById(R.id.spinner1);
Spinner htype = (Spinner)findViewById(R.id.spinner2);

String w = weight.getText().toString();
String h = height.getText().toString();
String s1 = wtype.getSelectedItem().toString();
String s2 = htype.getSelectedItem().toString();

output.setText(w+h+s1+s2);
}

在上面的代码中,w 和 h 的值显示在 s1 和 s2 的值并不适用于所有条件的地方...

这只是一个简单的 BMI 计算器。 strings.xml 包含以下文本。

<string-array name="weight_type">
<item>kgs</item>
<item>pounds</item>
</string-array>
<string-array name="height_type">
<item>metres</item>
<item>feet</item>
<item>inches</item>
</string-array>

默认显示公斤和米。我单击按钮打印 2 个默认值,但没有任何反应。我尝试更改下拉值并再次单击按钮。

仍然没有结果!!!!

尝试使用 http://www.mkyong.com/android/android-spinner-drop-down-list-example/ 中的示例我仍然无法得到结果。唯一的区别是我没有使用 TOAST,而是尝试将输出写入 TextView....

谁能发布一个工作代码!!!

最佳答案

首先必须在 onCreate() 方法中声明 findViewById。

然后像下面这样在字符串中获取您选择的项目:

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int pos, long arg3)
{
// TODO Auto-generated method stub
String s1 = parent.getItemAtPosition(pos).toString();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});

然后在您的 onClik 方法中,您只需设置文本:

String w = weight.getText().toString();
String h = height.getText().toString();
String s1 = wtype.getSelectedItem().toString();
String s2 = htype.getSelectedItem().toString(); // Just like S1 as above make another Spinner S2 Listener.

output.setText(w+h+s1+s2);

关于android - 无法读取 android 微调器值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21737352/

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