创建按钮不会在点击时进入新活动

时间:2016-09-02 11:28:47

标签: android button android-activity onclick

我创建了4个按钮,但只有facebook登录按钮才会响应。

public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {

private Button buttonRegistrujSe, buttonPrijavaRegistrovaniKorisnik;
private LoginButton loginButton;
private CallbackManager callbackManager;
private TextView info;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_main);
    AppEventsLogger.activateApp(this);


    SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
    findViewById(R.id.sign_in_button).setOnClickListener(this);

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

    GoogleApiClient mGoogleApiClient;
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

    buttonRegistrujSe = (Button) (findViewById(R.id.button_registruj_se));
    buttonRegistrujSe.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, RegistrationActivity.class);
            startActivity(i);
        }
    });

    buttonPrijavaRegistrovaniKorisnik = (Button) (findViewById(R.id.button_prijava_registrovani_korisnik));
    buttonPrijavaRegistrovaniKorisnik.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i3 = new Intent(MainActivity.this, LoginActivity.class);
            startActivity(i3);
        }
    });

    callbackManager = CallbackManager.Factory.create();
    setContentView(R.layout.activity_main);
    info = (TextView)findViewById(R.id.info);
    loginButton = (LoginButton)findViewById(R.id.connectWithFbButton);

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

            info.setText(
                    "User ID: "
                            + loginResult.getAccessToken().getUserId()
                            + "\n" +
                            "Auth Token: "
                            + loginResult.getAccessToken().getToken()
            );

        }

        @Override
        public void onCancel() {

            info.setText("Login attempt canceled.");

        }

        @Override
        public void onError(FacebookException e) {

            info.setText("Login attempt failed.");

        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    System.exit(0);
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
public void onClick(View view) {

}
}

告诉我你是否需要按下所有那些按下时没有响应的按钮的代码?

这是主要活动的xml文件

<?xml version="1.0" encoding="utf-8"?>
<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"
    xmlns:fb="http://schemas.android.com/apk/res-auto"
    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="com.milos.sportisa.MainActivity">



    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Registruj se"
        android:id="@+id/button_registruj_se"
        android:layout_above="@+id/button_prijava_registrovani_korisnik"
        android:layout_alignEnd="@+id/sign_in_button"
        android:layout_alignRight="@+id/sign_in_button"
        />



    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Prijavi se kao registrovani korisnik"
        android:id="@+id/button_prijava_registrovani_korisnik"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/info"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textSize="18sp"
        />

    <com.facebook.login.widget.LoginButton
        android:id="@+id/connectWithFbButton"
        style="@style/com_facebook_loginview_default_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_above="@+id/sign_in_button"
        android:layout_centerHorizontal="true" />

    <com.google.android.gms.common.SignInButton
        android:id="@+id/sign_in_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="43dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />


</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

尝试在Override OnClick中实现方法。你正在课堂上实现时钟,所以你应该在那里实现这些方法。

@Override
public void onClick(View view) {
switch(view){
case buttonPrijavaRegistrovaniKorisnik: 
//Todo your o'clock code
break;

//todo fill all the buttons.

}

}

答案 1 :(得分:0)

而不是编写这行代码:

findViewById(R.id.sign_in_button).setOnClickListener(this);

您应该编写以下代码行:

signInButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i = new Intent(MainActivity.this, your_activity_name.class);
        startActivity(i);
    }
});

或者实现你的覆盖onClick()方法,如下所示:

@Override
public void onClick(View view) {
    switch (view.getId()) {
    case R.id.signInButton:
        Intent i = new Intent(MainActivity.this, your_activity_name.class);
        startActivity(i);
        break;
    }
}