我正在制作一个可以在屏幕上显示城市(目的地)选项的应用程序。这些城市是:
悉尼
墨尔本
达尔文
雅加达
马来西亚
在屏幕过程中,我放置了切换过程,因此它将首先切换悉尼然后墨尔本然后达尔文。切换过程将连续工作。
现在我想做一个“按钮”来帮助用户选择城市。城市将出现在旋转器上
例如:用户想选择墨尔本。
用户将等到“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
所选城市时会出现在微调器上?