使用按钮获取值从onOptionsItem选择 [英] Using Button to Get A Value From onOptionsItem Selected

查看:325
本文介绍了使用按钮获取值从onOptionsItem选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我作出应用程序,可以在屏幕上显示的选项水果。果实为:


  1. 苹果


  2. 葡萄


  3. 橙色


在屏幕上的过程,我把切换过程,所以它会切换:


  1. 苹果第一2秒钟


  2. 然后切换到葡萄2秒钟


  3. 然后切换橙色2秒钟


切换过程将持续工作。

现在我想打一个选择按钮和微调,因此用户可以选择水果会看到微调他的选择。

例如:
用户想要选择一个橘子。

用户将等到橙色显示在屏幕上。

当正在屏幕上显示的橙色,直接将他preSS选择按钮,然后在同一时间的Spinner1将被橙色值来填补。

*我不希望使用TextView的。

下面是我的code ...

主要活动

 私人菜单项苹果;
私人菜单项葡萄;
私人菜单项橙色;    私人微调微调;
    私人按钮choose_button;
    私人按钮btnPrice;@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    choose_button =(按钮)findViewById(R.id.button1);        addListenerOnButton();
        addListenerOnSpinnerItemSelection();    }@覆盖
公共无效的onClick(视图v)
{//我不知道我应该怎么写在这里@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    Log.i(TAGonCreateOptionsMenu);
    苹果= menu.add(苹果);
    葡萄= menu.add(葡萄);
    橙色= menu.add(橙色);@覆盖
公共布尔onOptionsItemSelected(菜单项水果){
    Log.i(TAG,菜单项中选择+水果);
    如果(水果==苹果){
        frame.setViewMode(classfruit.apple);
        新主题(新的Runnable接口(){
            公共无效的run(){
                尝试{
                视频下载(2000);
                onOptionsItemSelected(葡萄);}
                赶上(例外前){}}
            })。开始();
    }否则如果(水果==葡萄){
        frame.setViewMode(classfruit.grape);
        新主题(新的Runnable接口(){
            公共无效的run(){
                尝试{
                视频下载(2000);
                onOptionsItemSelected(橙色);}
                赶上(例外前){}}
            })。开始();
    }否则如果(水果==橙色){
        frame.setViewMode(classfruit.orange);
        新主题(新的Runnable接口(){
            公共无效的run(){
                尝试{
                视频下载(2000);
                onOptionsItemSelected(苹果);}
                赶上(例外前){}}
            })。开始();公共无效addListenerOnSpinnerItemSelection(){    spinner1 =(微调)findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(新CustomOnItemSelectedListener());//获取选中下拉列表中值
    公共无效addListenerOnButton(){        spinner1 =(微调)findViewById(R.id.spinner1);            btnP​​rice =(按钮)findViewById(R.id.btnPrice);btnSubmit.setOnClickListener(新OnClickListener(){@覆盖
公共无效的onClick(视图v){
/ *得到的果实* /
串果=将String.valueOf(spinner1.getSelectedItem());    / *获取价格* /
串价格=用getPrice(水果);公共字符串用getPrice(字符串水果){
        串价格=0;
        如果(fruit.equalsIgnoreCase(苹果)){
            价格=1;
        }否则如果(warna.equalsIgnoreCase(葡萄)){
            价格=2;
        }否则如果(warna.equalsIgnoreCase(橙色)){
            价格=3;
        }

的main.xml

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=MainActivity。><微调
    机器人:ID =@ + ID / spinner1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentLeft =真
    机器人:layout_alignParentTop =真
    机器人:项=@阵列/果
/><按钮
    机器人:ID =@ + ID /按钮1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_centerVertical =真
    机器人:文字=@字符串/ btn_choose/>

String.xml

 <字符串名称=btn_choose>选择与LT; /串><字符串数组名=果>
    <项目>苹果< /项目>
    <项目>葡萄< /项目>
    <项目>橙色与LT; /项目>
< /字符串数组>


解决方案

检查我的code:

activity_main.xml中:

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =match_parent
        机器人:方向=垂直
        机器人:比重=中心
        机器人:layout_height =match_parent>    <微调
            机器人:ID =@ + ID /飞旋
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT/>    <按钮
        机器人:ID =@ + ID /按钮
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=选择/>
< / LinearLayout中>

MainActivity:

 包com.example.MyTest;进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.ArrayAdapter;
进口android.widget.Button;
进口android.widget.Spinner;进口java.util.Arrays中;
进口java.util.Timer中;
进口java.util.TimerTask中;公共类MainActivity延伸活动{   私人微调微调;
   私人Button按钮;
   私人定时器定时器;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        微调=(微调)findViewById(R.id.spinner);
        按钮=(按钮)findViewById(R.id.button);        spinner.setAdapter(新ArrayAdapter(这一点,
                android.R.layout.simple_spinner_dropdown_item,
                Arrays.asList(getResources()getStringArray(R.array.fruit))));
        定时器=新定时器();
        timer.schedule(新的TimerTask(){
            @覆盖
            公共无效的run(){
                runOnUiThread(新的Runnable(){
                    @覆盖
                    公共无效的run(){
                        如果(spinner.getSelectedItemPosition()==(Arrays.asList(getResources()getStringArray(R.array.fruit))尺寸() - 1)){
                            spinner.setSelection(0);
                        }其他{
                            spinner.setSelection(spinner.getSelectedItemPosition()+ 1);
                        }
                    }
                });
            }
        },0,2000);        button.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(查看视图){
            如果(定时器!= NULL){
                timer.cancel();
            }
            字符串spinnerSelectedItem =(字符串)spinner.getSelectedItem();
            Toast.makeText(MainActivity.this,spinnerSelectedItem,Toast.LENGTH_LONG).show();
        }
    });
    }}

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>

解决方案

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

}

这篇关于使用按钮获取值从onOptionsItem选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆