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. Visual Studio 2013打开项目出现“未安装项目的目标框架”提示

    问题描述: windows 10 系统里用Visual Studio 2013打开项目,提示如下: 说项目的.NET Framework version=v4.5,系统里没装,让将项目的框架从v4.5 ...

  2. 【Asp.net入门09】第一个ASP.NET 应用程序-处理窗体(1)

    我们创建了一个HTML窗体,可以通过它显示受邀参加晚会的嘉宾,但是,当嘉宾单击Submit RSVP按钮时,同一个页面会反复多次显示.为了解决此问题,需要实现一段代码,用于在将窗体数据发布到服务器时执 ...

  3. Nginx反向代理websocket配置实例

    最近有一个需求,就是需要使用 nginx 反向代理 websocket,经过查找一番资料,目前已经测试通过,本文只做一个记录 复制代码 代码如下: 注: 看官方文档说 Nginx 在 1.3 以后的版 ...

  4. [Spark]-Spark发展历程与基本概念

    Hadoop十年 找了一张Hadoop十年的生态发展图: Spark概况: Apache Spark是一个开源簇运算框架,最初是由加州大学柏克莱分校AMPLab所开发.相对于Hadoop的MapRed ...

  5. solr分组排序实现group by功能

    http://wiki.apache.org/solr/FieldCollapsing solr分组排序,实现group by功能,代码待添加!

  6. MAC下 Apache服务器配置

    今天做了一个注册登录提交的页面,后续操作需要用到后端的知识 php+Mysql,之前只是有些了解,现在开始具体操作了,首先从配置环境开始.查了好几篇文档与博客,了解了挺多知识. Mac下Apache服 ...

  7. appium 使用过程问题踩坑-笔记

    问题1:虚拟设备选用问题 运行脚本抛出异常,创建session对象失败 排查过程:在进入cmd模式下: ①adb devices   --ok ②appium-doctor  --ok ③appium ...

  8. 11 Facts about Data Science that you must know

    11 Facts about Data Science that you must know Statistics, Machine Learning, Data Science, or Analyt ...

  9. spring-boot Test for Controller

    spring-boot  controller 测试示例: 单元测试类 package com.zzhi; import com.fasterxml.jackson.databind.ObjectMa ...

  10. 【Linux】SecureCRT连接Linux乱码

    SecureCRT连接linux出现乱码问题.解决方法. 打开SecureCRT-->option-->Session option