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

时间:2015-02-13 14:52:20

标签: java android android-activity

我有一个ImageButton当我希望按下它以启动第二个Activity时。第二项活动被称为"尘埃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>

我尝试在网上关注指南但是现在我的应用程序在启动时崩溃了。

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

这也是我的主要活动,这是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>

4 个答案:

答案 0 :(得分:1)

Android中有两种可用的方法,您可以从一个Activity转到另一个Activity。

1。使用button.setOnClickListener()

xml文件中创建一个按钮。

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

现在为.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。使用<android:onClick="goNext">

将onClick作为您在xml文件中创建的按钮的属性。

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

现在,在您的.class文件中,将该按钮的事件定义为

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

答案 1 :(得分:0)

在您的第一个活动中执行此操作:

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

答案 2 :(得分:0)

尝试以下方法:

  1. implements OnClickListener添加到您的班级定义中:public class homepage extends ActionBarActivity implements OnClickListener
  2. 将出现错误并要求您添加未实现的方法。这样做。
  3. findViewById(R.id.imgDust2).setOnClickListener((View.OnClickListener) this);更改为findViewById(R.id.imgDust2).setOnClickListener(this);
  4. 单击按钮时将要执行的操作放入步骤2中添加的新方法。

答案 3 :(得分:0)

将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"/>