LightBlog

lundi 28 mars 2016

Mult Btn to Mult Activities Help

Hey Guys,

I'm pretty new to all this and slowly learning little by little.
This is my 1st post to any android forum about developing and I've started a little project to help me learn as I go.

I'm looking to work up a basic "step by step" app. (added a quick pic to show what I'm talking about somewhat)
So it won't let me add photos yet.
I would like to have MainActivity with several buttons: Sandwich, Burger and Hotdog for this example.
Click on Sandwich and it opens up to a "Sandwich Main" which would have additional buttons: Ham, Turkey
Click on Ham and we move to "Step 1" of the how-to... Get two pieces of bread.
Next Button takes you to step #2 and so on and so on...

Just a pretty dumb and I hope simple project I can use to learn some basics.

I used the following when I was testing a single button:

public void buttonOnClick (View view) {
Button button = (Button) view;
startActivity (new Intent(getApplicationContext(), Main2Activity.class));
}

This was straight forward easy way to get a single button to move to a new activity.
Now when I attempted to add a second button to the mix, it didn't work.
If there is a way to easily add multiple button to the above, let me know please... I found this way to be easiest for me to get so far.

So next I tried to go about it at a different angle:

activity_main.xml:

<RelativeLayout xmlns:android=".... (WON'T LET ME TYPE LINKS)

android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="104dp"
android:layout_marginTop="20dp"
android:text="Button1" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="34dp"
android:text="Button2" />

MainActivity.java:

public class MainActivity extends Activity implements OnClickListener {
Button b1,b2,
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b1 = (Button)findViewById( R.id.button1);
b2 = (Button)findViewById(R.id.button2);

b1.setOnClickListener(this);
b2.setOnClickListener(this);

}
@override
public void onClick(View v) {

switch (v.getId()) {
case R.id.button1:
Toast.makeText(getApplicationContext(), "button1 is working", 3000).show();
break;

case R.id.button2:
Toast.makeText(getApplicationContext(), "button2 is working", 3000).show();
break;

}

}
}

By doing the above I end up with the activity2 button clicking to activity3 first rather than activity2.
Activity3 button clicked correctly to activity3.
Another quick pic below to show what is going on... (whoops, won't let me post pics until I have 10 posts)

*MainActivity (BtnActivity2) and (BtnActivity3)
--- BtnActivity2 > opens Activity3 - (press phone back button) > Activity2 - (press back button again) > MainActivity
--- BtnActivity3 > opens Activity3 - (press phone back button) > Main Activity *** WORKING CORRECTLY ****

Any help would be appreciated!


from xda-developers http://ift.tt/1UXA590
via IFTTT

Aucun commentaire:

Enregistrer un commentaire