开发者

How to connect two activities

开发者 https://www.devze.com 2023-01-27 09:04 出处:网络
I have made one screen with two images and I would like to add a button lower on the pag开发者_运维问答e which will navigate to a second page when I click it. Do you know how to code this? I know how

I have made one screen with two images and I would like to add a button lower on the pag开发者_运维问答e which will navigate to a second page when I click it. Do you know how to code this? I know how to create a button but I don't know how to connect the two screens!


This task is accomplished with the startActivity(); method using Intents.

Intent i = new Intent(FromActivity.this, ToActivity.class);
startActivity(i);

In this case the Intent uses your current Activity as the Context in the first parameter, and the destination Activity in the second parameter.

Make sure that you add your second Activity to the manifest also (it resides in the tag)!

<activity android:name=".ToActivity"
          android:label="@string/app_name">
</activity>


To sum it up:

ImageView myImage = (ImageView) findViewById(R.id.image);
myImage.setOnClickListener(new OnClickListener() {
        @Override
        onClick(View v) {
            Intent intent = new Intent(FromActivity.this, ToActivity.class);
            startActivity(intent);
        }
    }
);


Intent intent = new Intent(currentActivity.this,nextActivity.class);

this.finish();

startActivity(intent);


Button start_button=(Button)findViewById(R.id.btnsend);

start_button.setonClickListener(new onClickListener( ){

    @override

    onClick(View view){

        Intent i = new Intent(MainActivity.this, NewActivity.class);

        startActivity(i);

    }

}

);


Lets break the answer in two parts, XML & JAVA part as every activity has each of these two. Assuming we are having only two activity, 'Activity1' being the one with the button which would redirect user to 'Activity2'. As we have 2 activity, we would be having 4 files related for these 2 activity.

XML

so lets first do the easy way, as soon as you open the .xml file of Activity1, you should shift to design tab.

After reaching the design part you can insert a button from pallet, now you can select button which is inside your layout. After selection you could see the properties of button in right section of your screen where you can effectly change multiple properties of the button.

Here you shall find the "onClick" option, fill the box next to it with anything very simple, or something which you can remember. For example enter "nextAct"

or

Hard way would be entering the onClick property manually by typing follwing line in button code in XML

android:onClick="nextAct"

This is all on XML part.

JAVA

Open the .java file of Activity1, here you have to make a new method. This method should be named same as in the "onClick" property of button. Over here i would be taking "nextAct" as that is what i had used in XML. You can place this new method anywhere inside the class of the java file, i prefer keeping it at the end of the class as i could easily locate it if any issue in future.

Now you have to write the body of nextAct method. This can be sumed up in these two lines

public void nextAct(View v){
Intent i = new Intent(this, Activity2.class);
startActivity(i);
}

After this both should be connected and working fine.


give id to your button and mention it in your MainActivity.class.Then you can call OnClickListener to listen to your click.

Button mButton = (Button)findViewById(R.id.buttonid);
mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

   //you can use anything in place of i
           Intent i = new Intent(MainActivity.this, NextActivity.class);
           startActivity(i);

         }
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消