亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在活動類中實現方法

如何在活動類中實現方法

瀟湘沐 2022-08-03 16:12:13
我剛剛開始學習Android Studio的Android編程,不幸的是,我有一個很大的問題,可能是一件非常簡單的事情,即在主活動的布局文件中,我將startActivity()方法分配給兩個按鈕的屬性(android:onClick=“startActivity()”)。android:onClick現在我應該在MainActivity類中,實現startActivity()方法,但是....我不知道該怎么做。我看到我應該在MainActivity中擁有:.我試過了,我正在尋找幾個小時的解決方案,我已經失去了希望。特別是我可以實現例如View.OnClickListener但是方法,startActivity()不能再這樣做了。如何實現此方法?public void startActivity(View v)我將 startActivity() 方法分配給了 activity_main.xml 中兩個按鈕的 android:onClick 屬性:<Button    android:id="@+id/button"    android:onClick="startActivity()"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginStart="32dp"    android:layout_marginTop="24dp"    android:text="@string/button"    app:layout_constraintStart_toStartOf="parent"    app:layout_constraintTop_toTopOf="parent"    tools:ignore="OnClick" /><Button    android:id="@+id/button2"    android:onClick="startActivity()"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginStart="32dp"    android:layout_marginTop="16dp"    android:text="@string/button2"    app:layout_constraintStart_toStartOf="parent"    app:layout_constraintTop_toBottomOf="@+id/button"    tools:ignore="OnClick" />接下來,我應該在MainActivity類中,實現startActivity()方法,但是....我不知道該怎么做:public class MainActivity extends AppCompatActivity implements startActivity() {    Button  b1, b2;    EditText et1, et2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        b1 = findViewById(R.id.button);        b2 = findViewById(R.id.button2);        et1 = findViewById(R.id.editText);        et2 = findViewById(R.id.editText2);    }}
查看完整描述

3 回答

?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

您錯誤地使用了屬性。這是錯誤的:onClick


android:onClick="startActivity()"

它應該是:


android:onClick="startActivity"

在 https://developer.android.com/guide/topics/ui/controls/button#HandlingEvents 閱讀更多內容


建議

應避免在 xml 中使用。請改用 。將邏輯布局和 UI 布局分開非常重要,這樣在更改 xml 布局時就不需要考慮太多。使用類似如下的內容:android:onClickonClickListener


Button button = (Button) findViewById(R.id.your_button);

button.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        // Do something here when button is clicked.

    }

});


查看完整回答
反對 回復 2022-08-03
?
蝴蝶刀刀

TA貢獻1801條經驗 獲得超8個贊

正如??? ???? ????9A https://developer.android.com/guide/topics/ui/controls/button#HandlingEvents


Responding to Click Events


<Button xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/button_send"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="@string/button_send"


    //onClick function/method name don't use round brackets


    android:onClick="sendMessage" />

和在您的活動中


//JAVA


/** Called when the user touches the button */

public void sendMessage(View view) {

    // Do something in response to button click

}


//Kotlin


/** Called when the user touches the button */

fun sendMessage(view: View) {

    // Do something in response to button click

}

并刪除tools:ignore="OnClick"


我希望它有幫助


查看完整回答
反對 回復 2022-08-03
?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

首先,改變這一點

android:onClick="startActivity()"

對此:

android:onClick="startActivity"

然后將 startActivity 方法移到 OnCreate 方法下方。它目前在OnCreate內部


查看完整回答
反對 回復 2022-08-03
  • 3 回答
  • 0 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號