Android基础知识【项目实训-登录与个人信息及样式背景】【7】
来源:互联网 发布:js cookie 编辑:程序博客网 时间:2024/06/06 05:34
【该项目实训是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() {@Overridepublic 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() {@Overridepublic 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;@Overrideprotected 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);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.userinfo, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {if(item.getItemId()==android.R.id.home){UserinfoActivity.this.finish();}return super.onOptionsItemSelected(item);}}
4、登录界面效果与个人信息界面效果
【郑重声明:信息都是 随便写的,没有任何意义】
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>
登录与用户完,下一篇说 点击某个事物,显示事物详情和购买功能。
1 0
- Android基础知识【项目实训-登录与个人信息及样式背景】【7】
- Android基础知识【项目实训-实现二级导航“我的账单”及用户登录对话框】【6】
- Android基础知识【项目实训-项目结构及主界面的实现】【4】
- Android Dialog 系统样式讲解及透明背景
- Android Dialog 系统样式讲解及透明背景
- Android Dialog 系统样式讲解及透明背景
- Android Dialog 系统样式讲解及透明背景
- Android Dialog 系统样式讲解及透明背景
- Android Studio之 更改背景颜色及主题样式
- Android基础知识【项目实训-实现二级导航“今日活动”及读取数据库】【5】
- Android基础知识【项目实训-功能设计】【1】
- css样式分类及背景样式
- Android授权登录新浪微博获取用户个人信息
- Android第三方QQ登录、获取个人信息、分享实现
- 登录/注册+修改个人信息
- Android 登录样式
- Android 自定义SeekBar背景样式
- 项目实训第三周(1)--信息维护模块编码之个人信息显示
- window与linux hdfs通讯环境搭建
- OpenCV坐标体系的初步认识
- 只需一个就够(控制器)
- 面向对象一
- GDI+ GetEncoderClsid
- Android基础知识【项目实训-登录与个人信息及样式背景】【7】
- instancetype 对比 id 的好处。
- Android Studio 报错:Failed to find:com.android.support:appcompat-v7:21.+
- 用JS打开新窗口,防止被浏览器阻止的解决方法
- 好几天来没写了。不知道该说什么好。
- c,c++输入流
- asp.net中js前台为span或后台lable,或Literal控件赋值
- 关于在Spring中注册自定义的PropertyEditor
- uc/os-II的内存改进与实现TLSF算法的详解,移植实现(二)