如何打开第二个活动的点击按钮在安卓应用程序

我正在学习构建 Android 应用程序,我需要一些具体的帮助。我似乎无法理解哪些模板代码需要修改,哪些是静态的。

布局文件夹中,我有我的 ACTIVITY _ Main. XML,它读取

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">


<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/main_buttons_photos" />


</LinearLayout>

接下来,我有我的第二个活动 ACTIVITY _ SEND _ Photos. XML

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >


<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".SendPhotos" />


<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/title_activity_send_photos"
android:textAppearance="?android:attr/textAppearanceLarge" />


</RelativeLayout>

然后我有我的 MainActivity.java(这是。类?)这读 包 com.example.assent.bc;

 import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;


public class MainActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
}

然后我的 SendPhotos.java文件是;

 package com.example.assent.bc;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;


public class SendPhotos extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_photos);
getActionBar().setDisplayHomeAsUpEnabled(true);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_send_photos, menu);
return true;
}




@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}


}

我想在我的主活动中的按钮链接到我的 sendphotos 活动,只是打开该活动,没有花哨,没有发送任何数据或任何东西。

我知道在某个地方我需要我的

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

但是我不知道用什么来代替 ToActivity.class,也不知道在哪里我还需要什么。

371163 次浏览

From Activity : where you are currently ?

To Activity : where you want to go ?

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

Both Activity must be included in manifest file otherwise it will not found the class file and throw Force close.

Edit your Mainactivity.java

crate button's object;

now Write code for click event.

        Button btn = (button)findViewById(R.id.button1);
btn.LoginButton.setOnClickListener(new View.OnClickListener()
{


@Override
public void onClick(View v)
{
//put your intent code here
}
});

Hope it will work for you.

Replace the below line code:

import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener{
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(this);
}
public void onClick(View v) {
if(v.getId==R.id.button1){
Intent i = new Intent(FromActivity.this, ToActivity.class);
startActivity(i);
}
}
}

Add the below lines in your manifest file:

   <activity android:name="com.packagename.FromActivity"></activity>
<activity android:name="com.packagename.ToActivity"></activity>

Try this

  Button button;


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(),SendPhotos.class);
startActivity(i);
}
});


}

just follow this step (i am not writing code just Bcoz you may do copy and paste and cant learn)..

  1. first at all you need to declare a button which you have in layout

  2. Give reference to that button by finding its id (using findviewById) in oncreate

  3. setlistener for button (like setonclick listener)

  4. last handle the click event (means start new activity by using intent as you know already)

  5. Dont forget to add activity in manifest file

BTW this is just simple i would like to suggest you that just start from simple tutorials available on net will be better for you..

Best luck for Android

You can move to desired activity on button click. just add this line.

android:onClick="sendMessage"

xml:

 <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="@string/button" />

In your main activity just add this method:

public void sendMessage(View view) {
Intent intent = new Intent(FromActivity.this, ToActivity.class);
startActivity(intent);
}

And the most important thing: don't forget to define your activity in manifest.xml

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

Replace your MainActivity.class with these code

public class MainActivity extends Activity {


Button b1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.onClickListener()
{
public void onClick(View v)
{
Intent i=new Intent(getApplicationContext(),SendPhotos.class);
startActivity(i);
}
}
)
}

Add these Code in your AndroidManifest.xml after the </activity> and Before </application>

<activity android:name=".SendPhotos"></activity>
Button btn = (Button) findViewById(R.id.button1);


btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, MainActivity2.class);
MainActivity.this.startActivity(myIntent);
}
});

The answer for the complete noob from a complete noob: MainActivity is the name of the first activity. MainActivity2 is the name of the second activity. button1 is the I.D of the button in xml for MainActivity Activity.

This always works, either one should be just fine:

@Override
public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);


setContentView(R.layout.activity_main);


Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
startActivity(new Intent("com.tobidae.Activity1"));
}
//** OR you can just use the one down here instead, both work either way
@Override
public void onClick (View v){
Intent i = new Intent(getApplicationContext(), ChemistryActivity.class);
startActivity(i);
}
}
}

If you have two buttons and have the same id call to your button click events like this:

Button btn1;
Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


btn1= (Button)findViewById(R.id.button1);


btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,target.class);
startActivity(intent);
}
});


btn2=(Button) findViewById(R.id.button1);//Have same id call previous button---> button1


btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


}
});

When you clicked button1, button2 will work and you cannot open your second activity.

 <Button
android:id="@+id/btnSignIn"
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:background="@drawable/circal"
android:text="Sign in"
android:textColor="@color/white"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etPasswordLogin" />

IN JAVA CODE

Button signIn= (Button) findViewById(R.id.btnSignIn);


signIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(SignInPage.this,MainActivity.class));
}
});

}

add below code to activity_main.xml file:

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonClick"
android:text="@string/button" />

and just add the below method to the MainActivity.java file:

public void buttonClick(View view){
Intent i = new Intent(getApplicationContext()SendPhotos.class);
startActivity(i);
}

You can move to desired activity on button click. just add
android:onClick="timerApp"this line.

xml:


<Button
android:id="@+id/timer_app"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="timerApp"
android:text="Click To run Timer Activity" />

In your main activity just add this method:

 public void timerApp(View view){
Intent intent= new Intent(MainActivity.this,TimerActivity.class);
startActivity(intent);
}

OR in onCreate() method add below code

Button btn =findViewById(R.id.timer_app);//Don't need to type casting in android studio 3


btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, TimerActivity.class);
startActivity(intent);
}
});