0

我已经被这个愚弄了好几天,但无法让它工作,尽管它编译没有错误,并且 apk 安装在模拟器中。模拟器显示位图(res/drawable/scr0.png)正常,但没有按钮显示,点击时也没有响应。

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

//public class PageOne extends Activity implements View.OnClickListener {
public class PageOne extends Activity  {

Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page_one);
    setContentView(new myView(this));
    //addListenerOnButton();

}

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

private class myView extends View {

    public myView (Context context) {
        super (context);
        //setContentView(R.layout.activity_page_one);

        addListenerOnButton();
        //button.bringToFront();
    }

    @Override
    protected void onDraw (Canvas canvas) {

        this.setBackgroundResource(R.drawable.scr0);
        button.bringToFront();

    }

}// myView

public void addListenerOnButton() {
    final Context context = this;
    button = (Button) findViewById(R.id.button1);
    //button.setBackgroundColor(Color.TRANSPARENT);
    button.bringToFront();
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(context, PageTwo.class);
                        startActivity(intent);   
        }
    });

}// addListenerOnButton()


}// PageOne


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

<Button
    android:id="@+id/button1"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="42dp"
    android:text="bubkiss" />

</RelativeLayout>

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.yetanotherpageturner.PageOne"
        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="com.example.yetanotherpageturner.PageTwo"
        android:label="@string/app_name"
    </activity>
</application>

</manifest>
4

1 回答 1

0

试试这个:button.setOnClickListener(new View().OnClickListener()); 如果有帮助,请投票。

于 2013-03-23T06:43:44.907 回答