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

来源:互联网 发布:筑巢软件骗局 编辑:程序博客网 时间:2024/06/05 02:49

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

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

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

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


效果图:登陆界面


布局文件

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#fff0f0f0" >     <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        android:paddingBottom="10.0dip" >                 <ImageView                    android:layout_width="57.0dip"                    android:layout_height="57.0dip"                    android:layout_gravity="center"                    android:src="@drawable/ic_launcher"                    android:adjustViewBounds="true"                    android:contentDescription="@null"                    android:scaleType="centerCrop"                    />             <EditText                android:id="@+id/userNameText"                android:layout_width="fill_parent"                android:layout_height="50.0dip"                android:layout_marginLeft="12.0dip"                android:layout_marginRight="12.0dip"                android:layout_marginTop="15.0dip"                android:background="#FFFFFF"                android:ems="10"                android:hint="@string/login_user"                android:inputType="number"                android:paddingBottom="2.0dip"                android:paddingLeft="15.0dip"                android:paddingRight="15.0dip"                android:paddingTop="2.0dip"                android:singleLine="true"                android:textColorHint="#ff999999"                android:textSize="16.0sp" />             <EditText                android:id="@+id/passwdText"                android:layout_width="fill_parent"                android:layout_height="50.0dip"                android:layout_marginBottom="20.0dip"                android:layout_marginLeft="12.0dip"                android:layout_marginRight="12.0dip"                android:layout_marginTop="20.0dip"                android:background="#ffffff"                android:drawablePadding="15.0dip"                android:ems="10"                android:hint="@string/login_password"                android:inputType="textPassword"                android:paddingBottom="2.0dip"                android:paddingLeft="15.0dip"                android:paddingRight="15.0dip"                android:paddingTop="2.0dip"                android:singleLine="true"                android:textColorHint="#ff999999"                android:textSize="16.0sp" />             <Button                android:id="@+id/bnLogin"                android:layout_width="fill_parent"                android:layout_height="50.0dip"                android:layout_marginLeft="12.0dip"                android:layout_marginRight="12.0dip"                android:background="#5CACEE"                android:singleLine="true"                android:text="@string/login_button"                android:textColor="#ffffffff"                android:textSize="18.0sp" />        </LinearLayout> </RelativeLayout>


主界面:

布局文件:

<?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:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:padding="5dp" >        <ImageView            android:id="@+id/iconChange"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:layout_weight="0.07"            android:contentDescription="@string/app_name"            android:src="@drawable/ic_launcher" />        <LinearLayout            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="7"            android:orientation="vertical"            android:paddingLeft="10dp"            android:paddingRight="10dp"            android:textSize="14sp" >            <TextView                android:id="@+id/usernameShow"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                 />            <TextView                android:id="@+id/nicknameShow"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                />            <TextView                android:id="@+id/signatureShow"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                />        </LinearLayout>        <Button            android:id="@+id/logout"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="14sp"            android:text="@string/exit"/>    </LinearLayout>    <ExpandableListView        android:id="@+id/list"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:paddingBottom="5dp"        android:paddingLeft="5dp"        android:paddingRight="5dp"        android:paddingTop="15dp" >    </ExpandableListView></LinearLayout>

更换头像界面:


布局文件:

<?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:orientation="vertical" >    <GridView        android:id="@+id/gridView"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:horizontalSpacing="3dp"        android:numColumns="4"        android:verticalSpacing="2pt" />    <Button        android:id="@+id/cancelButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/cancel" /></LinearLayout>


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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent select = new Intent(MainActivity.this,ChangeIcon.class);select.putExtra("str",iconNumber);//startActivityForResult(select,ICON_POSITION);startActivityForResult(select,REQUEST_CODE);//通过重写<span lang="EN-US" style="font-size: 10pt; line-height: 150%; font-family: "Courier New";">onActivityResult方法,实现改变头像</span>}});logout = (Button) findViewById(R.id.logout);logout.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubfinish();}});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" }, };@Overridepublic int getGroupCount() {// TODO Auto-generated method stubreturn groups.length;}@Overridepublic int getChildrenCount(int groupPosition) {// TODO Auto-generated method stubreturn child[groupPosition].length;}@Overridepublic Object getGroup(int groupPosition) {// TODO Auto-generated method stubreturn groups[groupPosition];}@Overridepublic Object getChild(int groupPosition,int childPosition) {// TODO Auto-generated method stubreturn child[groupPosition][childPosition];}@Overridepublic long getGroupId(int groupPosition) {// TODO Auto-generated method stubreturn groupPosition;}@Overridepublic long getChildId(int groupPosition,int childPosition) {// TODO Auto-generated method stubreturn childPosition;}@Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn true;}@Overridepublic View getGroupView(int groupPosition,boolean isExpanded, View convertView,ViewGroup parent) {// TODO Auto-generated method stubTextView 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;}@Overridepublic View getChildView(int groupPosition,int childPosition, boolean isLastChild,View convertView, ViewGroup parent) {// TODO Auto-generated method stubTextView 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;}@Overridepublic boolean isChildSelectable(int groupPosition,int childPosition) {// TODO Auto-generated method stubreturn 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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubfinish();}});List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();for (int i = 0; i < imageIds.length; i++) {Map<String, Object> listItem = new HashMap<String, Object>();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() {@Overridepublic 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工程结构有了认识。

工程源码

0 0