如何在活动类中实现方法

我刚刚开始学习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);

    }


}


潇湘沐
浏览 116回答 3
3回答

慕斯王

您错误地使用了属性。这是错误的:onClickandroid:onClick="startActivity()"它应该是:android:onClick="startActivity"在 https://developer.android.com/guide/topics/ui/controls/button#HandlingEvents 阅读更多内容建议应避免在 xml 中使用。请改用 。将逻辑布局和 UI 布局分开非常重要,这样在更改 xml 布局时就不需要考虑太多。使用类似如下的内容:android:onClickonClickListenerButton button = (Button) findViewById(R.id.your_button);button.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; // Do something here when button is clicked.&nbsp; &nbsp; }});

蝴蝶刀刀

正如ישו אוהב אותך9A https://developer.android.com/guide/topics/ui/controls/button#HandlingEventsResponding to Click Events<Button xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; android:id="@+id/button_send"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:text="@string/button_send"&nbsp; &nbsp; //onClick function/method name don't use round brackets&nbsp; &nbsp; android:onClick="sendMessage" />和在您的活动中//JAVA/** Called when the user touches the button */public void sendMessage(View view) {&nbsp; &nbsp; // Do something in response to button click}//Kotlin/** Called when the user touches the button */fun sendMessage(view: View) {&nbsp; &nbsp; // Do something in response to button click}并删除tools:ignore="OnClick"我希望它有帮助

红糖糍粑

首先,改变这一点android:onClick="startActivity()"对此:android:onClick="startActivity"然后将 startActivity 方法移到 OnCreate 方法下方。它目前在OnCreate内部
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java