我正在创建一个可以在屏幕上显示城市(目的地)选项的应用。城市是:
悉尼
墨尔本
达尔文
雅加达
马来西亚
在屏幕显示过程中,我将切换过程设置为首先切换悉尼然后切换墨尔本然后切换到达尔文。切换过程将持续进行。
现在我想制作一个“按钮”,帮助用户选择城市。这个城市将出现在一个旋转器上
例: 用户想要选择墨尔本。
用户将等到屏幕上出现“墨尔本”文字。
当屏幕上显示“墨尔本”时,他会直接点击“按钮”,然后“墨尔本”文字将出现在微调器上
*我不想使用文本视图。只是Spinner。
这是我的完整代码......
MainActivity
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" />
STRING.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
所选择的城市将会出现在微调器上时?
答案 0 :(得分: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
}
答案 1 :(得分:0)
如何将choose_button
连接到微调器上,以便当用户直接点击choose_button
所选择的城市将会出现在微调器上时?
Button OnclickListener
中有一个方法需要覆盖此方法,并且在该方法中调用微调器的对象,当您单击Button
并在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();
}
}
});
}