[英]onClick Method not working Android
我有一個帶有兩個按鈕的LandingPage,每個按鈕都指向一個不同的活動。.onClick方法均不起作用。.我知道這應該是非常基本的東西,但我找不到解決方法。
我的代碼如下:
public class LandingPage extends AppCompatActivity {
Button log_in, sign_up;
Typeface tfc_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_landing_page);
log_in = (Button) findViewById(R.id.LogIn_Button);
sign_up = (Button) findViewById(R.id.SignUp_Button);
setFontType();
addSoundtoButtons();
}
public void addSoundtoButtons(){
//Add Sound to the Buttons
final MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.button_click_sound);
log_in.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaPlayer.start();
}
});
sign_up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaPlayer.start();
}
});
}
public void setFontType(){
//Set Font Type for Buttons
tfc_button = Typeface.createFromAsset(getAssets(), "fonts/TEMPSITC.TTF");
log_in = (Button) findViewById(R.id.LogIn_Button);
sign_up = (Button) findViewById(R.id.SignUp_Button);
log_in.setTypeface(tfc_button);
sign_up.setTypeface(tfc_button);
}
public void OnClickButtonLogin(View view){
Intent intent = new Intent(this, SignInPage.class);
startActivity(intent);}
public void OnClickButtonSignUp(View view){
Intent intent = new Intent(this, SignUpPage.class);
startActivity(intent);
}
}
我的活動布局如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/landingpagenormal"
tools:context="com.example.gebruiker.prototype1oneplayerpurevisualisation.Activities.LandingPage">
<Button
android:id="@+id/LogIn_Button"
android:layout_width="171dp"
android:layout_height="57dp"
android:layout_marginBottom="17dp"
android:layout_marginStart="18dp"
android:background="@drawable/login_button"
android:onClick="OnClickButtonLogin"
android:text="Log In"
android:textColor="#FFEAEA"
android:textSize="35sp"
app:layout_constraintBottom_toTopOf="@+id/SignUp_Button"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/SignUp_Button"
android:layout_width="171dp"
android:layout_height="57dp"
android:onClick="OnClickButtonSignUp"
android:layout_marginBottom="38dp"
android:layout_marginStart="18dp"
android:background="@drawable/signup_button"
android:text="Sign Up"
android:textColor="#FFEAEA"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
我的活動的風景模式的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/landingpagelandscape"
tools:context="com.example.gebruiker.prototype1oneplayerpurevisualisation.Activities.LandingPage">
<Button
android:id="@+id/LogIn_Button"
android:layout_width="171dp"
android:layout_height="57dp"
android:layout_marginBottom="25dp"
android:layout_marginStart="100dp"
android:background="@drawable/login_button"
android:onClick="OnClickButtonLogin"
android:text="Log In"
android:textColor="#FFEAEA"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/SignUp_Button"
android:layout_width="171dp"
android:layout_height="57dp"
android:layout_marginBottom="25dp"
android:onClick="OnClickButtonSignUp"
android:layout_marginEnd="100dp"
android:background="@drawable/signup_button"
android:text="Sign Up"
android:textColor="#FFEAEA"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
好的,所以,您有兩個矛盾的地方。 首先,您的布局中的onClick
屬性:
android:onClick="OnClickButtonLogin"
其次,您的Activity中的setOnClickListener()
:
log_in.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mediaPlayer.start(); } });
第二個將覆蓋第一個。 您仍然應該看到/聽到mediaPlayer.start()
的效果,但是您將永遠不會調用OnClickButtonLogin()
。 如果您想同時擁有兩者,則可以刪除setOnClickListener()
調用,只需將mediaPlayer.start()
添加到OnClickButtonLogin()
方法中:
public void OnClickButtonLogin(View view) {
mediaPlayer.start();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.