Android的编程 – 帖子 8: 意图 – 屏幕之间切换
由 7 以前的帖子基本, 因为你掌握的界面设计如何,以及启动事件按钮的相对稳定. 而且我也相信,如果你做你的轨道系列打算进一步 – 意味着比别人用功非常好奇的孩子摸索着她的家是怎样按钮点击可以打开另一个屏幕. 今天,大家都会做,通过一个简单的应用程序 – 注册账号.
[qads]
本申请的原理简单介绍如下: 你必须输入用户名和密码登录到系统. 如果为true,移动到一个新的屏幕,开始使用该系统. 如果用户的账户,你可以注册一个帐号, 注册后,返回登录界面和自动填写登录名框中. 因此,我们有以下的应用示意图:

已经, 现在你打开Android Studio并创建一个新项目. 注意一点就是在目前的时间, Android的工作室使我们能够创建一个登录活动, 然而,我没有使用它,因为它会自动生成大量的代码, 解释所有这些对你的代码,它需要大量的时间,并不会集中在这个职位. 所以问问你自己了他们的研究下线的代码. 在这里,我从空创建活动.
创建当你的项目被命名活动LoginActivity, 文件中的XMLLàactivity_login, 标题Là登录. 该代码需要暂时无法离线的java. 现在让我们创建笔记 2 剩余活性为.
首先创建活动注册 (注册). 右键点击你的包, 选 新 - >活动 - >空Activty

接下来你的名和文件名接口活动, 经常自动把雅典的名义下活动.

注意:您 取消勾选启动活动 NHE. 如果融入活动,当你打开应用程序将打开.
同样, 我们创建笔记主要活动 – 登录界面,成功.
现在开始设计接口和Java代码.
首先你要定义在文件/res/values/string.xml变量
<resources>
<string name="app_name">TUT8Intent</string>
<!-- Strings related to login -->
<string name="login">Login</string>
<string name="user_name">User name</string>
<string name="password">Password</string>
<string name="confirm_password">Confirm Password</string>
<string name="register">Register</string>
<string name="action_sign_in_short">Sign in</string>
<string name="error_field_required">This field is required</string>
<string name="error_password_not_match">Password not match</string>
<string name="login_success">Hi, {0}. You login with password is {1}</string>
</resources>
我们添加库通过打开文件来使用TextInputLayout设计相结合的EditText build.gradle 和编辑 依赖 到:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:design:24.0.0'
}
在所有的接口上, 不难看出,我们可以使用便捷的LinearLayout. 下面是LoginActivity代码接口.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp">
<!-- 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: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">
<EditText
android:id="@+id/editUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/user_name"
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/editPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:inputType="textPassword"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:text="@string/login" />
</LinearLayout>
</ScrollView>
</LinearLayout>
单单这一节将不解释任何东西,除了对象 进度条 此显示作为对象的等待, 它会旋转 (或水平, 但这里是圆的) 以指示用户的等待我们正在处理的作业.
性能 机器人:能见度 值得 走了 将使其隐藏起来, 当你想让它显示出来,然后将该值设置为它 可见
登录活动处理的Java代码如下:
package com.nguyenvanquan7826.tut8intent;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
public static final String KEY_USER_TO_MAIN = "KEY_USER_TO_MAIN";
public static final String KEY_PASSWORD_TO_MAIN = "KEY_PASSWORD_TO_MAIN";
public static final String KEY_USER_FROM_REGISTER = "KEY_USER_FROM_REGISTER";
public static final int REQUEST_CODE_REGISTER = 1;
private Context context;
private EditText editUserName;
private EditText editPassword;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
context = this;
connectView();
}
private void connectView() {
editUserName = (EditText) findViewById(R.id.editUserName);
editPassword = (EditText) findViewById(R.id.editPassword);
progressBar = (ProgressBar) findViewById(R.id.progressLogin);
findViewById(R.id.btnLogin).setOnClickListener(this);
findViewById(R.id.btnRegister).setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.btnLogin:
login();
break;
case R.id.btnRegister:
register();
break;
}
}
private void login() {
boolean error = false;
showProgress(true);
// when process we must have sometime
// get data
String userName = editUserName.getText().toString().trim();
String password = editPassword.getText().toString().trim();
// password empty
if (TextUtils.isEmpty(password)) {
editPassword.requestFocus();
editPassword.setError(context.getResources().getString(R.string.error_field_required));
error = true;
}
// username empty
if (TextUtils.isEmpty(userName)) {
editUserName.requestFocus();
editUserName.setError(context.getResources().getString(R.string.error_field_required));
error = true;
}
// all data is ok
showProgress(false);
if (!error) {
// create intent to show Main Activity
Intent intent = new Intent(context, MainActivity.class);
// send data if need
intent.putExtra(KEY_USER_TO_MAIN, userName);
intent.putExtra(KEY_PASSWORD_TO_MAIN, password);
// start Main Activity
startActivity(intent);
}
}
private void register() {
Intent intent = new Intent(context, RegisterActivity.class);
startActivityForResult(intent, REQUEST_CODE_REGISTER);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_REGISTER && resultCode == Activity.RESULT_OK) {
String userName = data.getStringExtra(KEY_USER_FROM_REGISTER);
editUserName.setText(userName);
editPassword.requestFocus();
}
}
private void showProgress(boolean isShow) {
progressBar.setVisibility(isShow ? View.VISIBLE : View.GONE);
findViewById(R.id.login_form).setVisibility(!isShow ? View.VISIBLE : View.GONE);
}
}
在这段代码需要注意:
– 在登录功能, 我们已经知道 2 时间 显示进度(真正); 和 显示进度(假); 即当初始治疗,将需要一段时间才能显示出来进度, 然后处理完皮. 但是,由于我们工作的如此之快,你不会看到弹出的进展.
- 毕竟数据是确定的,然后开始服用MainActivity意图叫出. 在这里,你的注意力:
// create intent to show Main Activity Intent intent = new Intent(context, MainActivity.class); // send data if need intent.putExtra(KEY_USER_TO_MAIN, userName); intent.putExtra(KEY_PASSWORD_TO_MAIN, password); // start Main Activity startActivity(intent);
+/ 第一行是我们创建意图从活动转移到另一个活动的方式. 在这方面是上下文 – 目前活动, MainActivity.class是我们需要移动到活动.
+/ 第一线 2,3 我们使用意图把一些数据, 这里给出通过对MainActivity用户名和密码 1 关键字是KEY_USER_TO_MAIN和KEY_PASSWORD_TO_MAIN, 这是 2 常数, 它也是关键 – 关键的另一边,我们可以得到完全其中username, 哪里是密码.
+/ 最后一行是启动切换到新活动. -
你看,我已经设置错误,以EditText上的EditText用户名和密码有很大的不同. 当错误,我们有漂亮的界面如下:

- 当呼叫 登记, 其意图创建一个类似的, 但是注册后的召回过程中你完成, 我们需要填写您刚刚注册的用户名的EditText名, 所以我不得不打电话如何结束RegisterActivity将收到返回的结果后,通过命令用户名:
startActivityForResult(意图, REQUEST_CODE_REGISTER);
=>启动软件包 – 活动启动, ForResult被允许返回结果. 这里, REQUEST_CODE_REGISTER是整数, 这也是我们去了解,如果返回的结果是从公共活动支付的关键, 因为在实际中, 1 Activty可以调用和更活动活动也可以将结果返回到原始Activty. -
要接收电子数据返回注册, 我们必须重写功能 的onActivityResult – 当返回的结果活动. 这里:
请求代码 主要的区别是,当我们呼吁公众活动代码, 得到RegisterAcitivty的正确的结果, 我们需要检查一下,看看比赛REQUEST_CODE_REGISTER不.
发送resultCode 确认代码视图RegisterAcitivty接受返回结果不, 如果它是RESULT_OK.
然后,我们打通的结果 数据意向.
此时你绝对不能运行的应用程序, 但在MainActivity和RegisterActivity我们什么. 所以现在再次.
接口代码主要活动:
<?xml version="1.0" encoding="utf-8"?>
<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: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.nguyenvanquan7826.tut8intent.MainActivity">
<TextView
android:id="@+id/tvMain"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
JAVA代码主要活动:
package com.nguyenvanquan7826.tut8intent;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Context context;
private TextView tvMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
connectView();
getData();
}
private void connectView() {
tvMain = (TextView) findViewById(R.id.tvMain);
}
private void getData() {
String userName = getIntent().getStringExtra(LoginActivity.KEY_USER_TO_MAIN);
String password = getIntent().getStringExtra(LoginActivity.KEY_PASSWORD_TO_MAIN);
String helloText = context.getResources().getString(R.string.login_success);
helloText = completeText(helloText, new String[]{userName, password});
tvMain.setText(helloText);
}
private String completeText(String source, String[] items) {
for (int i = 0; i < items.length; i++) {
source = source.replace("{" + i + "}", items[i]);
}
return source;
}
}
以上, 我们取的数据与命令 getIntent().getStringExtra, 采取任何价值,它必须发送相应的键和完全一样的,当我们作为发送. 现在你注意到我为什么把KEY_USER_TO_MAIN和常量KEY_PASSWORD_TO_MAIN? 为了避免混乱, 在很多地方, 如果字符串传递作为一个男人,你要记住它的钥匙,在许多地方正确地写. 当我们把每一个名字,每一天,是确定, 不必担心说错话沸腾.
与注册离线最近活动继续. 它类似于登录,所以你只关心如何将数据发送回味儿LoginActivity.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp">
<!-- Login progress -->
<ProgressBar
android:id="@+id/progressLogin"
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: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">
<EditText
android:id="@+id/editUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/user_name"
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/editPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:inputType="textPassword"
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/editRePassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/confirm_password"
android:inputType="textPassword"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/btnRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:text="@string/register" />
</LinearLayout>
</ScrollView>
</LinearLayout>
RegisterActivity Java代码
package com.nguyenvanquan7826.tut8intent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
private Context context;
private EditText editUserName;
private EditText editPassword;
private EditText editRePassword;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
context = this;
connectView();
}
private void connectView() {
editUserName = (EditText) findViewById(R.id.editUserName);
editPassword = (EditText) findViewById(R.id.editPassword);
editRePassword = (EditText) findViewById(R.id.editRePassword);
progressBar = (ProgressBar) findViewById(R.id.progressLogin);
findViewById(R.id.btnRegister).setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.btnRegister:
register();
break;
}
}
private void register() {
boolean error = false;
showProgress(true);
// when process we must have sometime
// get data
String userName = editUserName.getText().toString().trim();
String password = editPassword.getText().toString().trim();
String rePassword = editRePassword.getText().toString().trim();
// password empty
if (TextUtils.isEmpty(rePassword)) {
editRePassword.requestFocus();
editRePassword.setError(context.getResources().getString(R.string.error_field_required));
error = true;
}
// password empty
if (TextUtils.isEmpty(password)) {
editPassword.requestFocus();
editPassword.setError(context.getResources().getString(R.string.error_field_required));
error = true;
}
// username empty
if (TextUtils.isEmpty(userName)) {
editUserName.requestFocus();
editUserName.setError(context.getResources().getString(R.string.error_field_required));
error = true;
}
if (!password.equals(rePassword)) {
editRePassword.requestFocus();
editRePassword.setError(context.getResources().getString(R.string.error_password_not_match));
error = true;
}
// all data is ok
showProgress(false);
if (!error) {
// create intent to send data back Login Activity
Intent intent = new Intent();
// send data
intent.putExtra(LoginActivity.KEY_USER_FROM_REGISTER, userName);
setResult(RESULT_OK, intent);
finish();
}
}
private void showProgress(boolean isShow) {
progressBar.setVisibility(isShow ? View.VISIBLE : View.GONE);
findViewById(R.id.login_form).setVisibility(!isShow ? View.VISIBLE : View.GONE);
}
}



贵重的, 弟子收到ķ先生〜
如果你想. 但有关阵列什么.
Android上可编程阵列先生
因此,只要学会去…
如果 (TextUtils.isEmpty(密码)) {
editPassword.requestFocus();
editPassword.setError(context.getResources().的getString(R.string.error_field_required));
错误= TRUE;
} 在其他值密码规则/ String.xml哈他?
没有’ 上述EDT声明的其他价值
由用户输入到该彩密码.
我的e故障停止屏幕做先生
这应该在一个新的logcat观看知道什么来纠正错误.
我的广告. e执行相同的,但马ķ斋DC . LUC它停止做梦了.
申请终止.
像这样的错误,你需要看到的logcat什么错误,如果它知道如何治愈.
嘿,为什么屏幕在MainActivity行中显示错误 22 , 34 , 40 先生
您可以看到它指出要修复的错误.
是的 , 我能修好他, Ck感谢 :ð
您好,
您的自学站点也很好?
是的,你. 创建自学编程页面,让您有写作和写作的爱好, 但是在那之后,没有人写^^.