Android基础知识【项目实训-登录与个人信息及样式背景】【7】

时间:2022-04-07 21:10:33

【该项目实训是Android基础知识的一个综合练习,特别提示:项目中会用到一些图片素材,都是随意整理的,稍后会上传一个资源,包含该事项项目的基本功能,也含有图片素材

【项目题目】:校园订餐App设计 综合案例【目标】

实现登录功能。匿名用户虽然可以查看各种食物、店铺等信息,但是涉及与用户相关操作时都需要验证登录信息。登录使用一个对话框完成。

具体登录对话框的弹出是当用户点击 “用户信息” 菜单时发生的,如果没有登录,会弹出登录框,否则显示用户信息界面:

1、登录对话框类是 LoginDialog,代码如下

/**
* 自定义 登录对话框
* @author Administrator
*/
public class LoginDialog {
/**
* 构造方法
* @param context 所属界面的Activity
*/
public LoginDialog(final Activity context){
AlertDialog.Builder builder=new Builder(context);
builder.setTitle("用户登录");
builder.setIcon(R.drawable.logo_s);
<span style="background-color: rgb(255, 102, 102);">View v =LayoutInflater.from(context).inflate(R.layout.dialog_login, null);</span>
final EditText et1 =(EditText) v.findViewById(R.id.dialog_login_name);
final EditText et2 =(EditText) v.findViewById(R.id.dialog_login_pwd);

builder.setView(v);
builder.setPositiveButton("登录账户",new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
String n =et1.getText().toString();
String p=et2.getText().toString();
if(n==null||p==null){
Toast.makeText(context, "请输入登录信息", Toast.LENGTH_SHORT).show();
return;
}
EatDbHelper dbh=new EatDbHelper(context, "fooddb.db3", null, 1);
UserInfo uinfo =new UserInfoDao().login(dbh, n,p);
if(uinfo==null){
Toast.makeText(context, "账户信息错误!", Toast.LENGTH_SHORT).show();
}else{
<span style="background-color: rgb(255, 102, 102);">EatApp app =(EatApp) context.getApplication();
app.userInfo=uinfo;</span>
Log.i("Msg", "用户登录成功:"+uinfo.toString());
Toast.makeText(context, "登录成功,请继续操作!", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("注册账户", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
//注册新账户

}
});
builder.show();
}

}

2、对话框是自定义布局的。布局文件如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:orientation="vertical" >

<TextView
android:id="@+id/dialog_login_nameStr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="百米账户:" />

<EditText
android:id="@+id/dialog_login_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/dialog_login_nameStr"
android:layout_marginLeft="8dp"
android:layout_toRightOf="@+id/dialog_login_nameStr"
android:background="@drawable/textbg"
android:textColor="@color/shopcart_fontcolor"
android:hint="请输入账户名称"
android:ems="10" >
<requestFocus />
</EditText>

<TextView
android:id="@+id/dialog_login_pwdStr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dialog_login_nameStr"
android:layout_marginTop="20dp"
android:text="百米密码:" />

<EditText
android:id="@+id/dialog_login_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/dialog_login_name"
android:layout_alignBottom="@+id/dialog_login_pwdStr"
android:ems="10"
android:background="@drawable/textbg"
android:textColor="@color/shopcart_fontcolor"
android:hint="请输入账户密码"
android:inputType="textPassword" />

</RelativeLayout>
3、如果是已经登录过了,点击个人信息菜单时,会打开信息界面 UserinfoActivity 。

public class UserinfoActivity extends Activity {
ListView accountLv,infoLv;
List<Map<String,Object>> accountData,userinfoData;
EatApp app;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_userinfo);
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
accountLv=(ListView) findViewById(R.id.userinfo_useraccount);
infoLv=(ListView) findViewById(R.id.userinfo_userinfo);
app=(EatApp) getApplication();
//初始化俩个列表
initLv();
}

private void initLv() {
accountData =new ArrayList<Map<String,Object>>();
Map<String,Object>acount =new HashMap<String,Object>();
acount.put("img", R.drawable.set);
acount.put("content", "编辑账户信息");
accountData.add(acount);
SimpleAdapter sa =new SimpleAdapter(
this,
accountData,
R.layout.userinfolist_item,
new String[]{"img","content"},
new int[]{R.id.userinfolist_item_leftimg,R.id.userinfolist_item_content}
);
accountLv.setAdapter(sa);
//
userinfoData=new ArrayList<Map<String,Object>>();
Map<String,Object> info1 =new HashMap<String,Object>();
info1.put("img", R.drawable.zaddress_icon);
info1.put("content", "地址:"+app.userInfo.getAddress());
userinfoData.add(info1);
Map<String,Object> info2 =new HashMap<String,Object>();
info2.put("img", R.drawable.zaddress_icon);
info2.put("content", "电话:"+app.userInfo.getPhone());
userinfoData.add(info2);
Map<String,Object> info3 =new HashMap<String,Object>();
info3.put("img", R.drawable.zaddress_icon);
info3.put("content", "生日:"+DateUtil.date2String(app.userInfo.getBirthday()));
userinfoData.add(info3);
sa =new SimpleAdapter(
this,
userinfoData,
R.layout.userinfolist_item,
new String[]{"img","content"},
new int[]{R.id.userinfolist_item_leftimg,R.id.userinfolist_item_content}
);
infoLv.setAdapter(sa);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.userinfo, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==android.R.id.home){
UserinfoActivity.this.finish();
}
return super.onOptionsItemSelected(item);
}
}

4、登录界面效果与个人信息界面效果

Android基础知识【项目实训-登录与个人信息及样式背景】【7】

Android基础知识【项目实训-登录与个人信息及样式背景】【7】
【郑重声明:信息都是 随便写的,没有任何意义】

5、项目中用的几个 背景框代码如下:

facebg。xml,用于显示用户头像后的框子。

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<corners android:radius="4dp"/>
<stroke android:width="2dp" android:color="#996666"/>
<solid android:color="#DDCCCC"/>

</shape>

编辑框背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<padding android:left="4dp" android:top="5dp"
/>
<corners android:radius="5dp"/>
<stroke android:width="2dp" android:color="#993333" />
<solid android:color="#AA993333"/>
</shape>
列表项所用,灰色边框

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="8dp"/>
<padding android:left="5dp"
android:top="5dp"
android:bottom="5dp"
/>
<stroke android:width="2dp" android:color="#999999"/>

</shape>
图标背景框

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<padding android:left="2dp" android:top="2dp"
android:right="2dp" android:bottom="2dp"/>
<corners android:radius="4dp"/>
<stroke android:width="2dp" android:color="#993333" />

</shape>
购买按钮样式 【这在后面说 加入购物车功能时 用到】

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true"
android:drawable="@drawable/bt_03"/>
<item android:drawable="@drawable/bt_01"/>

</selector>

登录与用户完,下一篇说 点击某个事物,显示事物详情和购买功能。