Using Button to Get A Value From onOptionsItem Sel

2019-06-04 21:17发布

问题:

I'm making an app that can show Fruit Options on screen. The fruits are:

  1. Apple

  2. Grape

  3. Orange

On screen process, i put switching process, so it will switch:

  1. Apple first for 2 secs

  2. Then switch to Grape for 2 secs

  3. Then Switch Orange for 2 secs

The switching process will work continuously.

Now i want to make a "Choose Button" and a "Spinner" so the user can choose a fruit the will see his choice on "spinner".

For Example: A user wants to choose an orange.

The user will wait till the "Orange" is shown on screen.

When "Orange" is being shown on the screen, directly he will press "Choose Button" then at the same time the "Spinner1" will be filled by "Orange Value".

*I don't want to use TextView.

Here is my code...

MAIN ACTIVITY

    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";
        }

Main.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" />

String.xml

<string name="btn_choose">Choose</string>

<string-array name="fruit">
    <item>Apple</item>
    <item>Grape</item>
    <item>Orange</item>
</string-array>

回答1:

Check my code:

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

MainActivity:

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();
        }
    });
    }

}



回答2:

Thank you very much Haresh, my program is on the half way to work well.....

Next how to set this timers to be like this..

when app is started, Only Spinner1 is running( for now when app is started all spinners are running),

then when i click btn_choose1 then the Spinner1 is stop then Spinner2 is starting till btn_choose2 is clicked again then it is stop.

here is my code...

// 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();
                }
            }
        });