为Onclick()侦听器准备一个按钮

时间:2014-03-19 08:27:18

标签: android onclick

尝试在当前代码中添加onclick侦听器按钮时出错。我找到代码的原始帖子如下所示。非常感谢您的帮助。我对Java感到满意,但仍然无法使用Eclipse应用程序进行导航。如果有人有任何文件可以帮助我更好地帮助我做好准备。

android eclipse button OnClick event

MainActivity.java

Button btn = (Button) findViewById(R.id.btnPlay);

    btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            myClick(v); /* my method to call new intent or activity */
        }
    });

    public void myClick(View v) {
        Intent intent = new Intent(**this, Swipe.class**);
        startActivity(intent);// for calling the activity
    }

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.apptest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.apptest.MainActivityAppTest"
            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=".Swipe"
    android:screenOrientation="landscape" >
</activity>

    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

我认为你收到编译错误了吗?

在这一行Intent intent = new Intent(**this, Swipe.class**);

您应该输入Intent intent = new Intent(MainActivity.this, Swipe.class);

应该有效

相关问题