[英]Button won't switch activities after checking if RadioButton is selected
[英]The activities in android studio don't switch after pressing the button
我已經嘗試過搜索Google,以了解如何在android studio中切換活動,甚至在帶有教程的官方文檔中也是如此。 我完全按照說的做,但是單擊按鈕后仍然無法重定向到另一個活動。
我在按鈕上輸入了方法的onClick名稱
它看起來像這樣: https : //i.imgur.com/pmsztaL.png (尚不能發布圖像)
這是我所說的MainActivity.class文件
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void handleButtonAddNew(View view) {
MainActivity.this.startActivity(new Intent(MainActivity.this, AddItemActivity.class));
}
}
按下電話中的按鈕后,該按鈕不起作用。
這是我的AddItemActivity.class
public class AddItemActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_item);
}
public void handleButtonRemember(View view) {
finish();
}
}
為什么按鈕不起作用,我做錯了什么?
編輯:成功運行了模擬器,並且按鈕實際上起作用了,問題出在我的手機內,那里的按鈕不想起作用。 現在問題可能在哪里?
編輯:MainActivity.class的XML布局
<?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"
tools:context=".MainActivity">
<Button
android:id="@+id/buttonAddNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="188dp"
android:layout_marginRight="188dp"
android:layout_marginBottom="264dp"
android:text="@string/buttonAdd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<ScrollView
android:id="@+id/scrollView2"
android:layout_width="395dp"
android:layout_height="715dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</android.support.constraint.ConstraintLayout>
編輯:問題在於滾動視圖阻止了可點擊按鈕,因此在我的手機上,該按鈕沒有記錄任何點擊,在刪除滾動視圖后,該按鈕可以正常工作。
恐怕此按鈕沒有附加任何偵聽器。
我建議您在onCreate
回調中使用它:
Button button = (Button) findViewById(R.id.your_btn_id);
button.setOnClickListener((view) -> { theMethodYouWantToCallHere(view); });
請使用此方法仔細檢查以下內容
AndroidManifest.xml
注冊 android
名稱空間包含到布局文件的父容器中的XML文件中。 IE: xmlns:android="http://schemas.android.com/apk/res/android"
setContentView(R.layout.your_layout_file);
設置活動的內容視圖setContentView(R.layout.your_layout_file);
android
名稱空間。 IE: android:onClick="yourMethod"
onClick
回調方法具有View view
參數; 不多不少 onClick
函數在活動中是public
的。 onClick
偵聽器的視圖:)包括名稱空間: xmlns:android="http://schemas.android.com/apk/res/android"
XML文件
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:text="Click me!" android:onClick="clicked" android:layout_height="wrap_content" android:layout_width="wrap_content"/>
</FrameLayout>
活動
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clicked(View view){
Toast.makeText(this, "Hey! Im clicked!",Toast.LENGTH_SHORT).show();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.