我试图通过按一下按钮让程序从一个类转到另一个类。我知道这需要有 Intent 地完成,但我不确定到底要使用什么行动来实现 Intent 。另外,我不想尝试使用 OnClickListener,因为在我看来,这对于我需要做的事情来说有点过多。虽然我可能是错的,而且我只是想多了。这是我的代码:
public void loginScreen(View view) {
// Should take user to the next view when button pressed
Intent intent = new Intent(this, LoginActivity.class);
Button login = (Button) findViewById(R.id.button1);
intent.getData(login);
startActivity(intent);
}
我希望它在仅按下按钮 1 时进入下一个屏幕,所以我不知道是否只需要第 3 行和第 6 行,但如果只是这两个行,它将无法工作。我相信问题是 getData,我不知道该放什么,但我知道 getData 是错误的。我可能还遗漏了其他东西,但我不知道是什么。
另外,如果这不太容易理解,请原谅我,第一次尝试在这里提问。
最佳答案
在您的 XML 文件中,您可以声明您的点击:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="loginScreen" />
然后在您的 Activity 中:
public void loginScreen(View button) {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
Name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity).
请注意,如果您将 onClick 添加到 Fragment 布局,它仍然需要在 Activity 中捕获。
关于java - 使用按钮更改类(class) View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14924472/