1

我正在制作一个可以在屏幕上显示水果选项的应用程序。果实是:

  1. 苹果

  2. 葡萄

  3. 橙子

在屏幕过程中,我放置了切换过程,所以它会切换:

  1. 苹果优先 2 秒

  2. 然后切换到葡萄 2 秒

  3. 然后切换橙色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>
4

2 回答 2

0

检查我的代码:

活动主.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_height="match_parent">

    <Spinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Choose"/>
</LinearLayout>

主要活动:

package com.example.MyTest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends Activity {

   private Spinner spinner;
   private Button button;
   private Timer timer;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner = (Spinner) findViewById(R.id.spinner);
        button = (Button) findViewById(R.id.button);

        spinner.setAdapter(new ArrayAdapter(this,
                android.R.layout.simple_spinner_dropdown_item,
                Arrays.asList(getResources().getStringArray(R.array.fruit))));
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(spinner.getSelectedItemPosition() == (Arrays.asList(getResources().getStringArray(R.array.fruit)).size()-1)){
                            spinner.setSelection(0);
                        }else{
                            spinner.setSelection(spinner.getSelectedItemPosition()+1);
                        }
                    }
                });
            }
        },0,2000);

        button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (timer != null) {
                timer.cancel();
            }
            String spinnerSelectedItem = (String) spinner.getSelectedItem();
            Toast.makeText(MainActivity.this,spinnerSelectedItem,Toast.LENGTH_LONG).show();
        }
    });
    }

}

在此处输入图像描述

于 2013-10-14T04:51:46.663 回答
0

非常感谢Haresh,我的程序正在运行中......

接下来如何设置这个计时器是这样的..

当应用程序启动时,只有 Spinner1 正在运行(现在当应用程序启动时所有微调器都在运行),

然后当我单击btn_choose1然后Spinner1停止然后Spinner2开始直到再次单击btn_choose2然后它停止。

这是我的代码...

// get the selected dropdown list value
    public void addListenerOnButton() {

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);
        spinner3 = (Spinner) findViewById(R.id.spinner3);

        btn_choose1 = (Button) findViewById(R.id.button1);
        btn_choose2 = (Button) findViewById(R.id.button2);
        btn_choose3 = (Button) findViewById(R.id.button3);

        timer1 = new Timer();
        timer1.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(spinner1.getSelectedItemPosition() == (Arrays.asList(getResources().getStringArray(R.array.cincin_warna)).size()-1)){
                            spinner1.setSelection(0);
                        }else{
                            spinner1.setSelection(spinner1.getSelectedItemPosition()+1);
                        }
                    }
                });
            }
        },0,1000);

        btn_choose1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(timer1!=null){
                    timer1.cancel();
                }
            }
        });

        timer2 = new Timer();
        timer2.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(spinner2.getSelectedItemPosition() == (Arrays.asList(getResources().getStringArray(R.array.cincin_warna)).size()-1)){
                            spinner2.setSelection(0);
                        }else{
                            spinner2.setSelection(spinner2.getSelectedItemPosition()+1);
                        }
                    }
                });
            }
        },0,1000);

        btn_choose2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(timer2!=null){
                    timer2.cancel();
                }
            }
        });

        timer3 = new Timer();
        timer3.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(spinner3.getSelectedItemPosition() == (Arrays.asList(getResources().getStringArray(R.array.cincin_warna)).size()-1)){
                            spinner3.setSelection(0);
                        }else{
                            spinner3.setSelection(spinner3.getSelectedItemPosition()+1);
                        }
                    }
                });
            }
        },0,1000);

        btn_choose3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(timer3!=null){
                    timer3.cancel();
                }
            }
        });
于 2013-10-23T11:18:00.080 回答