简体   繁体   English

使用微调器和提交按钮更新整数值(Android Studio SDK)

[英]Update Integer Value using Spinner and Submit Button (Android Studio SDK)

I have been studying Java for Android for a few weeks now and I am working on my first app. 我已经学习Android的Java了几个星期,并且正在开发我的第一个应用程序。 Looking thru answers to similar questions on this website, I'm starting to think I'm approaching my problem from the wrong angle. 通过浏览本网站上类似问题的答案,我开始认为我正在从错误的角度来解决我的问题。 Before I scrap my work, I'd like to ask some questions about my approach. 在我取消工作之前,我想问一些关于我的方法的问题。 I appreciate your help <3 感谢您的帮助<3

If I create a Spinner from an array in the strings.xml file, can I use the value of the spinner (after a Submit Button is pressed) to update an integer that I have also stored in my strings.xml file, and display it in a TextView area? 如果我从strings.xml文件中的数组创建一个微调 ,是否可以使用微调框的值(在按下“ 提交”按钮之后) 更新我也存储在strings.xml文件中的整数 ,并显示它在TextView区域中?

strings.xml: strings.xml:

<integer-array name="numbers_spinner">
    <item>1</item>
    <item>2</item>
    <item>3</item>
</integer-array>

<integer name="my_number">0</integer>

activity_myApp.xml: activity_myApp.xml:

<Spinner
    android:id="@+id/numbers_spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:entries="@array/numbers_spinner"
    android:background="@android:drawable/btn_dropdown"
    android:spinnerMode="dropdown" />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/submit_button"
    android:onClick="sendMessage"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@integer/my_number"/>

Context: I'm hoping to add the value from the Spinner to a total value, but I haven't worked out how to do that yet. 上下文:我希望将微调器中的值添加到总值中,但是我还没有弄清楚如何做到这一点。 I am currently stuck on creating an event listener to get the value from my Spinner using a Submit Button. 我目前坚持创建事件侦听器,以使用“提交”按钮从Spinner中获取值。

Is it possible to update the value of int my_number in this way, or do I have to create the Spinner Array in the .java Main Activity file ? 是否可以通过这种方式更新int my_number的值,还是必须在.java Main Activity文件中创建Spinner Array? I'm happy to do that if necessary, but as I've got it set up this way I'm hoping to continue down this path unless you guys believe it to be a poor choice. 如有必要,我很乐意这样做,但是由于我已经按照这种方式进行设置,因此我希望继续沿着这条道路前进,除非你们相信这是一个糟糕的选择。 I'm grateful for any help you can give, using the strings.xml file OR the MainActivity.java file <3 感谢您使用strings.xml文件或MainActivity.java文件<3提供的任何帮助

您可以通过单击以下按钮来做到这一点:

textview.setText(String.valueOf(spinner.getSelectedItem()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM