大家好,我在 android studio 上使用按鈕時遇到問題。目前,當我點擊按鈕時,點擊沒有任何反應。我正在使用 onClickListener() 并且它不起作用所以我嘗試在 XML 中使用 onClick 方法并撰寫自己的函式,但這只會導致我的應用程式崩潰。請讓我知道我的代碼是否有任何問題,如果您知道問題出在哪里,謝謝。
XML檔案:
<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="5dp"
android:background="#fff"
android:elevation="0dp"
android:text="NEW USER? SIGN UP"
android:textColor="#000"
android:id="@ id/signup_btn" />
登錄.class:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button button = (Button) findViewById(R.id.signup_btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Login.this, SignUp.class);
startActivity(i);
}
});
}
uj5u.com熱心網友回復:
嘗試像這樣更改您的代碼:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// In XML you use AppCompatButton so make same AppCompatButton too in your class
AppCompatButton button = (AppCompatButton) findViewById(R.id.signup_btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Login.this, SignUp.class);
startActivity(i);
}
});
}
uj5u.com熱心網友回復:
改變這一行:
Button button = (Button) findViewById(R.id.signup_btn);
對此:
AppCompatButton button = (AppCompatButton) findViewById(R.id.signup_btn);
并確保在清單中宣告了兩個活動(注冊和登錄)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/366428.html
上一篇:如何突出顯示Gtk中的按鈕?
