如何在Android应用程序点击按钮,打开第二个活动 [英] How to open a second activity on click of button in android app

查看:153
本文介绍了如何在Android应用程序点击按钮,打开第二个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习构建Android应用程序,我需要一些具体的帮助。我似乎无法让我的头周围的模板code位,我需要改变,哪些位是静态的。

I am learning to build android applications and I need some specific help. I can't seem to get my head around which bits of template code I am required to change, and which bits are static.

布局文件夹中,我有我的 ACTIVITY_MAIN.XML 读取

In the LAYOUT folder I have my ACTIVITY_MAIN.XML which reads

 <?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 (这是.class文件?)这个读      包com.example.assent.bc;

I then have my MainActivity.java (is this the .class?) this reads package 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 文件,该文件是;

and then my SendPhotos.java file which is;

 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活动,只要打开了该活动,没有什么花哨,不发送任何数据或任何东西。

I would like the button in my main activity to link through to my sendphotos activity, simply opening up that activity, nothing fancy, not sending any data or anything.

我知道的地方,我需要我的

I know that somewhere I need my

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

但我不知道该怎么替换 ToActivity.class 或什么,我需要的地方。

but I have no idea what to replace ToActivity.class with or what else I need where.

推荐答案

您可以移动到所需的活动上按一下按钮。只需添加机器人:的onClick =的sendMessage此行

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

的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);
}

是啊不要忘记最重要的是要确定你的manifest.xml活动

Yeah and most important thing dont forget to define you activity in manifest.xml

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

这篇关于如何在Android应用程序点击按钮,打开第二个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆