我只是想知道是否有人可以帮助我。我创建了一个登录页面,要求用户输入他们的用户名和密码。一旦细节正确,它应该移动到我的主页?我只是想知道如何做到这一点。先感谢您 这是我的登录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:background="#808080"
android:orientation="vertical" >
<!-- Login Title -->
<TextView
android:layout_width="222dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:text="@string/login_title"
android:textColor="#FFFF00"
android:textSize="24sp" />
<!-- Login Image -->
<!-- layout for Username -->
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="260dp"
android:layout_marginTop="-75dp"
android:src="@drawable/logov2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="60dp"
android:gravity="center_horizontal"
android:text="@string/pleaselogin"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:layout_weight="0.83"
android:text="@string/username"
android:textColor="#FFFF00"
android:textSize="24sp" />
<EditText
android:id="@+id/username1"
android:layout_width="200dp"
android:layout_height="30dp"
android:background="#FFFFFF"
android:ems="10"
android:inputType="text"
android:textSize="24sp" >
<requestFocus />
</EditText>
</LinearLayout>
<!-- layout for Password -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="67dp"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_weight="1.0"
android:text="@string/password"
android:textColor="#FFFF00"
android:textSize="24sp" />
<EditText
android:id="@+id/password1"
android:layout_width="200dp"
android:layout_height="30dp"
android:background="#FFFFFF"
android:inputType="textPassword" />
</LinearLayout>
<Button
android:id="@+id/login1"
android:layout_width="150sp"
android:layout_height="50dp"
android:layout_marginLeft="200dp"
android:background="#FF0000"
android:text="@string/login"
android:textColor="#FFFFFF"
android:textSize="28sp" />
</LinearLayout>
登录类
package com.example.workplease;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.workplease_v2.R;
public class LoginActivity extends Activity {
String userName, passWord;
EditText username, password;
Button login;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// UI elements gets bind in form of Java Objects
username = (EditText)findViewById(R.id.username1);
password = (EditText)findViewById(R.id.password1);
login = (Button)findViewById(R.id.login1);
// now we have got the handle over the UI widgets
// setting listener on Login Button
// i.e. OnClick Event
login.setOnClickListener(loginListener);
}
private OnClickListener loginListener = new OnClickListener() {
public void onClick(View v) {
//getting inputs from user and performing data operations
if(username.getText().toString().equals("test") &&
password.getText().toString().equals("test")){
// responding to the User inputs
Toast.makeText(getApplicationContext(), "Login Successfully !!!", Toast.LENGTH_LONG).show();
}else
Toast.makeText(getApplicationContext(), "Login Not Successful !!!", Toast.LENGTH_LONG).show();
}
};
}
主页XML
<LinearLayout 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:background="#808080"
android:orientation="vertical"
tools:context=".Home" >
<TextView
android:id="@+id/title"
android:layout_width="220dp"
android:layout_height="100dp"
android:layout_marginLeft="45dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="@string/description"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFFF00"
android:textSize="28sp" />
<ImageButton
android:id="@+id/exercise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="40dp"
android:src="@drawable/exercisesv2"
android:background="@null" />
<ImageButton
android:id="@+id/routines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="22dp"
android:src="@drawable/routines"
android:background="@null" />
<ImageButton
android:id="@+id/logbook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="22dp"
android:src="@drawable/logbook"
android:background="@null" />
<ImageButton
android:id="@+id/qrscanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="22dp"
android:src="@drawable/qrscanner1"
android:background="@null" />
<TextView
android:id="@+id/Copyright"
android:layout_width="206dp"
android:layout_height="wrap_content"
android:layout_marginLeft="260dp"
android:layout_marginTop="70dp"
android:text="@string/copyright"
android:textColor="#FFFF00"
android:textSize="14sp"
android:background="@null" />
<ImageView
android:id="@+id/Logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="260dp"
android:layout_marginTop="-550dp"
android:src="@drawable/logov2" />
</LinearLayout>
家庭课程
package com.example.workplease;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import com.example.workplease_v2.R;
public class Home extends Activity {
ImageButton imageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
AddListenerOnButton();
AddListenerOnButton1();
}
public void AddListenerOnButton(){
final Context context = this;
imageButton = (ImageButton) findViewById(R.id.exercise);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Exercises.class);
startActivity(intent);
}
});
}
public void AddListenerOnButton1(){
final Context context = this;
imageButton = (ImageButton) findViewById(R.id.routines);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Routines.class);
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.main, menu);
return true;
}
}
答案 0 :(得分:1)
要开始新的Activity
,请致电startActivity(Intent intent)
Intent intent = new Intent(this, Home.class);
startActivity(intent);
因此,您的loginListener
按钮OnClickListener将执行此操作:
public void onClick(View v) {
if(username.getText().toString().equals("test") &&
password.getText().toString().equals("test")){
// responding to the User inputs
Toast.makeText(getApplicationContext(), "Login Successfully !!!",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(LoginActivity.this, Home.class);
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(), "Login Not Successful !!!",
Toast.LENGTH_LONG).show();
}
}