java - 如何在 Android Studio 上将按钮与下一个屏幕连接

标签 java android onclicklistener buttonclick

我正在 android studio 上构建这个应用程序,我正在尝试将我的按钮与下一个 Activity (即登录屏幕)链接。在我让它与注册屏幕一起使用之前,但后来我搞乱了代码,现在当我运行应用程序并单击注册按钮时,它不起作用,我的应用程序崩溃并关闭,登录按钮甚至不执行任何操作。 以下是主页 Activity 和登录页面 Activity 的代码 首先,我将粘贴 Frontpage Activity 代码,然后是按钮所在的位置,然后是其 Java 类,然后我将粘贴登录页 Activity ,然后是其 Java 类。 有人可以建议我如何从首页的登录按钮调用登录 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 类

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类

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){
}
}

最佳答案

问题是您的 Activity 不包含为按钮的 android:onClick 属性设置的方法。

对于 Frontpage Activity 使用的布局,您可以将 btRegister 按钮的 android:onClick 属性更改为 android: onClick="onButtonClick" 或在该 Activity 中创建一个 public void bRegister(View v){...} 方法。

对于 Login Activity ,布局有两个按钮,其 android:onClick 属性设置为 userReguserLogin,您可以在 Activity 中创建这些方法或将这两个属性值更改为 buttonOnClick

关于java - 如何在 Android Studio 上将按钮与下一个屏幕连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34252242/

相关文章:

android - 如何通过 Complete Linux Installer 远程 ssh 到 Linux 服务器安装/运行在 Android 设备上

java - android:elevation 不适用于 BottomNavigationView

java - 为什么我们必须在 onClick 方法中添加 'View' 作为参数以及它的作用是什么?

java - 使用 Spring 依赖注入(inject)部署 CXF 服务端点

java - 插入方法中的数据是否应该尽可能原始?

java - Apache POI : How to add Diagonal Border

android - 使用 Picasso 从内部存储加载图像

java - 按代码排序

java - RecyclerView 中的 OnClickListener 具有图像、文本

Android ImageButton 在 Activity 中运行良好。它在 fragment 中不起作用