我正在制作一个可以在屏幕上显示水果选项的应用程序。果实是:
苹果
葡萄
橙子
在屏幕过程中,我放置了切换过程,所以它会切换:
苹果优先 2 秒
然后切换到葡萄 2 秒
然后切换橙色2秒
切换过程将连续进行。
现在我想做一个“选择按钮”和一个“微调器”,这样用户就可以选择一种水果,然后在“微调器”上看到他的选择。
例如:用户想要选择橙色。
用户将等到屏幕上显示“橙色”。
当屏幕上显示“橙色”时,直接按下“选择按钮”,同时“Spinner1”将被“橙色值”填充。
*我不想使用 TextView。
这是我的代码...
主要活动
private MenuItem Apple;
private MenuItem Grape;
private MenuItem Orange;
private Spinner spinner;
private Button choose_button;
private Button btnPrice;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
choose_button = (Button) findViewById(R.id.button1);
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
@Override
public void onClick(View v)
{ // i don't know what should i write here
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i(TAG, "onCreateOptionsMenu");
Apple = menu.add("Apple");
Grape = menu.add("Grape");
Orange = menu.add("Orange");
@Override
public boolean onOptionsItemSelected(MenuItem fruit) {
Log.i(TAG, "Menu Item selected " + fruit);
if (fruit == Apple) {
frame.setViewMode(classfruit.apple);
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
onOptionsItemSelected(Grape);}
catch(Exception ex){}}
}).start();
} else if (fruit == Grape) {
frame.setViewMode(classfruit.grape);
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
onOptionsItemSelected(orange);}
catch(Exception ex){}}
}).start();
} else if (fruit == orange) {
frame.setViewMode(classfruit.orange);
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
onOptionsItemSelected(apple);}
catch(Exception ex){}}
}).start();
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
// get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
btnPrice = (Button) findViewById(R.id.btnPrice);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/* get fruit */
String fruit = String.valueOf(spinner1.getSelectedItem());
/* get price */
String price = getPrice(fruit);
public String getPrice(String fruit) {
String price = "0";
if (fruit.equalsIgnoreCase("Apple")) {
price = "1";
} else if (warna.equalsIgnoreCase("Grape")) {
price = "2";
} else if (warna.equalsIgnoreCase("Orange")) {
price = "3";
}
主要的.XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context=".MainActivity" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:entries="@array/fruit"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/btn_choose" />
字符串.xml
<string name="btn_choose">Choose</string>
<string-array name="fruit">
<item>Apple</item>
<item>Grape</item>
<item>Orange</item>
</string-array>