如何开始button点击新的活动
在Android应用程序中,如何在单击另一个活动中的button时启动新的活动(GUI),以及如何在这两个活动之间传递数据?
简单。
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class); myIntent.putExtra("key", value); //Optional parameters CurrentActivity.this.startActivity(myIntent); 通过以下方式检索另一边的其他内容:
 @Override protected void onCreate(Bundle savedInstanceState) { Intent intent = getIntent(); String value = intent.getStringExtra("key"); //if it's a string you stored. } 
不要忘记在AndroidManifest.xml中添加新的活动:
 <activity android:label="@string/app_name" android:name="NextActivity"/> 
创buildViewPerson活动的意图并传递PersonID(例如,用于数据库查找)。
 Intent i = new Intent(getBaseContext(), ViewPerson.class); i.putExtra("PersonID", personID); startActivity(i); 
然后在ViewPerson Activity中,你可以得到一堆额外的数据,确保它不为空(如果你有时不传递数据的话),然后获取数据。
 Bundle extras = getIntent().getExtras(); if(extras !=null) { personID = extras.getString("PersonID"); } 
现在,如果您需要在两个活动之间共享数据,您也可以拥有一个全局单例。
 public class YourApplication extends Application { public SomeDataClass data = new SomeDataClass(); } 
然后通过以下方式在任何活动中致电
 YourApplication appState = ((YourApplication)this.getApplication()); appState.data.CallSomeFunctionHere(); // Do whatever you need to with data here. Could be setter/getter or some other type of logic 
当用户点击button时,直接在XML内部:
 <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextButton" android:onClick="buttonClickFunction"/> 
 使用属性android:onClick我们声明父级活动中必须存在的方法名称。 所以我必须像这样在我们的活动中创build这个方法: 
 public void buttonClickFunction(View v) { Intent intent = new Intent(getApplicationContext(), Your_Next_Activity.class); startActivity(intent); } 
 目前的反应非常好,但初学者需要更全面的回答。 在Android中有三种不同的方式来开始新的活动,他们都使用Intent类;  意图|  Android开发者 。 
-  使用Button的onClick属性。 (初学者)
-  通过一个匿名类来分配一个OnClickListener()。 (中间)
-  使用switch语句的活动宽接口方法。 (专业版)
这里是我的例子的链接,如果你想跟着: https : //github.com/martinsing/ToNewActivityButtons
  1.使用Button的onClick属性。  (初学者) 
 button在.xml文件中有一个onClick属性: 
 <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="goToAnActivity" android:text="to an activity" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="goToAnotherActivity" android:text="to another activity" /> 
在Java类中:
 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); } public void goToAnActivity(View view) { Intent Intent = new Intent(this, AnActivity.class); startActivity(Intent); } public void goToAnotherActivity(View view) { Intent Intent = new Intent(this, AnotherActivity.class); startActivity(Intent); } 
优势:容易上手,模块化,并可以轻松地设置多个onClicks同样的意图。
缺点:审查时可读性差。
  2.通过一个匿名类来分配一个OnClickListener() 。  (中间) 
 这是当你为每个button设置一个单独的setOnClickListener() ,并用它自己的意图覆盖每个onClick() 。 
在Java类中:
 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent Intent = new Intent(view.getContext(), AnActivity.class); view.getContext().startActivity(Intent);} }); button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent Intent = new Intent(view.getContext(), AnotherActivity.class); view.getContext().startActivity(Intent);} }); 
优点:易于飞行。
缺点:会有很多匿名的课程,在阅读时会使阅读变得困难。
  3.使用switch语句的活动宽接口方法。  (专业版) 
 这是当你在onClick()方法中使用switch语句来pipe理所有Activity的button的时候。 
在Java类中:
 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button1.setOnClickListener(this); button2.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.button1: Intent intent1 = new Intent(this, AnActivity.class); startActivity(intent1); break; case R.id.button2: Intent intent2 = new Intent(this, AnotherActivity.class); startActivity(intent2); break; default: break; } 
 优点:简单的buttonpipe理,因为所有的button意图都在一个onClick()方法中注册 
对于问题的第二部分,传递数据,请参阅如何在Android应用程序的活动之间传递数据?
 Intent iinent= new Intent(Homeactivity.this,secondactivity.class); startActivity(iinent); 
  Intent in = new Intent(getApplicationContext(),SecondaryScreen.class); startActivity(in); This is an explicit intent to start secondscreen activity. 
灵光,
我认为额外的信息应该放在开始活动之前,否则数据将无法使用,如果你在NextActivity的onCreate方法访问它。
 Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class); myIntent.putExtra("key", value); CurrentActivity.this.startActivity(myIntent); 
从发送活动尝试下面的代码
  //EXTRA_MESSAGE is our key and it's value is 'packagename.MESSAGE' public static final String EXTRA_MESSAGE = "packageName.MESSAGE"; @Override protected void onCreate(Bundle savedInstanceState) { .... //Here we declare our send button Button sendButton = (Button) findViewById(R.id.send_button); sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //declare our intent object which takes two parameters, the context and the new activity name // the name of the receiving activity is declared in the Intent Constructor Intent intent = new Intent(getApplicationContext(), NameOfReceivingActivity.class); String sendMessage = "hello world" //put the text inside the intent and send it to another Activity intent.putExtra(EXTRA_MESSAGE, sendMessage); //start the activity startActivity(intent); } 
从接收活动中尝试以下代码:
  protected void onCreate(Bundle savedInstanceState) { //use the getIntent()method to receive the data from another activity Intent intent = getIntent(); //extract the string, with the getStringExtra method String message = intent.getStringExtra(NewActivityName.EXTRA_MESSAGE); 
然后,将以下代码添加到AndroidManifest.xml文件中
  android:name="packagename.NameOfTheReceivingActivity" android:label="Title of the Activity" android:parentActivityName="packagename.NameOfSendingActivity" 
 Intent i = new Intent(firstactivity.this, secondactivity.class); startActivity(i); 
开始新活动的方法是广播一个意图,并且可以使用一种特定的意图来将数据从一个活动传递到另一个活动。 我的build议是,您可以查看与开发者意图相关的Android开发人员文档; 这是关于这个问题的丰富信息,也有例子。
你可以试试这个代码:
 Intent myIntent = new Intent(); FirstActivity.this.SecondActivity(myIntent); 
试试这个简单的方法。
 startActivity(new Intent(MainActivity.this, SecondActivity.class)); 
 从另一个活动开始一个活动是在android应用程序开发中非常常见的情况。 要开始一个活动,你需要一个Intent对象。 
intent对象在其构造函数中使用两个参数
- 上下文
- 要开始的活动的名称 。
 所以举个例子,如果你有两个活动,比如说HomeActivity和DetailActivity ,你想从HomeActivity (HomeActivity  – > DetailActivity)启动DetailActivity 。 
这里是显示如何从HomeActivity启动DetailActivity的代码片段。
 Intent i = new Intent(HomeActivity.this,DetailActivity.class); startActivity(i); 
你完成了。
回到button点击部分。
 Button button = (Button) findViewById(R.id.someid); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent i = new Intent(HomeActivity.this,DetailActivity.class); startActivity(i); } }); 
实现View.OnClickListener接口并重写onClick方法。
 ImageView btnSearch; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search1); ImageView btnSearch = (ImageView) findViewById(R.id.btnSearch); btnSearch.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnSearch: { Intent intent = new Intent(Search.this,SearchFeedActivity.class); startActivity(intent); break; } 
从这个活动开始另一个活动,你也可以通过Bundle对象传递参数。
 Intent intent = new Intent(getBaseContext(), YourActivity.class); intent.putExtra("USER_NAME", "xyz@gmail.com"); startActivity(intent); 
在另一个活动中回顾数据(YourActivity)
 String s = getIntent().getStringExtra("USER_NAME"); 
虽然已经提供了适当的答案,但我在这里是为了在Kotlin语言中search答案。 这个问题不是关于语言特定的,所以我添加了代码来完成Kotlin语言的这个任务。
这里是你如何在Kotlin为andorid做到这一点
 testActivityBtn1.setOnClickListener{ val intent = Intent(applicationContext,MainActivity::class.java) startActivity(intent) }