package com.example.android.login;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText ed1;
EditText ed2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1 = (EditText) findViewById(R.id.ID);
ed2 = (EditText) findViewById(R.id.password);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(ed1.getText().toString().equals("xyz") &&
ed2.getText().toString().equals("abcd")) {
Intent intent = new Intent(MainActivity.this,
HomePageActivity.class);
startActivity(intent);
}
else
Toast.makeText(getBaseContext(), "Invalid username or password", Toast.LENGTH_LONG).show();
}
});
}
}
这是我的主要活动。如果用户名或密码正确,我想转到HomePageActivity。但是当我这样做时,我的应用程序崩溃了。 HomePageActivity只有onCreate方法,它具有另一个xml文件的setViewContent。
答案 0 :(得分:1)
试试这个例子:
1)LoginActivity.class:----------
public class LoginActivity extends AppCompatActivity {
private EditText edt_1;
private EditText edt_2;
private Button b;
private final String EDT1 = "xyz";
private final String EDT2 = "abcd";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
edt_1 = (EditText) findViewById(R.id.edt_1);
edt_2 = (EditText) findViewById(R.id.edt_2);
b = (Button) findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!edt_1.getText().toString().isEmpty() && !edt_2.getText().toString().isEmpty()){
if(edt_1.getText().toString().equals(EDT1) && edt_2.getText().toString().equals(EDT2)){
finish();
Intent information_intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(information_intent);
}else{
Toast.makeText(getApplicationContext() , "Wrong" , Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getApplicationContext() , "Edt 1 or Edt 2 is empty" , Toast.LENGTH_LONG).show();
}
}
});
}
}
2)MainActivity.class:------------
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
}
3)login_activity.xml:----
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:id="@+id/tv1"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:text="Edt 1"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter 1..."
android:layout_alignParentEnd="true"
android:layout_toEndOf="@id/tv1"
android:layout_centerVertical="true"
android:layout_marginStart="20dp"
android:background="@android:color/transparent"
android:id="@+id/edt_1"
android:text=""/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:id="@+id/tv2"
android:layout_marginStart="10dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:text="Edt 2"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter 2..."
android:id="@+id/edt_2"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@id/tv2"
android:layout_centerVertical="true"
android:layout_marginStart="20dp"
android:background="@android:color/transparent"
android:text=""/>
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/b"
android:layout_marginTop="50dp"
android:text="Check"/>
</LinearLayout>
4)main_activity.xml:-------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Activity"/>
</LinearLayout>
5)AndroidManifest:
<activity android:name="com.example.admin.accessories.MainActivity">
<intent-filter>
<action android:name="com.example.admin.accessories.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.example.admin.accessories.LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
答案 1 :(得分:0)
删除toString()
以更改条件if(ed1.getText().equals("xyz") && ed2.getText().equals("abcd"))
另外,对于你的吐司,最好使用
getApplicationContext()