先贴上要完成的效果图:
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代码
- <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>
接下来为账号的文本和输入框,首先是账号的文本,代码如下:
XML/HTML代码
- <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代码
- <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表示输入的类型为文本。
接下来再是密码文本和输入框,代码如下:
- <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>
- 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"
- />
- <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><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>
- <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>
- <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>
- <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>
- <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"
- />
- <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:14px;">具体步骤如下:</strong>
- <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>
- <?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>
- <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-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>
第七步:
- <pre name="code" class="html"><strong>res/layout/activity_main.xml</strong>
- <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>
- <strong>第八步:</strong>
- <strong>LoginActivity.java:</strong>
- <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("");
- }
- }
- }
- <span style="font-weight: bold; font-family: SimSun;">Login2Activity.java:</span>
- 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;
- }
- }
- <strong>MainActivity.java:</strong>
- <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;
- }
- }
- <strong>运行结果图:</strong>
- 输入正确:
- <img src="https://img-blog.csdn.net/20150630152116599?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzA4MDUwOA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />