package com.example.t;
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.TextView;
import android.widget.Toast;
public class LoginActivity extends Activity {
private EditText mAccount;
private EditText mPwd;
private Button mLoginButton;
private View loginView;
private View loginSuccessView;
private TextView loginSuccessShow;
private LoginDataManager mUserDataManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpage);
mAccount=(EditText) findViewById(R.id.login_edit_account);
mPwd = (EditText) findViewById(R.id.login_edit_pwd);
mLoginButton = (Button) findViewById(R.id.login_btn_login);
loginView=findViewById(R.id.login_view);
loginSuccessView=findViewById(R.id.login_success_view);
loginSuccessShow=(TextView) findViewById(R.id.login_success_show);
mLoginButton.setOnClickListener(mListener);
if (mUserDataManager == null) {
mUserDataManager = new LoginDataManager(this);
mUserDataManager.openDataBase();
}
}
OnClickListener mListener = new OnClickListener() {
public void onClick(View v) {
int id=v.getId();
if( id==R.id.login_btn_login){
login();
}
}
};
public void login() {
if (isUserNameAndPwdValid()) {
String userName = mAccount.getText().toString().trim();
String userPwd = mPwd.getText().toString().trim();
int result=mUserDataManager.findUserByNameAndPwd(userName, userPwd);
if(result==1){
//login success
loginView.setVisibility(View.GONE);
loginSuccessView.setVisibility(View.VISIBLE);
loginSuccessShow.setText(getString(R.string.user_login_sucess, userName));
Toast.makeText(this, getString(R.string.login_sucess),
Toast.LENGTH_SHORT).show();
}else if(result==0){
//login failed,user does't exist
Toast.makeText(this, getString(R.string.login_fail),
Toast.LENGTH_SHORT).show();
}
}
}
public void register() {
if (isUserNameAndPwdValid()) {
String userName = mAccount.getText().toString().trim();
String userPwd = mPwd.getText().toString().trim();
//check if user name is already exist
int count=mUserDataManager.findUserByName(userName);
if(count>0){
Toast.makeText(this, getString(R.string.name_already_exist, userName),
Toast.LENGTH_SHORT).show();
return ;
}
}
}
public boolean isUserNameAndPwdValid() {
if (mAccount.getText().toString().trim().equals("")) {
Toast.makeText(this, getString(R.string.account_empty),
Toast.LENGTH_SHORT).show();
return false;
} else if (mPwd.getText().toString().trim().equals("")) {
Toast.makeText(this, getString(R.string.pwd_empty),
Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
@Override
protected void onResume() {
if (mUserDataManager == null) {
mUserDataManager = new LoginDataManager(this);
mUserDataManager.openDataBase();
}
super.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
if (mUserDataManager != null) {
mUserDataManager.closeDataBase();
mUserDataManager = null;
}
super.onPause();
}
}
另一個
package com.example.t;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
/**
@description 首頁
*/
public class HomePageActivity extends Activity implements OnItemClickListener {
private boolean[] isPermitted;
int drawableitems[] = { R.drawable.aa,R.drawable.bb,R.drawable.cc,R.drawable.dd,R.drawable.ee,R.drawable.ff,R.drawable.gg};
String[] itemsnames = { "最熱單品","時尚飾品","珠寶首飾", "品質手表","潮流眼鏡","紳士配件","DIY" };
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
GridView gridview = (GridView) findViewById(R.id.GridView);
List> itemList = new ArrayList>();
for (int i = 0; i < itemsnames.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", drawableitems[i]);
map.put("ItemText", itemsnames[i]);
itemList.add(map);
}
SimpleAdapter saMenuItem = new SimpleAdapter(this, itemList, // 數據源
R.layout.homeitems, // xml實現
new String[] { "ItemImage", "ItemText" }, // 對應map的Key
new int[] { R.id.ItemImage, R.id.ItemText }); // 對應R的Id
// 添加Item到網格中
gridview.setAdapter(saMenuItem);
gridview.setOnItemClickListener(this);
//permit setting
isPermitted = new boolean[saMenuItem.getCount()];
}
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
System.out.println("id:" + id);
System.out.println("position:" + position);
// String[] itemsnames = { "最熱單品","時尚飾品","珠寶首飾", "品質手表","潮流眼鏡","紳士配件","DIY" };
if (position == 0) {
System.out.println("最熱單品");
Intent intent1 = new Intent(this, HotActivity.class);
startActivity(intent1);
}
if (position == 1) {
System.out.println("時尚飾品");
Intent intent = new Intent(this, FashionActivity.class);
startActivity(intent);
}
if (position == 2) {
System.out.println("珠寶首飾");
Intent intent = new Intent(this, JewelryActivity.class);
startActivity(intent);
}
if (position == 3) {
System.out.println("品質手表");
Intent intent = new Intent(this, WatchActivity.class);
startActivity(intent);
}
if (position == 4) {
System.out.println("潮流眼鏡");
Intent intent = new Intent(this, GlassesActivity.class);
startActivity(intent);
}
if (position == 5) {
System.out.println("紳士配件");
Intent intent = new Intent(this,GentryActivity.class);
startActivity(intent);
}
if (position == 6) {
System.out.println("DIY");
Intent intent = new Intent(HomePageActivity.this,DiyActicity.class);
startActivity(intent);
}
}
}
之前可能發錯了,不好意思
是不是缺少了一些代碼,求補充~
//判斷用戶名和密碼是否正確,如果正確了跳轉到其他頁面
if(username.equals("admin")&&password.equals("123456")){
Intent intent = new Intent();
//AaActivity.this代表本類 HomePageActivity.class代表要跳轉的類
intent.setClass(AaActivity.this, HomePageActivity.class);
//頁面跳轉
startActivity(intent);
Toast.makeText(AaActivity.this, "登錄成功", 0).show();
}else{
Toast.makeText(AaActivity.this, "用戶名或密碼錯誤", 0).show();
}