添加 Onclick 侦听器以使按钮重定向到另一个 xml 页面

Add Onclick listener to make button redirect to another xml page

public class MainActivity extends Activity {

private EditText  username=null;
private EditText  password=null;
private TextView attempts;
private Button login;
int counter = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    username = (EditText)findViewById(R.id.editText1);
    password = (EditText)findViewById(R.id.editText2);
    attempts = (TextView)findViewById(R.id.textView5);
    attempts.setText(Integer.toString(counter));
    login = (Button)findViewById(R.id.button1);
}

public void login(View view){
    if(username.getText().toString().equals("admin") &&
            password.getText().toString().equals("admin")){
        Toast.makeText(getApplicationContext(), "Redirecting...",
                Toast.LENGTH_SHORT).show();
    }
    else{
        Toast.makeText(getApplicationContext(), "Wrong Credentials",
                Toast.LENGTH_SHORT).show();
        attempts.setBackgroundColor(Color.RED);
        counter--;
        attempts.setText(Integer.toString(counter));
        if(counter==0){
            login.setEnabled(false);
        }

    }

}
@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, menu);
    return true;
}

}

那是 MainActivity.Java,我希望它重定向到我的 Home.Java 文件

目前它显示消息重定向,我已经尝试通过 XML 文件使用 onclick。

这是Activity_Main.XML

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="18dp"
    android:text="@string/login_screen"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="50dp"
    android:text="@string/username"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView2"
    android:layout_marginLeft="32dp"
    android:layout_toRightOf="@+id/textView2"
    android:ems="10" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="38dp"
    android:text="@string/password"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView3"
    android:layout_alignLeft="@+id/editText1"
    android:ems="10"
    android:inputType="textPassword" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="94dp"
    android:onClick="login"
    android:text="@string/Login" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_below="@+id/textView3"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="48dp"
    android:text="@string/attempts"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
    android:layout_alignTop="@+id/textView4"
    android:text="TextView" />

抱歉没有完全理解你的问题,但我猜你想在单击按钮时打开另一个 activity?

如果是这样,那么您需要这样做:

public void login(View view){
if(username.getText().toString().equals("admin") &&
        password.getText().toString().equals("admin")){
    Toast.makeText(getApplicationContext(), "Redirecting...",
            Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Home.class));
}
else{
    Toast.makeText(getApplicationContext(), "Wrong Credentials",
            Toast.LENGTH_SHORT).show();
    attempts.setBackgroundColor(Color.RED);
    counter--;
    attempts.setText(Integer.toString(counter));
    if(counter==0){
        login.setEnabled(false);
    }

}

并在您的 Manifest.xml 应用程序标签中添加以下行

<activity android:name="your.package.name.home" </activity>

此外,请确保您的 Home.java 扩展 Activity class。

你需要再做一个activity假设HomeActivity 会在登录成功时打开。看下面的代码

开始回家activity:

public void login(View view){
    if(username.getText().toString().equals("admin") &&
            password.getText().toString().equals("admin")){
        Toast.makeText(getApplicationContext(), "Redirecting...",
                Toast.LENGTH_SHORT).show();
       Intent intent = (HomeActivity.class, MainActivity.this);
       startActivity(intent);

    } 
    else{ 
        Toast.makeText(getApplicationContext(), "Wrong Credentials",
                Toast.LENGTH_SHORT).show();
        attempts.setBackgroundColor(Color.RED);
        counter--;
        attempts.setText(Integer.toString(counter));
        if(counter==0){
            login.setEnabled(false);
        } 

    } 

}