android中 MiniTwitter 实现记住密码

</pre>MiniTwitter记住密码等功能实现 </h1></div><div id="article_content" class="article_content"><span style="font-size:14px;"><strong>miniTwitter登录界面效果图</strong></span><p><strong>先贴上要完成的效果图:</strong></p><p><strong><img alt="" src="https://img-blog.csdn.net/20150630135933012?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzA4MDUwOA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" /></strong></p><pre class="java" name="code">



miniTwitter登录界面的布局分析

首先由界面图分析布局,基本可以分为三个部分,下面分别讲解每个部分。

第一部分是一个带渐变色背景的LinearLayout布局,关于背景渐变色就不再贴代码了,效果如下图所示:



Android渐变色背景

第二部分,如图所示:



Android miniTwitter登录界面

这是一个带圆角且背景色为#55FFFFFF(淡蓝色)的RelativeLayout布局,代码如下:

XML/HTML代码
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#55FFFFFF" />
<!-- 设置圆角
注意: bottomRightRadius是左下角而不是右下角 bottomLeftRadius右下角-->
<corners android:topLeftRadius="10dp" android:topRightRadius="10dp"
android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"/>
</shape>

solid表示填充色,这里填充的是淡蓝色。corners是设置圆角。

然后RelativeLayou的background引用此drawable,具体RelativeLayout设置如下:

XML/HTML代码

  1. <RelativeLayout    
  2.           android:id="@+id/login_div"    
  3.           android:layout_width="fill_parent"    
  4.           android:layout_height="wrap_content"    
  5.           android:padding="15dip"    
  6.           android:layout_margin="15dip"    
  7.           android:background="@drawable/background_login_div_bg"    
  8.           >    
  9. </RelativeLayout>    
<RelativeLayout  
          android:id="@+id/login_div"  
          android:layout_width="fill_parent"  
          android:layout_height="wrap_content"  
          android:padding="15dip"  
          android:layout_margin="15dip"  
          android:background="@drawable/background_login_div_bg"  
          >  
</RelativeLayout>  
padding 是指内边距(也就是指内容与边框的距离),layout_margin为外边距(它的上一层与边框的距离)。

接下来为账号的文本和输入框,首先是账号的文本,代码如下:

XML/HTML代码

  1. <TextView   
  2. android:id="@+id/login_user_input"   
  3. android:layout_width="wrap_content"   
  4. android:layout_height="wrap_content"   
  5. android:layout_alignParentTop="true"   
  6. android:layout_marginTop="5dp"   
  7. android:text="@string/login_label_username"   
  8. style="@style/normalText"/>   
<TextView 
android:id="@+id/login_user_input" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentTop="true" 
android:layout_marginTop="5dp" 
android:text="@string/login_label_username" 
style="@style/normalText"/> 

android:layout_alignParentTop 这里表示此TextView的位置处于顶部

android:layout_marginTop="5dp" 这里表示此TextView的边框与RelativeLayout的顶部边框距离有5dp

这里需要对这个TextView设置下字体颜色和字体大小,定义在res/style.xml里面:

XML/HTML代码
<style name="normalText" parent="@android:style/TextAppearance">
<item name="android:textColor">#444</item>
<item name="android:textSize">14sp</item>
</style>
定义账号的输入框,如下:

XML/HTML代码

  1. <EditText   
  2. android:id="@+id/username_edit"   
  3. android:layout_width="fill_parent"   
  4. android:layout_height="wrap_content"   
  5. android:hint="@string/login_username_hint"   
  6. android:layout_below="@id/login_user_input"   
  7. android:singleLine="true"   
  8. android:inputType="text"/>   
<EditText 
android:id="@+id/username_edit" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:hint="@string/login_username_hint" 
android:layout_below="@id/login_user_input" 
android:singleLine="true" 
android:inputType="text"/> 

android:layout_below这里是设置为在账号的文本框的下面,android:singleLine 为单行输入(即你输入回车的时候不会在换行了),android:inputType这里text表示输入的类型为文本。

接下来再是密码文本和输入框,代码如下:

  1. <TextView    
  2.      android:id="@+id/login_password_input"    
  3.      android:layout_width="wrap_content"    
  4.      android:layout_height="wrap_content"    
  5.      android:layout_below="@id/username_edit"    
  6.      android:layout_marginTop="3dp"    
  7.      android:text="@string/login_label_password"    
  8.      style="@style/normalText"/>    
  9. <EditText    
  10.      android:id="@+id/password_edit"    
  11.      android:layout_width="fill_parent"    
  12.      android:layout_height="wrap_content"    
  13.      android:layout_below="@id/login_password_input"    
  14.      android:password="true"    
  15.      android:singleLine="true"    
  16.      android:inputType="textPassword"    
  17. <strong>/>  </strong>  
<TextView  
     android:id="@+id/login_password_input"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_below="@id/username_edit"  
     android:layout_marginTop="3dp"  
     android:text="@string/login_label_password"  
     style="@style/normalText"/>  
<EditText  
     android:id="@+id/password_edit"  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
     android:layout_below="@id/login_password_input"  
     android:password="true"  
     android:singleLine="true"  
     android:inputType="textPassword"  
<strong>/>  </strong>
登录按钮:

  1. XML/HTML代码  
  2. <Button    
  3.       android:id="@+id/signin_button"    
  4.       android:layout_width="wrap_content"    
  5.       android:layout_height="wrap_content"    
  6.       android:layout_below="@id/password_edit"    
  7.       android:layout_alignRight="@id/password_edit"    
  8.       android:text="@string/login_label_signin"    
  9.       android:background="@drawable/blue_button"    
  10. />   
XML/HTML代码
<Button  
      android:id="@+id/signin_button"  
      android:layout_width="wrap_content"  
      android:layout_height="wrap_content"  
      android:layout_below="@id/password_edit"  
      android:layout_alignRight="@id/password_edit"  
      android:text="@string/login_label_signin"  
      android:background="@drawable/blue_button"  
/> 
  1. <span style="color: rgb(51, 51, 51); font-size: 14px; line-height: 23px;"><span style="font-family:SimSun;"><strong>   第三部分:底下的文字和两张图片:</strong></span></span>  
<span style="color: rgb(51, 51, 51); font-size: 14px; line-height: 23px;"><span style="font-family:SimSun;"><strong>   第三部分:底下的文字和两张图片:</strong></span></span>
  1. <span style="color: rgb(51, 51, 51); font-size: 14px; line-height: 23px;"><span style="font-family:SimSun;"><strong><img src="https://img-blog.csdn.net/20150630144738211?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzA4MDUwOA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />  
  2. </strong></span></span>  
<span style="color: rgb(51, 51, 51); font-size: 14px; line-height: 23px;"><span style="font-family:SimSun;"><strong><img src="https://img-blog.csdn.net/20150630144738211?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzA4MDUwOA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
</strong></span></span>
  1. <span style="color: rgb(51, 51, 51); font-size: 14px; line-height: 23px;"><span style="font-family:SimSun;"><strong>还是一个RelativeLayout,但这里设置的很简单,代码如下:  
  2.   
  3. XML/HTML代码  
  4. </strong></span></span><pre name="code" class="html"><RelativeLayout    
  5.       android:layout_width="fill_parent"    
  6.       android:layout_height="wrap_content">    
  7. </RelativeLayout>  
<span style="color: rgb(51, 51, 51); font-size: 14px; line-height: 23px;"><span style="font-family:SimSun;"><strong>还是一个RelativeLayout,但这里设置的很简单,代码如下:

XML/HTML代码
</strong></span></span><pre name="code" class="html"><RelativeLayout  
      android:layout_width="fill_parent"  
      android:layout_height="wrap_content">  
</RelativeLayout>

 
  
  1. <span style="color: rgb(51, 51, 51); font-size: 14px; line-height: 23px;"><span style="font-family:SimSun;"><strong>"没有账号?注册"这几个文字定义在string里面,包含了一个<a>标签:  
  2.   
  3. XML/HTML代码  
  4. </strong><string name="login_register_link">没有帐号? <a href="#" mce_href="#">注册</a></string>  <strong>  
  5.        定义如下:  
  6.   
  7. XML/HTML代码  
  8. </strong><TextView  android:id="@+id/register_link"    
  9.     android:text="@string/login_register_link"    
  10.     android:layout_width="wrap_content"    
  11.     android:layout_height="wrap_content"    
  12.     android:layout_marginLeft="15dp"    
  13.     android:textColor="#888"    
  14.     android:textColorLink="#FF0066CC"    
  15. />  <strong>  
  16.  TextView是支持简单的html标签的,如<a>标签,但并不是支持所有标签,支持更复杂的html标签得用webView组件。  
  17. </strong></span></span>  
<span style="color: rgb(51, 51, 51); font-size: 14px; line-height: 23px;"><span style="font-family:SimSun;"><strong>"没有账号?注册"这几个文字定义在string里面,包含了一个<a>标签:

XML/HTML代码
</strong><string name="login_register_link">没有帐号? <a href="#" mce_href="#">注册</a></string>  <strong>
       定义如下:

XML/HTML代码
</strong><TextView  android:id="@+id/register_link"  
    android:text="@string/login_register_link"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_marginLeft="15dp"  
    android:textColor="#888"  
    android:textColorLink="#FF0066CC"  
/>  <strong>
 TextView是支持简单的html标签的,如<a>标签,但并不是支持所有标签,支持更复杂的html标签得用webView组件。
</strong></span></span>
  1. <span style="color: rgb(51, 51, 51); font-size: 14px; line-height: 23px;"><span style="font-family:SimSun;"><strong><span style="color: rgb(51, 51, 51); font-size: 14px; line-height: 23px;">卡通图片:</span>  
  2. </strong></span></span>  
<span style="color: rgb(51, 51, 51); font-size: 14px; line-height: 23px;"><span style="font-family:SimSun;"><strong><span style="color: rgb(51, 51, 51); font-size: 14px; line-height: 23px;">卡通图片:</span>
</strong></span></span>
  1. <span style="color: rgb(51, 51, 51); line-height: 23px;"><span style="font-family:SimSun;"><span style="color: rgb(51, 51, 51); line-height: 23px;"></span></span></span><pre name="code" class="html"><strong style="font-size: 14px;">XML/HTML代码  
  2. </strong><span style="font-family:SimSun;font-size: 14px;"><ImageView android:id="@+id/miniTwitter_logo"    
  3.         android:src="@drawable/cat"    
  4.         android:layout_width="wrap_content"    
  5.         android:layout_height="wrap_content"    
  6.         android:layout_alignParentRight="true"    
  7.         android:layout_alignParentBottom="true"    
  8.         android:layout_marginRight="25dp"    
  9.         android:layout_marginLeft="10dp"    
  10.         android:layout_marginBottom="25dp"    
  11. />  </span><strong style="font-size: 14px;">  
  12.   </strong><pre name="code" class="html" style="font-size: 14px;"><strong>左边是一个带文字的图片的ImageView:  
  13.   
  14. XML/HTML代码  
  15. </strong><ImageView android:src="@drawable/logo"    
  16.      android:layout_width="wrap_content"    
  17.      android:layout_height="wrap_content"    
  18.      android:layout_toLeftOf="@id/miniTwitter_logo"    
  19.      android:layout_alignBottom="@id/miniTwitter_logo"    
  20.      android:paddingBottom="8dp"    
  21. />    
<span style="color: rgb(51, 51, 51); line-height: 23px;"><span style="font-family:SimSun;"><span style="color: rgb(51, 51, 51); line-height: 23px;"></span></span></span><pre name="code" class="html"><strong style="font-size: 14px;">XML/HTML代码
</strong><span style="font-family:SimSun;font-size: 14px;"><ImageView android:id="@+id/miniTwitter_logo"  
        android:src="@drawable/cat"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentRight="true"  
        android:layout_alignParentBottom="true"  
        android:layout_marginRight="25dp"  
        android:layout_marginLeft="10dp"  
        android:layout_marginBottom="25dp"  
/>  </span><strong style="font-size: 14px;">
  </strong><pre name="code" class="html" style="font-size: 14px;"><strong>左边是一个带文字的图片的ImageView:

XML/HTML代码
</strong><ImageView android:src="@drawable/logo"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_toLeftOf="@id/miniTwitter_logo"  
     android:layout_alignBottom="@id/miniTwitter_logo"  
     android:paddingBottom="8dp"  
/>  
  1. <strong style="font-family: SimSun; font-size: 12px;"><span style="font-family:SimSun;"><span style="font-size:14px;"><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;">实现miniTwitter登陆界面的具体步骤</span></span></span></span></strong>  
<strong style="font-family: SimSun; font-size: 12px;"><span style="font-family:SimSun;"><span style="font-size:14px;"><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;">实现miniTwitter登陆界面的具体步骤</span></span></span></span></strong>
  1. <strong style="font-family: SimSun;font-size:14px;">具体步骤如下:</strong>  
<strong style="font-family: SimSun;font-size:14px;">具体步骤如下:</strong>
  1. <span style="font-family: SimSun;font-size:14px;"></span><pre name="code" class="html"><strong>第一步:一些字符串定义  
  2. res/values/strings.xml  
  3.   
  4. XML/HTML代码  
  5. </strong><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>  
  6. <resources>  
  7.   
  8.     <string name="app_name">MiniTwitterSimulate</string>  
  9.     <string name="action_settings">Settings</string>  
  10.     <string name="hello_world">Hello world!</string>  
  11.     <string name="tvName">用户名</string>  
  12.     <string name="tvPassword">密码</string>  
  13.     <string name="btnLogin">登录</string>  
  14.     <string name="tvRegister">没有帐号? <a href="#" mce_href="#">注册</a></string>  
  15.     <string name="remember">记住密码</string>  
  16.     <string name="title_activity_login2">Login2Activity</string>  
  17.       <string name="tv_success">登录成功!</string>  
  18. </resources>  
<span style="font-family: SimSun;font-size:14px;"></span><pre name="code" class="html"><strong>第一步:一些字符串定义
res/values/strings.xml

XML/HTML代码
</strong><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">MiniTwitterSimulate</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="tvName">用户名</string>
    <string name="tvPassword">密码</string>
    <string name="btnLogin">登录</string>
    <string name="tvRegister">没有帐号? <a href="#" mce_href="#">注册</a></string>
    <string name="remember">记住密码</string>
    <string name="title_activity_login2">Login2Activity</string>
      <string name="tv_success">登录成功!</string>
</resources>
第二步:res/values/style.xmlXML/HTML代码<?xml version="1.0" encoding="utf-8"?> <resources> <style name="normalText" parent="@android:style/TextAppearance"> <item name="android:textColor">#444</item> <item name="android:textSize">14sp</item> </style> </resources> 第三步:背景色为渐变色res/drawable-mdpi/background_login.xmlXML/HTML代码<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:startColor="#FFACDAE5" android:endColor="#FF72CAE1" android:angle="45" /> </shape> 第四步:背景色味淡蓝色且为圆角res/drawable-mdpi/background_login_div_bg.xmlXML/HTML代码<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#55FFFFFF" /> <!-- 设置圆角 注意: bottomRightRadius是左下角而不是右下角 bottomLeftRadius右下角--> <corners android:topLeftRadius="10dp" android:topRightRadius="10dp" android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"/> </shape> 第五步:
res/layout/login_top.xmlXML/HTML代码
 
  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="@drawable/btnbg_roundcorner"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin" >  
  10.   
  11.     <TextView  
  12.         android:id="@+id/tvUsername"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentLeft="true"  
  16.         android:layout_alignParentTop="true"  
  17.         android:text="@string/tvName"  
  18.         android:textAppearance="?android:attr/textAppearanceMedium" />  
  19.   
  20.     <EditText  
  21.         android:id="@+id/etUsername"  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_alignLeft="@+id/tvUsername"  
  25.         android:layout_below="@+id/tvUsername"  
  26.         android:background="@android:drawable/edit_text"  
  27.         android:ems="10" >  
  28.   
  29.         <requestFocus />  
  30.     </EditText>  
  31.   
  32.     <TextView  
  33.         android:id="@+id/tvPassword"  
  34.         android:layout_width="wrap_content"  
  35.         android:layout_height="wrap_content"  
  36.         android:layout_alignLeft="@+id/etUsername"  
  37.         android:layout_below="@+id/etUsername"  
  38.         android:text="@string/tvPassword"  
  39.         android:textAppearance="?android:attr/textAppearanceMedium" />  
  40.   
  41.     <EditText  
  42.         android:id="@+id/etPassword"  
  43.         android:layout_width="match_parent"  
  44.         android:layout_height="wrap_content"  
  45.         android:layout_alignLeft="@+id/tvPassword"  
  46.         android:layout_below="@+id/tvPassword"  
  47.         android:layout_marginTop="16dp"  
  48.         android:background="@android:drawable/edit_text"  
  49.         android:ems="10"  
  50.         android:inputType="textPassword" />  
  51.   
  52.     <Button  
  53.         android:id="@+id/btnLogin"  
  54.         android:layout_width="wrap_content"  
  55.         android:layout_height="wrap_content"  
  56.         android:layout_alignRight="@+id/etPassword"  
  57.         android:layout_below="@+id/etPassword"  
  58.         android:layout_marginTop="20dp"  
  59.         android:background="#FF72CAE1"  
  60.         android:text="@string/btnLogin" />  
  61.   
  62.     <CheckBox  
  63.         android:id="@+id/rememberpassword"  
  64.         android:layout_width="wrap_content"  
  65.         android:layout_height="wrap_content"  
  66.         android:layout_alignLeft="@+id/etPassword"  
  67.         android:layout_alignTop="@+id/btnLogin"  
  68.         android:layout_marginLeft="19dp"  
  69.         android:checked="true"  
  70.         android:text="@string/remember" />  
  71.   
  72. </RelativeLayout>  
<?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="wrap_content"
    android:background="@drawable/btnbg_roundcorner"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/tvUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/tvName"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etUsername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvUsername"
        android:layout_below="@+id/tvUsername"
        android:background="@android:drawable/edit_text"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/tvPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etUsername"
        android:layout_below="@+id/etUsername"
        android:text="@string/tvPassword"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvPassword"
        android:layout_below="@+id/tvPassword"
        android:layout_marginTop="16dp"
        android:background="@android:drawable/edit_text"
        android:ems="10"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/etPassword"
        android:layout_below="@+id/etPassword"
        android:layout_marginTop="20dp"
        android:background="#FF72CAE1"
        android:text="@string/btnLogin" />

    <CheckBox
        android:id="@+id/rememberpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etPassword"
        android:layout_alignTop="@+id/btnLogin"
        android:layout_marginLeft="19dp"
        android:checked="true"
        android:text="@string/remember" />

</RelativeLayout>
第六步:
 
  
  1. <pre name="code" class="html" style="font-size:14px; font-family: SimSun; color: rgb(51, 51, 51); line-height: 23px;"><strong></strong><pre name="code" class="html">res/layout/login_bottom.xml  
<pre name="code" class="html" style="font-size:14px; font-family: SimSun; color: rgb(51, 51, 51); line-height: 23px;"><strong></strong><pre name="code" class="html">res/layout/login_bottom.xml
 
  
  1. <pre name="code" class="html" style="font-family: SimSun;"><span style="font-size:10px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content" >  
  5.   
  6.     <TextView  
  7.         android:id="@+id/tvRegist"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"</span><span style="font-size:14px;">  
  10.     </span><span style="font-size:10px;">    android:layout_alignParentLeft="true"  
  11.         android:layout_alignParentTop="true"  
  12.         android:layout_marginLeft="21dp"  
  13.         android:layout_marginTop="18dp"  
  14.         android:text="@string/tvRegister"  
  15.           
  16.         android:textColorLink="#FF0066CC" />  
  17.   
  18.     <ImageView  
  19.         android:id="@+id/imageView1"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_alignParentBottom="true"  
  23.         android:layout_alignParentRight="true"  
  24.         android:layout_marginBottom="24dp"  
  25.         android:src="@drawable/panda" />  
  26.   
  27.     <ImageView  
  28.         android:id="@+id/imageView2"  
  29.         android:layout_width="wrap_content"  
  30.         android:layout_height="wrap_content"  
  31.         android:layout_alignParentBottom="true"  
  32.         android:layout_centerHorizontal="true"  
  33.         android:layout_marginBottom="28dp"  
  34.         android:src="@drawable/icon" />  
  35.   
  36. </RelativeLayout></span>  
<pre name="code" class="html" style="font-family: SimSun;"><span style="font-size:10px;"><?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="wrap_content" >

    <TextView
        android:id="@+id/tvRegist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"</span><span style="font-size:14px;">
    </span><span style="font-size:10px;">    android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="21dp"
        android:layout_marginTop="18dp"
        android:text="@string/tvRegister"
        
        android:textColorLink="#FF0066CC" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="24dp"
        android:src="@drawable/panda" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="28dp"
        android:src="@drawable/icon" />

</RelativeLayout></span>

第七步
 
  
  1. <pre name="code" class="html"><strong>res/layout/activity_main.xml</strong>  
<pre name="code" class="html"><strong>res/layout/activity_main.xml</strong>
 
  
  1. <pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:background="@drawable/loginbg"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     tools:context=".LoginActivity" >  
  12.   
  13.   <include layout="@layout/login_top"/>  
  14.    
  15.   <include layout="@layout/login_bottom"/>"  
  16.   
  17. </LinearLayout>  
<pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/loginbg"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".LoginActivity" >

  <include layout="@layout/login_top"/>
 
  <include layout="@layout/login_bottom"/>"

</LinearLayout>
  1. <strong>第八步:</strong>  
<strong>第八步:</strong>
  1. <strong>LoginActivity.java:</strong>  
<strong>LoginActivity.java:</strong>
  1. <pre name="code" class="html">package com.example.minitwittersimulate;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.content.SharedPreferences;  
  7. import android.content.SharedPreferences.Editor;  
  8. import android.text.Editable;  
  9. import android.text.TextUtils;  
  10. import android.view.Menu;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.view.Window;  
  14. import android.widget.Button;  
  15. import android.widget.CheckBox;  
  16. import android.widget.EditText;  
  17. import android.widget.TextView;  
  18. import android.widget.Toast;  
  19.   
  20. public class LoginActivity extends Activity {  
  21. private Button btnLogin;  
  22. private EditText etUsername;  
  23.   
  24. private EditText etPassword;  
  25. private CheckBox rememberpassword;  
  26.   
  27. // 声明一个SharedPreferences用于保存数据  
  28. private SharedPreferences setting = null;  
  29. private static final String PREFS_NAME = "NamePwd";  
  30.     @Override  
  31.     protected void onCreate(Bundle savedInstanceState) {  
  32.         super.onCreate(savedInstanceState);  
  33.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  34.         setContentView(R.layout.activity_main);  
  35.         etUsername=(EditText)findViewById(R.id.etUsername);  
  36.         etPassword=(EditText)findViewById(R.id.etPassword);  
  37.         rememberpassword=(CheckBox)findViewById(R.id.rememberpassword);  
  38.         btnLogin=(Button)findViewById(R.id.btnLogin);  
  39.         setListener() ;  
  40.         getData();  
  41.     }  
  42.     private void setListener() {  
  43.         // 为登录按钮绑定事件  
  44.         btnLogin.setOnClickListener(new OnClickListener() {  
  45.             String username=etUsername.getText().toString();  
  46.             String password=etPassword.getText().toString();  
  47.             @Override  
  48.             public void onClick(View arg0) {  
  49.                 if(!(TextUtils.isEmpty(username))&&(TextUtils.isEmpty(password)))  
  50.                 {  
  51.                 // 判断用户名和密码  
  52.                 if ("wch".equals(username)&&"wch".equals(password))  
  53.                 {  
  54.                     // 判断复选框是否选中  
  55.                     /*if (rememberpassword.isChecked())  
  56.                     {  
  57.                         setting = getSharedPreferences(PREFS_NAME,  
  58.                                 MODE_PRIVATE);  
  59.                         // 得到Editor对象  
  60.                         Editor edit = setting.edit();  
  61.                         // 记录保存标记  
  62.                         edit.putBoolean("rememberpassword", true);  
  63.                         // 记录用户名  
  64.                         edit.putString("username",username);  
  65.                         // 记录密码  
  66.                         edit.putString("password",password);  
  67.                         edit.commit(); // 一定记得提交  
  68.                     }  
  69.                       
  70.                     else   
  71.                     {  
  72.                         settinggetSharedPreferences(PREFS_NAME,  
  73.                                 MODE_PRIVATE);  
  74.                         // 得到Editor对象  
  75.                         Editor edit = setting.edit();  
  76.                         // 记录保存标记  
  77.                         edit.putBoolean("rememberpassword", false);  
  78.                         // 记录用户名  
  79.                         edit.putString("username", "");  
  80.                         // 记录密码  
  81.                         edit.putString("password", "");  
  82.                         edit.commit(); // 一定记得提交  
  83.                     }  
  84.                     */  
  85.                       
  86.                     // 跳转到首页  
  87.                     Intent intent = new Intent(LoginActivity.this,  
  88.                             Login2Activity.class);  
  89.                     startActivity(intent);  
  90.                     finish();  
  91.                 }   
  92.                 }  
  93.                 else   
  94.                 {  
  95.                     // 显示错误提示  
  96.                     Toast.makeText(getApplicationContext(), "用户名或密码错误",  
  97.                             Toast.LENGTH_SHORT).show();  
  98.                 }  
  99.                   
  100.             }  
  101.         });  
  102.     }  
  103.   
  104.   
  105.     @Override  
  106.     public boolean onCreateOptionsMenu(Menu menu) {  
  107.         // Inflate the menu; this adds items to the action bar if it is present.  
  108.         getMenuInflater().inflate(R.menu.main, menu);  
  109.         return true;  
  110.     }  
  111.     protected void onResume() {  
  112.         // 在界面显示数据之前得到之前存储的数据  
  113.         super.onResume();  
  114.         getData();  
  115.     }  
  116.   
  117.     /**  
  118.      * 获取存储是数据  
  119.      */  
  120.     private void getData() {  
  121.         // 得到sharedpreferences对象  
  122.         setting = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);  
  123.         // 判断是否之前存储过用户名密码  
  124.         if (setting.getBoolean("isKeep", false)) {  
  125.             // 如果之前存储过,则显示在相应文本框内  
  126.             etUsername.setText(setting.getString("username", ""));  
  127.         etPassword.setText(setting.getString("password", ""));  
  128.         } else {  
  129.             // 否则显示空  
  130.             etUsername.setText("");  
  131.             etPassword.setText("");  
  132.         }  
  133.     }  
  134.   
  135. }  
<pre name="code" class="html">package com.example.minitwittersimulate;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.Editable;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {
private Button btnLogin;
private EditText etUsername;

private EditText etPassword;
private CheckBox rememberpassword;

// 声明一个SharedPreferences用于保存数据
private SharedPreferences setting = null;
private static final String PREFS_NAME = "NamePwd";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		etUsername=(EditText)findViewById(R.id.etUsername);
		etPassword=(EditText)findViewById(R.id.etPassword);
		rememberpassword=(CheckBox)findViewById(R.id.rememberpassword);
		btnLogin=(Button)findViewById(R.id.btnLogin);
		setListener() ;
		getData();
	}
	private void setListener() {
		// 为登录按钮绑定事件
		btnLogin.setOnClickListener(new OnClickListener() {
			String username=etUsername.getText().toString();
			String password=etPassword.getText().toString();
			@Override
			public void onClick(View arg0) {
				if(!(TextUtils.isEmpty(username))&&(TextUtils.isEmpty(password)))
				{
				// 判断用户名和密码
				if ("wch".equals(username)&&"wch".equals(password))
				{
					// 判断复选框是否选中
					/*if (rememberpassword.isChecked())
					{
						setting = getSharedPreferences(PREFS_NAME,
								MODE_PRIVATE);
						// 得到Editor对象
						Editor edit = setting.edit();
						// 记录保存标记
						edit.putBoolean("rememberpassword", true);
						// 记录用户名
						edit.putString("username",username);
						// 记录密码
						edit.putString("password",password);
						edit.commit(); // 一定记得提交
					}
					
					else 
					{
						setting= getSharedPreferences(PREFS_NAME,
								MODE_PRIVATE);
						// 得到Editor对象
						Editor edit = setting.edit();
						// 记录保存标记
						edit.putBoolean("rememberpassword", false);
						// 记录用户名
						edit.putString("username", "");
						// 记录密码
						edit.putString("password", "");
						edit.commit(); // 一定记得提交
					}
					*/
					
					// 跳转到首页
					Intent intent = new Intent(LoginActivity.this,
							Login2Activity.class);
					startActivity(intent);
					finish();
				} 
				}
				else 
				{
					// 显示错误提示
					Toast.makeText(getApplicationContext(), "用户名或密码错误",
							Toast.LENGTH_SHORT).show();
				}
				
			}
		});
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	protected void onResume() {
		// 在界面显示数据之前得到之前存储的数据
		super.onResume();
		getData();
	}

	/**
	 * 获取存储是数据
	 */
	private void getData() {
		// 得到sharedpreferences对象
		setting = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
		// 判断是否之前存储过用户名密码
		if (setting.getBoolean("isKeep", false)) {
			// 如果之前存储过,则显示在相应文本框内
			etUsername.setText(setting.getString("username", ""));
		etPassword.setText(setting.getString("password", ""));
		} else {
			// 否则显示空
			etUsername.setText("");
			etPassword.setText("");
		}
	}

}
  1. <span style="font-weight: bold; font-family: SimSun;">Login2Activity.java:</span>  
<span style="font-weight: bold; font-family: SimSun;">Login2Activity.java:</span>
  1. package com.example.minitwittersimulate;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.content.SharedPreferences;  
  7. import android.content.SharedPreferences.Editor;  
  8.   
  9. import android.text.TextUtils;  
  10. import android.view.Menu;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13. import android.widget.Toast;  
  14.   
  15. public class Login2Activity extends Activity {  
  16.       
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_login2);  
  21.           
  22.     }  
  23.   
  24.     @Override  
  25.     public boolean onCreateOptionsMenu(Menu menu) {  
  26.         // Inflate the menu; this adds items to the action bar if it is present.  
  27.         getMenuInflater().inflate(R.menu.login2, menu);  
  28.         return true;  
  29.     }  
  30.   
  31. }  
package com.example.minitwittersimulate;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import android.text.TextUtils;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login2Activity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login2);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login2, menu);
		return true;
	}

}
  1. <strong>MainActivity.java:</strong>  
<strong>MainActivity.java:</strong>
  1. <pre name="code" class="html">package com.example.minitwittersimulate;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.Window;  
  7.   
  8. public class MainActivity extends Activity {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  14.         setContentView(R.layout.activity_main);  
  15.     }  
  16.   
  17.     @Override  
  18.     public boolean onCreateOptionsMenu(Menu menu) {  
  19.         // Inflate the menu; this adds items to the action bar if it is present.  
  20.         getMenuInflater().inflate(R.menu.main, menu);  
  21.         return true;  
  22.     }  
  23.   
  24. }  
<pre name="code" class="html">package com.example.minitwittersimulate;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
  1. <strong>运行结果图:</strong>  
<strong>运行结果图:</strong>
如果输入的用户名或者密码不正确

  
 
  
  1. 输入正确:  
输入正确:
  1. <img src="https://img-blog.csdn.net/20150630152116599?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzA4MDUwOA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />  
<img src="https://img-blog.csdn.net/20150630152116599?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzA4MDUwOA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
  1.   
</pre><pre>
  1.   
</pre><strong style="FONT-FAMILY: SimSun; FONT-SIZE: 14px"></strong><pre>
  
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值