Choosing Between Intents(Button Clicks) - Android App Development
I am new to Android application development, and I've been working on a simple FlashLight app that switches between various colors on the screen. The initial activity draws up a layout with only 2 buttons, labeled Green and Blue. I've installed Click Listeners on both buttons and set intents on them both in order for each to load it's corresponding activity, but when I run the app I can only go from the first view to ONE of the other views(Green OR Blue, but not both). I want to be able to choose EITHER button and load the next activity, but I'm a bit lost. Maybe create a boolean that determines which button the user clicked? IDK. This may sound a bit confusing as I'm not good at describing technical things like this, but here's my code below.
package com.jbsoft.SimpleFlashlight;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.widget.Button;
import android.widget.Toast;
public class SimpleFlashLightActivity extends Activity {
Button GreenButton;
Button BlueButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
BlueButton = (Button) findViewById(R.id.bluebutton);
BlueButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent blueintent = new Intent(SimpleFlashLightActivity.this,
BlueFlashLightActivity.class);
startActivity(blueintent);
Toast.makeText(v.getContext(), "SWITCH COLOR!",
Toast.LENGTH_LONG);
GreenButton = (Button) findViewById(R.id.bluebutton);
GreenButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent greenintent = new Intent(
SimpleFlashLightActivity.this,
GreenFlashLightActivty.class);
startActivity(greeninten开发者_运维技巧t);
}
});
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.list_menu, menu);
return true;
}
}
you are setting the onclicklistener for the greenbutton in the onclick event of the bluebotton. so the listener gets set only once the user clicks the bluebotton. or am i missing something here?
set the onclick listener for the greenbutton outside of the listnere for the bluebutton and it should work..
精彩评论