0

我正在制作一个可以在屏幕上显示城市(目的地)选项的应用程序。这些城市是:

  1. 悉尼

  2. 墨尔本

  3. 达尔文

  4. 雅加达

  5. 马来西亚

在屏幕过程中,我放置了切换过程,因此它将首先切换悉尼然后墨尔本然后达尔文。切换过程将连续工作。

现在我想做一个“按钮”来帮助用户选择城市。城市将出现在旋转器上

例如:用户想选择墨尔本。

用户将等到“Melbourne”文本出现在屏幕上。

当屏幕上显示“Melbourne”时,他会直接单击“按钮”,然后“Melbourne”文本将出现在微调器上

*我不想使用文本视图。只是微调。

这是我的完整代码...

主要活动

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {
private static final String TAG = "Sample::Activity";

private MenuItem Sydney;
private MenuItem Melbourne;
private MenuItem Darwin;
private MenuItem Jakarta;
private MenuItem Malaysia;

private Spinner spinner1;
private Spinner spinner2;
private Spinner spinner3;
private Spinner spinner4;
private Button choose_button;
private Button price_button;

private ClassTwo classtwo;
FrameLayout frame;

@Override
public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        setContentView(classtwo);
        setContentView(R.layout.activity_main);

        choose_btn = (Button) findViewById(R.id.button1);
        choose_btn.setOnClickListener(this);

        addListenerOnButton();
        addListenerOnSpinnerItemSelection();

        frame = (FrameLayout) findViewById(R.id.preview);
        frame.addView(classtwo);
}

@Override
public void onClick(View v)
{
   // What should i write in this method?
}

public boolean onCreateOptionsMenu(Menu menu) {
    Log.i(TAG, "onCreateOptionsMenu");
   Sydney = menu.add("Sydney");
    Melbourne = menu.add("Melbourne");
    Darwin = menu.add("Darwin");
    Jakarta = menu.add("Jakarta");
    Malaysia = menu.add("Malaysia");
            return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Log.i(TAG, "Menu Item selected " + item);
    if (item == Sydney) {
        classtwo.setViewMode(ClassTwo.Sydney);
        new Thread(new Runnable() {
            public void run() {
                try {
                Thread.sleep(2000);
                onOptionsItemSelected(Melbourne);}
                catch(Exception ex){}}
            }).start();
    } else if (item == Melbourne) {
        classtwo.setViewMode(ClassTwo.Melbourne);
        new Thread(new Runnable() {
            public void run() {
                try {
                Thread.sleep(2000);
                onOptionsItemSelected(Darwin);}
                catch(Exception ex){}}
            }).start();
    } else if (item == Darwin) {
        classtwo.setViewMode(ClassTwo.Darwin);
        new Thread(new Runnable() {
            public void run() {
                try {
                Thread.sleep(2000);
                onOptionsItemSelected(Jakarta);}
                catch(Exception ex){}}
            }).start();
    } else if (item == Jakarta) {
        classtwo.setViewMode(ClassTwo.Jakarta);
        new Thread(new Runnable() {
            public void run() {
                try {
                Thread.sleep(2000);
                onOptionsItemSelected(Malaysia);}
                catch(Exception ex){}}
            }).start();
    } else if (item == tombolMalaysia) {
        classtwo.setViewMode(ClassTwo.Malaysia);
        new Thread(new Runnable() {
            public void run() {
                try {
                Thread.sleep(2000);
                onOptionsItemSelected(Sydney);}
                catch(Exception ex){}}
            }).start();
    }
            return true;
    }

// SPINNER PROGRAM

public void addListenerOnSpinnerItemSelection() {

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    spinner2.setOnItemSelectedListener(new CustomOnItemSelectedListener());
    spinner3 = (Spinner) findViewById(R.id.spinner3);
    spinner3.setOnItemSelectedListener(new CustomOnItemSelectedListener());
    spinner4 = (Spinner) findViewById(R.id.spinner4);
    spinner4.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}

// 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);
        spinner4 = (Spinner) findViewById(R.id.spinner4);

        price_button = (Button) findViewById(R.id.btnSubmit);

        price_button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                /* get city */
                String city1 = String.valueOf(spinner1.getSelectedItem());
                String city2 = String.valueOf(spinner2.getSelectedItem());
                String multiply = String.valueOf(spinner3.getSelectedItem());
                String percentage = String.valueOf(spinner4.getSelectedItem());
                /* get price */
                String price1 = getCityPrice(city1);
                String price2 = getCityPrice(city2);
                int multiply = getMultiply(multiply);
                int percentage = getPercentage(percentage);

                String newPrice = price1.concat(price2);
                Integer price = Integer.parseInt(newPrice);

                Toast.makeText(MainActivity.this,
                        "Result : " + "\n " + (price * multiply)

                        + " Ringgit +" + percentage + "%", Toast.LENGTH_LONG).show();

            }
        });

    }

    public String getCityPrice(String city) {
        String Price = "0";
        if (warna.equalsIgnoreCase("Sydney")) {
            Price = "0";
        } else if (warna.equalsIgnoreCase("Melbourne")) {
            Price = "1";
        } else if (warna.equalsIgnoreCase("Darwin")) {
            Price = "2";
        } else if (warna.equalsIgnoreCase("Jakarta")) {
            Price = "3";
        } else if (warna.equalsIgnoreCase("Malaysia")) {
            Price = "4";
        } 
        return Price;
    }

    public int getMultiply(String city) {
        int Price = 1;
        if (city.equalsIgnoreCase("Sydney")) {
            Price = 1;
        } else if (city.equalsIgnoreCase("Melbourne")) {
            Price = 10;
        } else if (city.equalsIgnoreCase("Darwin")) {
            Price = 100;
        } else if (city.equalsIgnoreCase("Jakarta")) {
            Price = 1000;
        } else if (city.equalsIgnoreCase("Malaysia")) {
            Price = 10000;
        } 
        return Price;
    }

    public int getPercentage(String city) {
        int Price = 0;
        if (city.equalsIgnoreCase("Sydney")) {
            Price = 5;
        } else if (city.equalsIgnoreCase("Malaysia")) {
            return 5;

        }
        return Price;
    }

Activity_Main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal|left"
    android:orientation="vertical" >

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="200dp"
    android:layout_height="40dp"
    android:layout_alignParentRight="true"
    android:entries="@array/city"
    />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="200dp"
    android:layout_height="40dp"
    android:layout_alignParentRight="true"
    android:entries="@array/city"
    />

<Spinner
    android:id="@+id/spinner3"
    android:layout_width="200dp"
    android:layout_height="40dp"
    android:layout_alignParentRight="true"
    android:entries="@array/city"
    />

<FrameLayout
    android:id="@+id/preview"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="110dp" >
</FrameLayout>

<Button
    android:id="@+id/btnSumbit"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/spinner4"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="86dp"
    android:text="@string/price_button" />

<Spinner
    android:id="@+id/spinner4"
    android:layout_width="200dp"
    android:layout_height="40dp"
    android:layout_alignParentRight="true"
    android:entries="@array/percentage"
    />

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/spinner4"
    android:layout_alignTop="@+id/btnSubmit"
    android:layout_marginRight="14dp"
    android:layout_toLeftOf="@+id/textView4"
    android:text="@string/choose_button" />

字符串.XML

 <?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">CityDestination</string>
    <string name="action_settings">Settings</string>
    <string name="tmbsubmit">price_button</string>
    <string name="button1">choose_button</string>

    <string-array name="city">
        <item>Sydney</item>
        <item>Melbourne</item>
        <item>Darwin</item>
        <item>Jakarta</item>
        <item>Malaysia</item>
    </string-array>
    <string-array name="percentage">
        <item>Sydney</item>
        <item>Malaysia</item>

    </string-array>
    </resources>

我的问题:

如何连接choose_button到微调器,以便当用户直接单击choose_button所选城市时会出现在微调器上?

4

2 回答 2

0

如何连接choose_button到微调器,以便当用户直接单击choose_button所选城市时会出现在微调器上?

Button 中有一个方法,OnclickListener您需要覆盖此方法,并在该方法中调用微调器的对象,当您单击 Button 时将打开微调器

并在您的按钮中使用微调器编辑的实例调用以下方法:虚拟按钮如下

private void DummyButton()

{
    DummyButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                 String selectedItemOfSpinner = (String) spinner.getSelectedItem();  //Click handler of your spinner
                //post your code here
            } catch (NoSuchAlgorithmException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }

    });
}
于 2013-10-14T08:29:55.160 回答
0

在您choose_button按下它时,您需要获取微调器的文本

@Override
public void onClick(View v) {
     **What should i write in this method?**
    Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
    String text = mySpinner.getSelectedItem().toString();

    //call required function
    //e.g. String str = getCityPrice...
    //now set this text wherever you need to 
}
于 2013-10-14T08:28:40.760 回答