使用onClick Android Studio切换活动

使用onClick Android Studio切换活动,android,Android,我试图让我的button id=buttonToScreen2转到我的第二个活动屏幕名=FullscreenActivity2,但在我下面的代码中,我有一个错误 我的“主要”FullscreenActivity.java XML 从编译器错误来看,似乎您编写了错误的方法代码。 现在,正如您已经在xml布局文件中声明的那样,无需使用onClickListner再次写入按钮的onClick。简单的类级方法编写按钮的onClickToScreen2方法如下: 替换下面的代码行 Button goToS

我试图让我的button id=buttonToScreen2转到我的第二个活动屏幕名=FullscreenActivity2,但在我下面的代码中,我有一个错误

我的“主要”FullscreenActivity.java

XML


从编译器错误来看,似乎您编写了错误的方法代码。

现在,正如您已经在xml布局文件中声明的那样,无需使用
onClickListner再次写入按钮的onClick。简单的类级方法编写按钮的
onClickToScreen2
方法如下:

替换下面的代码行

Button goToScreen2Function = (Button)findViewById(R.id.buttonToScreen2);

goToScreen2Function.setOnClickListener(new View.OnClickListener() {
    public void onClickToScreen2(View v){
    Intent intent = new Intent(FullscreenActivity.this, FullscreenActivity2.class);
    startActivity(intent);
    }
});
下面是代码行

 public void onClickToScreen2(View v){
        Intent intent = new Intent(FullscreenActivity.this, FullscreenActivity2.class);
        startActivity(intent);
        }

在您的主要活动,即FullscreenActivity.java中,尝试使用以下代码:

    Button goToScreen2Function = (Button)findViewById(R.id.buttonToScreen2);

goToScreen2Function.setOnClickListener(new View.OnClickListener() {
    public void onClickToScreen2(View v){
    Intent intent = new Intent(this, FullscreenActivity2.class);
    startActivity(intent);
    }
});

它似乎在我的.java文件中的某个地方,请参阅
FullscreenActivity
的compilePost complete代码中的附加错误。当您将
onClickToScreen2
和按钮的
onClick
与监听器组合在一起时。很抱歉,您可以再解释一下这一行“当您将onClickToScreen2和按钮的onClick与监听器组合在一起时”。我应该使用onClick吗(视图v)相反,当我现在点击按钮时,请查看我的logcat问题though@Codeconuts-这两种方法都是可行的。使用其中一种形式可以简化代码并更好地理解。
//Line 123 is goToScreen2Function.setOnClickListener(new View.OnClickListener()
Error:(123, 43) error: <identifier> expected 
Error:(123, 44) error: illegal start of type
Error:(123, 47) error: ')' expected
Error:(123, 52) error: ';' expected
Error:(123, 53) error: invalid method declaration; return type required
Error:(124, 9) error: illegal start of expression
Error:(124, 16) error: illegal start of expression
Error:(124, 37) error: ';' expected
Error:(124, 44) error: ';' expected
Error:(128, 6) error: illegal start of type
import com.scorekeeper.util.SystemUiHider;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;


public class FullscreenActivity extends Activity {

private static final boolean AUTO_HIDE = true;

private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

private static final boolean TOGGLE_ON_CLICK = true;


private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;

private SystemUiHider mSystemUiHider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_fullscreen);

    final View controlsView = findViewById(R.id.fullscreen_content_controls);
    final View contentView = findViewById(R.id.fullscreen_content);

    // Set up an instance of SystemUiHider to control the system UI for
    // this activity.
    mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
    mSystemUiHider.setup();
    mSystemUiHider
            .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
                // Cached values.
                int mControlsHeight;
                int mShortAnimTime;

                @Override
                @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
                public void onVisibilityChange(boolean visible) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
                        // If the ViewPropertyAnimator API is available
                        // (Honeycomb MR2 and later), use it to animate the
                        // in-layout UI controls at the bottom of the
                        // screen.
                        if (mControlsHeight == 0) {
                            mControlsHeight = controlsView.getHeight();
                        }
                        if (mShortAnimTime == 0) {
                            mShortAnimTime = getResources().getInteger(
                                    android.R.integer.config_shortAnimTime);
                        }
                        controlsView.animate()
                                .translationY(visible ? 0 : mControlsHeight)
                                .setDuration(mShortAnimTime);
                    } else {
                        // If the ViewPropertyAnimator APIs aren't
                        // available, simply show or hide the in-layout UI
                        // controls.
                        controlsView.setVisibility(visible ? View.VISIBLE : View.GONE);
                    }

                    if (visible && AUTO_HIDE) {
                        // Schedule a hide().
                        delayedHide(AUTO_HIDE_DELAY_MILLIS);
                    }
                }
            });

    // Set up the user interaction to manually show or hide the system UI.
    contentView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (TOGGLE_ON_CLICK) {
                mSystemUiHider.toggle();
            } else {
                mSystemUiHider.show();
            }
        }
    });

    // Upon interacting with UI controls, delay any scheduled hide()
    // operations to prevent the jarring behavior of controls going away
    // while interacting with the UI.
    findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);
}

Button goToScreen2Function = (Button)findViewById(R.id.buttonToScreen2);

goToScreen2Function.setOnClickListener(new View.OnClickListener() {
    public void onClickToScreen2(View v){
    Intent intent = new Intent(FullscreenActivity.this, FullscreenActivity2.class);
    startActivity(intent);
    }
});

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);

    // Trigger the initial hide() shortly after the activity has been
    // created, to briefly hint to the user that UI controls
    // are available.
    delayedHide(100);
}

View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (AUTO_HIDE) {
            delayedHide(AUTO_HIDE_DELAY_MILLIS);
        }
        return false;
    }
};

Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
    @Override
    public void run() {
        mSystemUiHider.hide();
    }
};

private void delayedHide(int delayMillis) {
    mHideHandler.removeCallbacks(mHideRunnable);
    mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
}
Button goToScreen2Function = (Button)findViewById(R.id.buttonToScreen2);

goToScreen2Function.setOnClickListener(new View.OnClickListener() {
    public void onClickToScreen2(View v){
    Intent intent = new Intent(FullscreenActivity.this, FullscreenActivity2.class);
    startActivity(intent);
    }
});
 public void onClickToScreen2(View v){
        Intent intent = new Intent(FullscreenActivity.this, FullscreenActivity2.class);
        startActivity(intent);
        }
    Button goToScreen2Function = (Button)findViewById(R.id.buttonToScreen2);

goToScreen2Function.setOnClickListener(new View.OnClickListener() {
    public void onClickToScreen2(View v){
    Intent intent = new Intent(this, FullscreenActivity2.class);
    startActivity(intent);
    }
});