Using Button to Get A Value From onOptionsItem Selected

谁说胖子不能爱 提交于 2019-12-06 16:24:03
Haresh Chhelana

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

}

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();
                }
            }
        });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!