简体   繁体   English

我如何更改按钮点击android上的活动

[英]How do i change activity on a button click android

I have an ImageButton that when I want it to be pressed to launch a second Activity . 我有一个ImageButton ,当我希望按下它以启动第二个Activity The second activity is called "Dust 2" as seen in my android manifest 第二个活动称为“灰尘2”,如我的Android清单所示

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pete.smokesapp" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".homepage"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Dust2"
            android:label="@string/title_activity_dust2" >
        </activity>
    </application>
</manifest>

I tried following a guide online but got me as far as this stage now my app crashed on launch. 我尝试按照在线指南进行操作,但是到现在为止,我的应用在启动时崩溃了。

package pete.smokesapp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;


public class homepage extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_homepage);


    findViewById(R.id.imgDust2).setOnClickListener((View.OnClickListener)       this);

}

public void imgDust2(View view) {
switch (view.getId()){
    case R.id.imgDust2:
        Toast.makeText(this, "Dust 2 Clicked", Toast.LENGTH_LONG ).show();
        break;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_homepage, menu);
    return true;
}

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

Here is also my main activity, this is the activity with the ImageButton , i am also new to this so sorry if I seem clueless. 这也是我的主要活动,这是ImageButton活动,对此我也很ImageButton ,因此,如果我似乎一无所知,请对不起。

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Maps"
                android:id="@+id/txtMaps"
                android:layout_weight="0.44" />

            <ImageButton
                android:layout_width="350dp"
                android:layout_height="150dp"
                android:id="@+id/imgDust2"
                android:clickable="false"
                android:src="@drawable/dust2"
                android:layout_above="@+id/imgOverpass"
                android:layout_alignLeft="@+id/imgOverpass"
                android:layout_alignStart="@+id/imgOverpass"
                android:layout_marginBottom="10dp" />



            <ImageButton
                android:layout_width="350dp"
                android:layout_height="150dp"
                android:id="@+id/imgInferno"
                android:layout_below="@+id/txtOverpass"
                android:layout_alignLeft="@+id/imgOverpass"
                android:layout_alignStart="@+id/imgOverpass"
                android:layout_marginBottom="10dp"
                android:src="@drawable/inferno" />

            <ImageButton
                android:layout_width="350dp"
                android:layout_height="150dp"
                android:id="@+id/imgOverpass"
                android:src="@drawable/overpass"
                android:layout_marginBottom="10dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true" />

            <ImageButton
                android:layout_width="350dp"
                android:layout_height="150dp"
                android:id="@+id/imgMirage"
                android:clickable="false"
                android:src="@drawable/mirage"
                android:layout_above="@+id/imgOverpass"
                android:layout_alignLeft="@+id/imgOverpass"
                android:layout_alignStart="@+id/imgOverpass"
                android:layout_marginBottom="10dp" />

            <ImageButton
                android:layout_width="350dp"
                android:layout_height="150dp"
                android:id="@+id/imgCache"
                android:clickable="false"
                android:src="@drawable/cache"
                android:layout_above="@+id/imgOverpass"
                android:layout_alignLeft="@+id/imgOverpass"
                android:layout_alignStart="@+id/imgOverpass"
                android:layout_marginBottom="10dp" />

            <ImageButton
                android:layout_width="350dp"
                android:layout_height="150dp"
                android:id="@+id/imgNuke"
                android:clickable="false"
                android:src="@drawable/nuke"
                android:layout_above="@+id/imgOverpass"
                android:layout_alignLeft="@+id/imgOverpass"
                android:layout_alignStart="@+id/imgOverpass"
                android:layout_marginBottom="10dp" />

        </LinearLayout>
    </ScrollView>
</RelativeLayout>

There are two methods available in android using which you can go from one Activity to another. android有两种可用的方法,您可以使用它们从一个Activity转到另一个Activity。

1. Use button.setOnClickListener() 1.使用button.setOnClickListener()

Create a button in xml file. xml文件中创建一个按钮。

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

Now set event listener for the button in your .class file 现在为.class文件中的按钮设置事件监听器

Button button = (Button) findViewById(R.id.button);

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

        //set the event you want to perform when button is clicked
        //you can go to another activity in your app by creating Intent
        Intent intent = new Intent(getApplicationContext, Activity2.class);
        startActivity(intent);
    }
});

2. Use <android:onClick="goNext"> 2.使用<android:onClick="goNext">

Put the onClick as the attribute of the button you have created in xml file. 将onClick用作在xml文件中创建的按钮的属性。

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

Now in your .class file define an event for that button as, 现在,在您的.class文件中,为该按钮定义一个事件,

goNext() {
    Intent intent = new Intent(getApplicationContext, Activity2.class);
    startActivity(intent);
}

In your first activity do this: 在您的第一个活动中,请执行以下操作:

 Button button = (Button) findViewById(R.id.imgDust2);

button.setOnClickListener(new View.OnClickListener () {
@Override
public void onClick(View v){
Intent intent = new Intent(v.getContext(), dust2.class);
startActivity(intent);
}
});

Try the following: 请尝试以下操作:

  1. Add implements OnClickListener to your class definition: public class homepage extends ActionBarActivity implements OnClickListener implements OnClickListener添加到您的类定义中: public class homepage extends ActionBarActivity implements OnClickListener
  2. An error will appear and ask you to add unimplemented methods. 将会出现错误,并要求您添加未实现的方法。 Do so. 这样做。
  3. Change findViewById(R.id.imgDust2).setOnClickListener((View.OnClickListener) this); 更改findViewById(R.id.imgDust2).setOnClickListener((View.OnClickListener) this); to findViewById(R.id.imgDust2).setOnClickListener(this); findViewById(R.id.imgDust2).setOnClickListener(this);
  4. Place the stuff you want to do when the button is clicked into the new method that was added in step 2. 单击按钮时,将您要执行的操作放入步骤2中添加的新方法中。

Set onClick to the xml imageView and put the name of the method: 将onClick设置为xml imageView并放置方法的名称:

 <ImageButton
        android:layout_width="350dp"
        android:layout_height="150dp"
        android:id="@+id/imgDust2"
        android:clickable="false"
        android:src="@drawable/dust2"
        android:layout_above="@+id/imgOverpass"
        android:layout_alignLeft="@+id/imgOverpass"
        android:layout_alignStart="@+id/imgOverpass"
        android:layout_marginBottom="10dp"
        android:onClick="imgDust2"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在单击按钮时更改我的 android 活动? - 堆栈溢出 - How can I change my android activity on button click? - Stack Overflow 如何为Android中的活动分配按钮? - How do I assign a Button to an activity in Android? 在Android活动中更改单击按钮时的外观 - change the apperance of a button on click in Android Activity 如何在按钮上显示随机活动单击在Android Studio中 - How Can I Display Random Activity On Button Click In Android Studio 单击按钮如何打开新活动? - How do I open a new activity with a button click? 如何通过在 android 工作室中单击按钮从另一个活动中更改活动中的 AsyncTask 变量? - How to change a variable of an AsyncTask in an activity from another activity by a button click in android studio? 如何更改android中按钮的背景? - How do I change the background of a button in android? 如何获得在Android Studio中打开新活动的按钮? - How do i get a button to open a new activity in android studio? 如何更改Android清单以启动应用程序而不是活动? - How do I change the Android Manifest to launch an Application instead of an Activity? 如何在android studio中点击按钮时从另一个活动更改textview文本? - How to change textview text from another activity on button click in android studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM