package com.example.wang.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText et_usercode; EditText et_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); et_password=(EditText)findViewById(R.id.et_password);
et_usercode=(EditText)findViewById(R.id.et_usercode); } public void zhuce(View v)
{
Intent intent=new Intent(this,ZhuceActivity.class); startActivityForResult(intent,1);
} String username;
String usercode;
String password; @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); if (requestCode==1)
{
if (resultCode==RESULT_OK)
{ username=data.getStringExtra("name"); usercode=data.getStringExtra("code"); password=data.getStringExtra("pass");
}
}
} public void denglu(View v)
{
String et_code=et_usercode.getText().toString();
String et_pass=et_password.getText().toString(); if (et_code.trim().length()==0||et_pass.trim().length()==0)
{
Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
return;
} if (et_code==null||!et_code.equals(usercode))
{
Toast.makeText(MainActivity.this, "用户未注册", Toast.LENGTH_SHORT).show();
return;
} if (et_pass==null||!et_pass.equals(password))
{
Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
return;
}
else
{
Toast.makeText(this,"用户登录成功",Toast.LENGTH_LONG).show(); //传递信息到TestActivity页面
Intent intent=new Intent(this,TestActivity.class); intent.putExtra("code",usercode); intent.putExtra("name",username); startActivity(intent); } }
}

MainActivity

package com.example.wang.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class ZhuceActivity extends AppCompatActivity { EditText usercode; EditText username; EditText password; Button bt_quxiao; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zhuce); usercode=(EditText)findViewById(R.id.usercode);
username=(EditText)findViewById(R.id.username);
password=(EditText)findViewById(R.id.password); bt_quxiao=(Button)findViewById(R.id.bt_quxiao); bt_quxiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(RESULT_CANCELED,null);
finish();
}
});
} public void queding(View v)
{
String user_name=username.getText().toString(); if (user_name==null||user_name.trim().length()==0)
{
Toast.makeText(ZhuceActivity.this, "请正确输入用户名", Toast.LENGTH_SHORT).show();
return;
} String user_code=usercode.getText().toString(); if (user_code==null||user_code.trim().length()==0)
{
Toast.makeText(ZhuceActivity.this, "请正确输入用户代码", Toast.LENGTH_SHORT).show();
return;
} String pass_word=password.getText().toString(); if (pass_word==null||pass_word.trim().length()==0)
{
Toast.makeText(ZhuceActivity.this, "请正确填写用户密码", Toast.LENGTH_SHORT).show();
return;
} Intent intent=new Intent(); intent.putExtra("name",user_name ); intent.putExtra("code",user_code); intent.putExtra("pass",pass_word); setResult(RESULT_OK,intent); finish();
} }

ZhuceActivity

package com.example.wang.myapplication;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; public class TestActivity extends AppCompatActivity { Button bt_1;
EditText et_1;
TextView tv_1;
TextView tv_2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test); et_1=(EditText)findViewById(R.id.et_1); tv_1=(TextView)findViewById(R.id.tv_1); tv_2=(TextView)findViewById(R.id.tv_2); //从上一个页面传递信息到本页面
Intent intent=getIntent(); String str1=intent.getStringExtra("code");
String str2=intent.getStringExtra("name"); tv_1.setText("用户代码:"+str1);
tv_2.setText("用户名称:"+str2); bt_1=(Button)findViewById(R.id.bt_1); //间接拨打电话
bt_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { // //方法1
// String str1=et_1.getText().toString();
//
// if (str1.trim().length()==0||str1==null)
// {
// Toast.makeText(TestActivity.this, "请正确填写号码", Toast.LENGTH_SHORT).show();
// return;
// }
//
// Intent intent=new Intent(Intent.ACTION_DIAL);
//
// intent.setData(Uri.parse("tel:"+str1));
//
// startActivity(intent); //方法2 调用getphone()方法 String phone=getphone(); if (phone==null) return; Intent intent=new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:"+phone)); startActivity(intent); }
});
}
//由于方法重复使用,定义一个方法
public String getphone()
{
String phone=et_1.getText().toString(); if (phone.trim().length()==0||phone==null)
{
Toast.makeText(TestActivity.this, "请正确填写号码", Toast.LENGTH_SHORT).show();
return null;
}
else
{
return phone;
} } //直接拨打电话方法
public void zhibo(View v)
{
String phone=getphone(); if (phone==null) return; Intent intent=new Intent(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:" + phone)); try {
startActivity(intent);
}
catch (Exception e)
{
e.printStackTrace();
}
finish(); }
}

TestActivity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.example.wang.myapplication.MainActivity"> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户代码"
android:id="@+id/et_usercode"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:layout_below="@id/et_usercode"
android:id="@+id/et_password"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_password">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="登陆"
android:id="@+id/bt_denglu"
android:onClick="denglu"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="注册"
android:id="@+id/bt_zhuce"
android:onClick="zhuce"
android:layout_weight="1"/>
</LinearLayout> </RelativeLayout>

activity_main

<?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:layout_margin="16dp"
android:orientation="vertical"
> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户代码"
android:id="@+id/usercode"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名称"
android:id="@+id/username"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:id="@+id/password"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="取消"
android:id="@+id/bt_quxiao"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="确定"
android:id="@+id/bt_queding"
android:layout_weight="1"
android:onClick="queding"/>
</LinearLayout> </LinearLayout>

activity_zhuce

<?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.example.wang.myapplication.TestActivity"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/tv_1" /> <TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/tv_2" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="170dp"
android:layout_height="wrap_content"
android:hint="电话号码"
android:inputType="phone"
android:id="@+id/et_1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="间接拨打"
android:id="@+id/bt_1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="直接拨打"
android:id="@+id/bt_2"
android:onClick="zhibo"/> </LinearLayout> </LinearLayout>

activity_test

\

Activity总结练习的更多相关文章

  1. EventBus实现activity跟fragment交互数据

    最近老是听到技术群里面有人提出需求,activity跟fragment交互数据,或者从一个activity跳转到另外一个activity的fragment,所以我给大家介绍一个开源项目,EventBu ...

  2. Android—Service与Activity的交互

    service-Android的四大组件之一.人称"后台服务"指其本身的运行并不依赖于用户可视的UI界面 实际开发中我们经常需要service和activity之间可以相互传递数据 ...

  3. Android:Activity+Fragment及它们之间的数据交换.

    Android:Activity+Fragment及它们之间的数据交换 关于Fragment与Fragment.Activity通信的四种方式 比较好一点的Activity+Fragment及它们之间 ...

  4. Android中Activity处理返回结果的实现方式

    大家在网上购物时都有这样一个体验,在确认订单选择收货人以及地址时,会跳转页面到我们存入网站内的所有收货信息(包含收货地址,收货人)的界面供我们选择,一旦我们点击其中某一条信息,则会自动跳转到订单提交界 ...

  5. 报错:You need to use a Theme.AppCompat theme (or descendant) with this activity.

    学习 Activity 生命周期时希望通过 Dialog 主题测试 onPause() 和 onStop() 的区别,点击按钮跳转 Activity 时报错: E/AndroidRuntime: FA ...

  6. Android 旋转屏幕--处理Activity与AsyncTask的最佳解决方案

    一.概述 运行时变更就是设备在运行时发生变化(例如屏幕旋转.键盘可用性及语言).发生这些变化,Android会重启Activity,这时就需要保存activity的状态及与activity相关的任务, ...

  7. Activity之概览屏幕(Overview Screen)

    概览屏幕 概览屏幕(也称为最新动态屏幕.最近任务列表或最近使用的应用)是一个系统级别 UI,其中列出了最近访问过的 Activity 和任务. 用户可以浏览该列表并选择要恢复的任务,也可以通过滑动清除 ...

  8. Android开发学习—— 创建项目时,不是继承activity,而是继承ActionBarActivity

    对于我们新建android项目时, 会 继承ActionBarActivity. 我们在新建项目时, 最小需求的sdk 选择 4.0以上版本.这样 新建的android项目就是继承activity了!

  9. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

  10. Android中Activity的四大启动模式实验简述

    作为Android四大组件之一,Activity可以说是最基本也是最常见的组件,它提供了一个显示界面,从而实现与用户的交互,作为初学者,必须熟练掌握.今天我们就来通过实验演示,来帮助大家理解Activ ...

随机推荐

  1. Python高手之路【四】python函数装饰器,迭代器

    def outer(func): def inner(): print('hello') print('hello') print('hello') r = func() print('end') p ...

  2. P1776 宝物筛选_NOI导刊2010提高(02)&& 多重背包二进制优化

    多重背包, 要求 \(N\log N\) 复杂度 Solution 众所周和, \(1-N\) 之内的任何数可以由 \(2^{0}, 2^{1}, 2^{2} ... 2^{\log N}, N - ...

  3. JAVA核心技术I---JAVA开发环境配置

    一:常常有看到Java SE,Java EE,Java ME,那么他们的区别呢? 1. Java SE(Java Platform,Standard Edition  java平台标准版). Java ...

  4. 蓝桥杯 方格填数 DFS 全排列 next_permutation用法

    如下的10个格子(参看[图1.jpg]) 填入0~9的数字.要求:连续的两个数字不能相邻.(左右.上下.对角都算相邻) 一共有多少种可能的填数方案? 请填写表示方案数目的整数.注意:你提交的应该是一个 ...

  5. Writing your first academic paper

    Writing your first academic paper If you are working in academics (and you are if you are working wi ...

  6. Spring Cloud(十一)声名式服务调用:Feign的使用 (上)

    一.写在前边 最近开发任务比较忙,下班也开始锻炼了,这个系列的文章就放了很久,看github我提交的Feign的入门程序已经好久了,今天正好得空,这就更上一贴,准备分几部分写 注意:之前几个项目中,笔 ...

  7. ElasticSearch关键概念

    Elasticsearch 添加索引 一个存储关联数据的地方 用来指向一个或者多个分片(shards)的逻辑命名空间(logical namespcase) 应用程序直接与索引通信 一个分片(shar ...

  8. 14、BigInteger类简介

    BigInteger类概述 BigInteger类可以让超过Integer范围的数据进行运算,通常在对数字计算比较大的行业中应用的多一些. package com.sutaoyu.usually_cl ...

  9. 一个diff工具,用于判断两个目录下所有的改动(比较新旧版本文件夹)

    需求: 编写一个diff工具,用于判断两个目录下所有的改动 详细介绍: 有A和B两个目录,目录所在位置及层级均不确定 需要以B为基准找出两个目录中所有有改动的文件(文件或内容增加.修改.删除),将有改 ...

  10. equals方法变量和常量位置区别

    对于字符串比较,我的习惯用法是   变量.equals(常量) 比如:     a.equals("a") 今天看视频才知道变量在前面与后面有很大影响,正确的写法是常量放前面(可以 ...