以登录为例,假设有一个界面为登录界面,另一个为登录成功后跳转的页面
----------登陆界面---------------- -------------登陆成功后跳转的页面--------------
用户名的输入框(editText组件) 欢迎你,XXX
用户密码的输入框(editText组件)
登录按钮(button组件)
------------------------------------ -------------------------------------------------------
页面的布局代码如下:
----------登陆界面----------------
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/logintitle"
android:textSize="24sp"
/>
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:hint="@string/nametip" 请输入用户名
/>
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:hint="@string/pwdtip" 请输入密码
android:password="true"
/>
android:id="@+id/loginbutton"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="@string/loginbutton"
/>
-------------登陆成功后跳转的页面--------------
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/result"
android:textSize="24sp"
/>
android:id="@+id/loginbutton"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="@string/jumpbutton" 跳转
/>
登录界面对应的Activity
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AppActivity extends Activity implements OnClickListener {
private EditText nameText;
private EditText pwdText;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nameText = (EditText) findViewById(R.id.username);
pwdText = (EditText) findViewById(R.id.password); 获取组件
button = (Button) findViewById(R.id.loginbutton);
//点击按钮,响应button的OnClick事件3种方案,最常用的是这种:让Activity实现OnClicListener接口
button.setOnClickListener(this); 因为该类本身就实现了OnClickListener接口
}
public void onClick(View v) {
String name = nameText.getText().toString(); 根据获取的组件获得用户输入的值
String pwd = pwdText.getText().toString();
if("admin".equals(name)&&"123".equals(pwd)){
//这里边有两大类问题:①传数据的载体--intent有abc三种写法,常用的是前两种(好比request)
Intent intent = new Intent();
intent.setClass(this, SucActivity.class);//a 显式意图
//b//intent.setComponent(new ComponentName(this,SucActivity.class));//说明了想往Suc.activity跳
//c//Intent intent = new Intent(this,SucActivity.class);
//②封装数据的箱子(好比ModelAndView)---有Bundle
//a intent直接装常规的数据类型
/*intent.putExtra("name", name);
intent.putExtra("pwd", pwd);*/
//b 先用bundle将数据封装成键值对的形式,在用intent装载
/*Bundle bundle = new Bundle();
bundle.putString("name", name);
bundle.putString("pwd",pwd);
intent.putExtras(bundle);*/
//c 用intent装载实现可序列化接口的类
User user = new User();
user.setName(name);
user.setPwd(pwd);
intent.putExtra("user", user);
}else{
//error
//在Android中的最简单的弹出框写法
Toast toast = Toast.makeText(this,R.string.loginerror, Toast.LENGTH_LONG);
toast.show();
//Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
}
登陆成功后跳转的页面
对应的Activity
public class SucActivity extends Activity implements OnClickListener{
private TextView textView;
private Button button ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//绑定布局文件
setContentView(R.layout.suc);
textView = (TextView) findViewById(R.id.result);
button = (Button) findViewById(R.id.examplebutton);
button.setOnClickListener(this);
//获取AppActivity中传来的值咋放咋取Intent intent = getIntent();如果采用的是intent直接装常规的数据类型//Bundle b = intent.getExtras();先用bundle将数据封装成键值对的形式,在用intent装载intent.putExtras(bundle);User user = (User) intent.getSerializableExtra("user");用intent装载实现可序列化接口的类
String name = user.getName();//b.getString("name");//intent.getStringExtra("name");
String pwd = user.getPwd();//b.getString("pwd");//intent.getStringExtra("pwd");
textView.setText("欢迎你," + name);
其实到这里,任务已经完成,但需要知道,Activity的切换不仅可以发生在同一个应用程序中,还可以发生在不同的应用程序之间
@Override
public void onClick(View v) {
//隐式意图//Uri uri = Uri.parse("tel://111000");Uri uri = Uri.parse("smsto:111000");Intent intent = new Intent(Intent.ACTION_SENDTO,uri);intent.putExtra("sms_body", "Hello,Android!This is a sms!");startActivity(intent);}
}