您的位置:首页 > 移动开发 > Android开发

AndroidStudio创建的LoginActivity分析

2016-03-08 22:55 495 查看
从今天起记录一下对Android一些知识的学习吧。

感觉AS中生成的LoginActivity布局结构很简洁,稍加修改就能有很好的效果,所以学习一下这个Activity的代码。

首先是布局文件

<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:gravity="center_horizontal"
android:orientation="vertical"
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="com.dc.activity.LoginActivity">

<!-- Login progress -->
<ProgressBar
android:id="@+id/login_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone"/>

<ScrollView
android:id="@+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<AutoCompleteTextView
android:id="@+id/account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_account"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true"/>

</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_password"
android:imeActionId="@+id/login"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"/>

</android.support.design.widget.TextInputLayout>

<Button
android:id="@+id/email_sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/action_sign_in"
android:textStyle="bold"/>

</LinearLayout>
</ScrollView>
</LinearLayout>
LinearLayout下嵌套着一个gone的ProgressBar,和一个ScrollView,ScrollView下又是两个TextInputLayout和一个Button。很简洁的界面,有需要可以加上Toolbar。

成员变量

/**
* Id to identity READ_CONTACTS permission request.
*/
private static final int REQUEST_READ_CONTACTS = 0;

/**
* A dummy authentication store containing known user names and passwords.
* TODO: remove after connecting to a real authentication system.
*/
private static final String[] DUMMY_CREDENTIALS = new String[]{
"foo@example.com:hello", "bar@example.com:world"
};
/**
* Keep track of the login task to ensure we can cancel it if requested.
*/
private UserLoginTask mAuthTask = null;

// UI references.
private AutoCompleteTextView accountView;
private EditText mPasswordView;
private View mProgressView;
private View mLoginFormView;


DUMMY_CREDENTTALS用于模拟已存在的账户,冒号前为账户,冒号后为密码

mAuthTask是UserLoginTask的实例,UserLoginTask继承自AsyncTask,后面会提到这个的用处

然后是onCreate方法
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Set up the login form.
accountView = (AutoCompleteTextView) findViewById(R.id.account);
populateAutoComplete();

mPasswordView = (EditText) findViewById(R.id.password);
mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent)
{
if (id == R.id.login || id == EditorInfo.IME_NULL) {
attemptLogin();
return true;
}
return false;
}
});

Button mEmailSignInButton = (Button) findViewById(R.id.sign_in_button);
mEmailSignInButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
attemptLogin();
}
});

mLoginFormView = findViewById(R.id.login_form);
mProgressView = findViewById(R.id.login_progress);
}
onCreate方法里主要是进行了View的初始化和设置了一些监听事件,EditView设置的onEditorAction在按下回车的时候执行
方法最后两个mLoginFormView和mProgressView是用于获取显示的View,在登陆的时候可以进行登陆窗口gone,ProgressBar visible的操作。

还有一个就是在AutoCompleteEditText之后的populateAutoComplete()方法,按方法名来看是构造自动补全的列表,跟进

private void populateAutoComplete()
{
if (!mayRequestContacts()) {
return;
}

getLoaderManager().initLoader(0, null, this);
}


先是通过mayRequestContacts判断是否继续执行,若通过判断则初始化Loaders,通过Loaders后台异步读取用户的账户信息。
再看mayRequestContacts(),代码如下

private boolean mayRequestContacts()
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return true;
}
if (checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
return true;
}
if (shouldShowRequestPermissionRationale(READ_CONTACTS)) {
Snackbar.make(accountView, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE)
.setAction(android.R.string.ok, new View.OnClickListener()
{
@Override
@TargetApi(Build.VERSION_CODES.M)
public void onClick(View v)
{
requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
}
});
} else {
requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
}
return false;
}
可以看出这个方法是用于请求用户以获取读取账户的权限,主要是为了适配6.0新的权限机制

接下来在button的点击事件和EditText的回车事件中都能找到attemptLogin()方法,这个方法主要是初步判断输入的账户密码的合法性(是否为空,长度是否过小),并给出错误提示。通过初步检验后,隐藏登陆框和按钮,显示进度条,并在AsyncTask中进行后台登陆,这个AsyncTask就是上面的变量中的mAuthTask,使用的时候改写doInBackground方法实现自己的业务逻辑。

这些就是LoginActivity主要的代码理解了。


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: