如何将按钮与 android studio 上的下一个屏幕连接
how to connect button with next screen on android studio
我正在 android studio 上构建此应用程序,我正在尝试 link 我的按钮和下一个 activity 登录屏幕。在我让它与注册屏幕一起工作之前,但后来我搞砸了代码,现在当我 运行 应用程序并单击注册按钮时它不起作用,我的应用程序崩溃并关闭,登录按钮甚至不起作用任何事物。
下面是主页 activity 和登录页面 activity 的代码
首先,我将在按钮所在的位置粘贴首页 activity 代码,然后是其 java class,然后我将粘贴登录页面 activity,然后是其 java class.
有人可以告诉我如何从首页的登录按钮调用登录 activity 吗?
提前谢谢你
首页activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Login">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:background="@drawable/bg3"
android:gravity="center|top">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Easy Booking"
android:id="@+id/textView"
android:textSize="33dp"
android:gravity="center"
android:textColor="#0c0c0c"
/>
<Button
android:layout_width="98dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Login"
android:id="@+id/btLogin"
android:onClick="bLogin"
android:background="@null"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="108dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Register"
android:id="@+id/btRegister"
android:onClick="bRegister"
android:background="@null"
android:layout_gravity="center_horizontal" />
</LinearLayout>
现在 java class
public class Frontpage extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frontpage);
//OnclickButtonListener();
}
public void bLogin(View view) {
}
public void onButtonClick(View v){
if (v.getId() == R.id.btRegister) {
Intent i = new Intent(new Intent(Frontpage.this, Register.class));
startActivity(i);
}
}
/**
public void OnclickButtonListener(){
button = (Button)findViewById(R.id.bRegister);
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("/Users/umairfarooq/AndroidStudioProjects/Easybooking/app/src/main/res/layo ut/activity_register");
startActivity(intent);
}
}
);
} /**@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity_login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
*/}
下面是登录activity
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#635b5b"
android:orientation="vertical"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login Form"
android:textAppearance="?android:textAppearanceLarge"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
/>
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="Email"
android:id="@+id/etUsername"
android:layout_gravity="center_horizontal"
android:layout_marginTop="70dp"
/>
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="Password"
android:id="@+id/etPassword"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:inputType="textPassword"
/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Login"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:onClick="userLogin"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register Now"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:onClick="userReg"
/>
</LinearLayout>
现在登录 java class
package com.example.umairfarooq.easybooking;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
public class Login extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void buttonOnClick (View v){
}
}
when i run the application and click on register button my app crashes
and shuts down
因为这个按钮:
<Button
android:layout_width="108dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Register"
android:id="@+id/btRegister"
android:onClick="bRegister"
android:background="@null"
android:layout_gravity="center_horizontal" />
在您的首页 activity 中需要一个在您的 FrontPage.java class 中带有签名 public void bRegister(View view)
的方法。由于你没有这个方法,它崩溃了。
login button doesn't even do anything
原因是,这个按钮
<Button
android:layout_width="98dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Login"
android:id="@+id/btLogin"
android:onClick="bLogin"
android:background="@null"
android:layout_gravity="center_horizontal" />
在您的首页 activity 中需要在您的 Frontpage.java class 中调用一个名为 public void bLogin(View view)
的方法。尽管存在该方法,但您没有任何代码,因此它不会执行任何操作。
您需要在 bLogin 方法中添加适当的代码,以便您的登录按钮开始工作,甚至在此之前添加 bRegister 方法,以便您的注册按钮开始工作。
希望对您有所帮助。
问题是您的活动不包含您为按钮的 android:onClick
属性设置的方法。
对于 Frontpage
activity 使用的布局,您可以将 btRegister
按钮的 android:onClick
属性更改为 android:onClick="onButtonClick"
或创建一个 [= activity.
中的 15=] 方法
对于 Login
activity,布局有两个按钮,其 android:onClick
属性设置为 userReg
和 userLogin
,您可以创建它们activity 中的方法或将这两个属性值更改为 buttonOnClick
.
我正在 android studio 上构建此应用程序,我正在尝试 link 我的按钮和下一个 activity 登录屏幕。在我让它与注册屏幕一起工作之前,但后来我搞砸了代码,现在当我 运行 应用程序并单击注册按钮时它不起作用,我的应用程序崩溃并关闭,登录按钮甚至不起作用任何事物。 下面是主页 activity 和登录页面 activity 的代码 首先,我将在按钮所在的位置粘贴首页 activity 代码,然后是其 java class,然后我将粘贴登录页面 activity,然后是其 java class. 有人可以告诉我如何从首页的登录按钮调用登录 activity 吗? 提前谢谢你
首页activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Login">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:background="@drawable/bg3"
android:gravity="center|top">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Easy Booking"
android:id="@+id/textView"
android:textSize="33dp"
android:gravity="center"
android:textColor="#0c0c0c"
/>
<Button
android:layout_width="98dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Login"
android:id="@+id/btLogin"
android:onClick="bLogin"
android:background="@null"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="108dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Register"
android:id="@+id/btRegister"
android:onClick="bRegister"
android:background="@null"
android:layout_gravity="center_horizontal" />
</LinearLayout>
现在 java class
public class Frontpage extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frontpage);
//OnclickButtonListener();
}
public void bLogin(View view) {
}
public void onButtonClick(View v){
if (v.getId() == R.id.btRegister) {
Intent i = new Intent(new Intent(Frontpage.this, Register.class));
startActivity(i);
}
}
/**
public void OnclickButtonListener(){
button = (Button)findViewById(R.id.bRegister);
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("/Users/umairfarooq/AndroidStudioProjects/Easybooking/app/src/main/res/layo ut/activity_register");
startActivity(intent);
}
}
);
} /**@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity_login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
*/}
下面是登录activity
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#635b5b"
android:orientation="vertical"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login Form"
android:textAppearance="?android:textAppearanceLarge"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
/>
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="Email"
android:id="@+id/etUsername"
android:layout_gravity="center_horizontal"
android:layout_marginTop="70dp"
/>
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="Password"
android:id="@+id/etPassword"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:inputType="textPassword"
/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Login"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:onClick="userLogin"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register Now"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:onClick="userReg"
/>
</LinearLayout>
现在登录 java class
package com.example.umairfarooq.easybooking;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
public class Login extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void buttonOnClick (View v){
}
}
when i run the application and click on register button my app crashes and shuts down
因为这个按钮:
<Button
android:layout_width="108dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Register"
android:id="@+id/btRegister"
android:onClick="bRegister"
android:background="@null"
android:layout_gravity="center_horizontal" />
在您的首页 activity 中需要一个在您的 FrontPage.java class 中带有签名 public void bRegister(View view)
的方法。由于你没有这个方法,它崩溃了。
login button doesn't even do anything
原因是,这个按钮
<Button
android:layout_width="98dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Login"
android:id="@+id/btLogin"
android:onClick="bLogin"
android:background="@null"
android:layout_gravity="center_horizontal" />
在您的首页 activity 中需要在您的 Frontpage.java class 中调用一个名为 public void bLogin(View view)
的方法。尽管存在该方法,但您没有任何代码,因此它不会执行任何操作。
您需要在 bLogin 方法中添加适当的代码,以便您的登录按钮开始工作,甚至在此之前添加 bRegister 方法,以便您的注册按钮开始工作。
希望对您有所帮助。
问题是您的活动不包含您为按钮的 android:onClick
属性设置的方法。
对于 Frontpage
activity 使用的布局,您可以将 btRegister
按钮的 android:onClick
属性更改为 android:onClick="onButtonClick"
或创建一个 [= activity.
对于 Login
activity,布局有两个按钮,其 android:onClick
属性设置为 userReg
和 userLogin
,您可以创建它们activity 中的方法或将这两个属性值更改为 buttonOnClick
.