[安卓初学者实验]实现一个简单仿qq登陆界面

实现一个简易仿qq登录界面,要求实现:

1)     登录界面有帐号、密码文本和编辑框,登录和退出按钮。在程序中维护一个帐号密码的数组,用以判断正确登录与否。如果登录成功,则进入qq主界面,否则清空帐号和密码编辑框,重新登录;

2)     在qq主界面中,显示当前登录帐号信息、退出登录按钮和一个列表框listview,列表框中有以下几项:在线好友、我的好友、陌生人、黑名单和我的群;

3)     在qq主界面中,可以给当前登录帐号选择头像。当点击登录帐号头像时,进入头像选择界面,该界面是一个gridview(其用法参考P133例子),每个格子中放一个头像,选中确定后返回qq主界面,并在qq主界面显示所选中的头像。


效果图:登陆界面

[安卓初学者实验]实现一个简单仿qq登陆界面_第1张图片

布局文件



 
    
 
                
 
            
 
            
 
            


主界面:
[安卓初学者实验]实现一个简单仿qq登陆界面_第2张图片

布局文件:




    

        

        

            

            

            
        

        

更换头像界面:

[安卓初学者实验]实现一个简单仿qq登陆界面_第3张图片

布局文件:




    

    


JAVA代码:

登陆部分:用户名和密码存储在values的array内

public int loginCheck() {
		Resources res = getResources();
		String[] usernames = res.getStringArray(R.array.username);
		String[] passwords = res.getStringArray(R.array.password);
		String[] nicknames = res.getStringArray(R.array.nickname);
		String[] signatures = res.getStringArray(R.array.signature);
		String un = usernameText.getText().toString(), pw = passwordText
				.getText().toString();
		if (un.equals("") || pw.equals("")) {
			Toast.makeText(getApplicationContext(), "用户名和密码不能为空!",
					Toast.LENGTH_SHORT).show();
			usernameText.setText("");
			passwordText.setText("");
		}
		for (int i = 0; i < usernames.length; i++) {
			if (un.equals(usernames[i]) && pw.equals(passwords[i])) {
				usernameShow = usernames[i];
				nicknameShow = nicknames[i];
				signatureShow = signatures[i];
				return 1;
			}
		}
		Toast.makeText(getApplicationContext(), "用户名或密码错误", Toast.LENGTH_SHORT)
				.show();
		usernameText.setText("");
		passwordText.setText("");
		return 0;
	}
onCreate部分写的很麻烦,正常应该是再新建一个activity的,我直接把主界面和登录写在一个里面了
protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		login = (Button) findViewById(R.id.bnLogin);
		usernameText = (EditText) findViewById(R.id.userNameText);
		passwordText = (EditText) findViewById(R.id.passwdText);
		login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (loginCheck() == 1) {
					setContentView(R.layout.info_show);
					TextView usernameShow = (TextView) findViewById(R.id.usernameShow);
					TextView nicknameShow = (TextView) findViewById(R.id.nicknameShow);
					TextView signatureShow = (TextView) findViewById(R.id.signatureShow);
					setIcon = (ImageView) findViewById(R.id.iconChange);
					setIcon.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							Intent select = new Intent(MainActivity.this,ChangeIcon.class);
							select.putExtra("str",iconNumber);
							//startActivityForResult(select,ICON_POSITION);	
							startActivityForResult(select,REQUEST_CODE);//通过重写onActivityResult方法,实现改变头像
						}
					});
					logout = (Button) findViewById(R.id.logout);
					logout.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							finish();
						}
					});
					usernameShow.setText(MainActivity.this.usernameShow);
					nicknameShow.setText(MainActivity.this.nicknameShow);
					signatureShow.setText(MainActivity.this.signatureShow);
					ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
						Resources res = getResources();
						String[] groups = res.getStringArray(R.array.groups);
						String[][] child = new String[][] {
								{ "1", "2", "3", "4" },
								{ "5", "6", "7", "8", "9", "10" },
								{ "11", "12", "13", "14", "15", "16", "17", },
								{ "18" },
								{ "19", "20", "21", "22", "23", "24", "25",
										"26" }, };

						@Override
						public int getGroupCount() {
							// TODO Auto-generated method stub
							return groups.length;
						}

						@Override
						public int getChildrenCount(int groupPosition) {
							// TODO Auto-generated method stub
							return child[groupPosition].length;
						}

						@Override
						public Object getGroup(int groupPosition) {
							// TODO Auto-generated method stub
							return groups[groupPosition];
						}

						@Override
						public Object getChild(int groupPosition,
								int childPosition) {
							// TODO Auto-generated method stub
							return child[groupPosition][childPosition];
						}

						@Override
						public long getGroupId(int groupPosition) {
							// TODO Auto-generated method stub
							return groupPosition;
						}

						@Override
						public long getChildId(int groupPosition,
								int childPosition) {
							// TODO Auto-generated method stub
							return childPosition;
						}

						@Override
						public boolean hasStableIds() {
							// TODO Auto-generated method stub
							return true;
						}

						@Override
						public View getGroupView(int groupPosition,
								boolean isExpanded, View convertView,
								ViewGroup parent) {
							// TODO Auto-generated method stub
							TextView textView = new TextView(MainActivity.this);
							textView.setTextSize(18);
							textView.setGravity(Gravity.CENTER_VERTICAL
									| Gravity.LEFT);
							textView.setPadding(50, 5, 5, 5);
							textView.setText(getGroup(groupPosition).toString()
									+ "0/" + getChildrenCount(groupPosition));
							return textView;
						}

						@Override
						public View getChildView(int groupPosition,
								int childPosition, boolean isLastChild,
								View convertView, ViewGroup parent) {
							// TODO Auto-generated method stub
							TextView textView = new TextView(MainActivity.this);
							textView.setTextSize(14);
							textView.setGravity(Gravity.CENTER_VERTICAL
									| Gravity.LEFT);
							textView.setPadding(70, 5, 5, 5);
							textView.setText(getChild(groupPosition,
									childPosition).toString());
							return textView;
						}

						@Override
						public boolean isChildSelectable(int groupPosition,
								int childPosition) {
							// TODO Auto-generated method stub
							return true;
						}
					};
					ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list);
					expandableListView.setAdapter(adapter);
				}

			}
		});
	}

onActivityResult函数:

protected void onActivityResult(int requestCode, int resultCode, Intent data)  
    {  
        if (requestCode==REQUEST_CODE)  
        {  
            if (resultCode==ChangeIcon.RESULT_CODE)  
            {  
                Bundle bundle=data.getExtras();  
                String str=bundle.getString("back");
                iconNumber = str;
                setIcon.setImageResource(imageIds[Integer.valueOf(str)]);
                Toast.makeText(MainActivity.this, "更换头像成功", Toast.LENGTH_LONG).show();
            }  
        }  
    }  

ChangeIcon.class:

public class ChangeIcon extends MainActivity {
	public final static int RESULT_CODE=1;
	Button cancelBtn;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.icon_change);
		/*
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		String str = bundle.getString("iconPosition");
		*/
		cancelBtn = (Button) findViewById(R.id.cancelButton);
		cancelBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});	
		List> listItems = new ArrayList>();
		for (int i = 0; i < imageIds.length; i++) {
			Map listItem = new HashMap();
			listItem.put("image", imageIds[i]);
			listItems.add(listItem);
		}
		SimpleAdapter simpleAdapter = new SimpleAdapter(this,
				listItems, R.layout.image_show, new String[] { "image" },
				new int[] { R.id.imageShow });
		GridView grid = (GridView) findViewById(R.id.gridView);
		grid.setAdapter(simpleAdapter);
		grid.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView parent, View view,
					int position, long id) {
				Intent intent=new Intent();
				String str = position + "";
	            intent.putExtra("back", str);
	            setResult(RESULT_CODE, intent); 				
				finish();
			}
		});
	}
}


小结:整体写的很乱,使用了书中 ExpandableListAdapter、GridView等案例,拼凑出这样一个程序,初步对一个android工程结构有了认识。

工程源码

你可能感兴趣的:(安卓课程实验)