Change a Public Float Value within a Menu [Android]
Basically I am making an app where there is a Ball On the screen and the size of the ball is determined by this code
private static final float sBallDiameter = 0.011f;
Is there any way i could create a menu system for the user , For example Like a pop up menu shown here
http://i.stack.imgur.com/WsHJX.png
That would change the size of the ball , For example where on the menu above it says Map , Traffic , Street View etc. - I could use Small, Medium, Large
Small, changing the sBallDiameter
to something like 0.009f
and large changing it to something like 0.018f
I hope you get what i mean , but if someone could assist me on creating this menu type thing it would be great !
**UPDATE :**
Here is the开发者_高级运维 current Menu
private static final int Small= 1;
public boolean onCreateOptionsMenu(Menu menu) {
((Activity) menu).onCreateOptionsMenu(menu);
menu.add(0, Small, 0, "Small");
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Small:
// here i want it to change the size down to something like 0.009f
return true;
}
return false;
}
public float sBallDiameter = 0.013f;
Basically - What i want to do is make it so when the case "Small" is selected - it decreases the size of "sBallDiameter"
- I hope that clears things up a bit more -
Use Enum instead of fixed size
public Enum BallSize{
SMALL(0.09f),MEDIUM(0.11f),LARGE(0.018f);
BallSize(float size)
{
this.size=size;
}
private final float size;
}
And after user changes menu compare the menu item with Ballsize
Here is how you create menus: Creating Menus.
Define you menu in a res/menu/ball_menu.xml file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_small"
android:icon="@drawable/ic_menu_small"
android:title="@string/small" />
</menu>
Then in your activity:
private static final float SMALL_SIZE = 0.1f;
private static final float BIG_SIZE = 0.3f;
private float mBallSize = SMALL_SIZE;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.ball_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_small:
setBallSize(SMALL_SIZE);
default:
return super.onOptionsItemSelected(item);
}
}
private void setBallSize(float size) {
this.mBallSize = size;
}
精彩评论