使用onClick Android Studio切换活动

时间:2015-04-30 21:21:29

标签: android

我正在尝试将我的按钮id = buttonToScreen2转到我的第二个活动屏幕名称= FullscreenActivity2,但在我的代码中的某处我有错误。

  

我的“主要”FullscreenActivity.java

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

XML

<Button android:id="@+id/buttonToScreen2" style="?metaButtonBarButtonStyle"
            android:layout_width="0dp" android:layout_height="wrap_content"
            android:layout_weight="1" android:text="@string/titleScreen1"
            android:onClick="onClickToScreen2"/>
  

清单

<activity
    android:name=".FullscreenActivity2"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:label="@string/title_activity_fullscreen_activity2"
    android:theme="@style/FullscreenTheme" >
</activity>

Android Dev新手,尝试按照教程和答案但没有奏效。 How do I get a button to open another activity in Android Studio? 提前谢谢!

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

完整代码       包com.scorekeeper;

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

3 个答案:

答案 0 :(得分:1)

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

现在,由于您已经在xml布局文件中声明了android:onClick="onClickToScreen2"按钮的onClick,因此无需再次使用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);
        }

答案 1 :(得分:0)

在您的主要活动中,即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);
    }
});

答案 2 :(得分:0)

伙计们,您可以通过onclick方法轻松地从当前活动跳转到新活动,您只需要使用意图即可,代码行应类似于下面给出的代码,或者您可以直接转到他们的youtube链接已经正确解释了

https://www.youtube.com/watch?v=5HbBuxZvWeM

ps:这不是我的频道,所以我没有晋级。

`     按钮按钮;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button=findViewById(R.id.click);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i=new Intent(MainActivity.this,Second.class);
            startActivity(i);
        }
    });
}

} `