Android中多界面跳转的一个简单应用
来源:互联网 发布:java ftp断点续传 下载 编辑:程序博客网 时间:2024/05/17 06:44
多界面跳转的步骤:
一,在layout布局中编辑好布局
二,在src文件夹下写逻辑
三,设置权限
四,在清单文件下注册新建的Activity
1,<activity android:name="com.example.jump.homeActivity" ></activity>
2,<activity android:name="com.example.jump.phoneActivity"></activity>
3,<activity android:name="com.example.jump.sdCardActivity"></activity>
4.<activity android:name="com.example.jump.smsActivity"></activity>
下面模拟了一个多界面跳转的实例,从启动页跳转到第二个界面,然后分别跳转到发短信的界面,打电话的界面,获取手机内存的界面。
布局和代码如下:
MainActivity布局中:
<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"
tools:context=".MainActivity" >
<ImageView
android:src="@drawable/qidong2"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_world"/>
<Button
android:id="@+id/bt_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
第二个界面布局中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#44f0f0">
<Button
android:id="@+id/bt_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/send"
android:layout_marginTop="30dp"
android:onClick="open01"
/>
<Button
android:id="@+id/bt_call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/call"
android:layout_marginTop="30dp"
android:onClick="open02"
/>
<Button
android:id="@+id/bt_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/get"
android:layout_marginTop="30dp"
android:onClick="open03"
/>
</LinearLayout>
发短信界面中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4400ff00"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入收件人的电话号码"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone"
android:id="@+id/et_phone"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入信息的内容"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="5"
android:inputType="textMultiLine"
android:id="@+id/et_body"/>
<Button
android:id="@+id/bt_sms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"/>
</LinearLayout>
打电话界面布局中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#440000ff"
android:orientation="vertical" >
<EditText
android:id="@+id/et_ph"
android:hint="请输入要拨打的电话号码"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone"/>
<Button
android:id="@+id/bt_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="拨打"/>
</LinearLayout>
存储空间布局中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="#4400ff00">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cunchu" />
</LinearLayout>
代码逻辑如下:
MainActivity中:
public class MainActivityextends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.bt_start);
start.setOnClickListener(new OnClickListener(){public void onClick(View v){
Intent intent = new Intent(MainActivity.this,homeActivity.class);
startActivity(intent);
}
});
}
第二个界面中:
public class homeActivityextends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
}
public void open01(View v){
//注意上下文为该类.即第一个参数
Intent intent = new Intent(homeActivity.this,smsActivity.class);
startActivity(intent);
}
public void open02(View v){
Intent intent = new Intent(homeActivity.this,phoneActivity.class);
startActivity(intent);
}
public void open03(View v){
Intent intent=new Intent(homeActivity.this,sdCardActivity.class);
startActivity(intent);
}
}
发短信界面中:
public class smsActivityextends Activity {
private EditTextet_phone;
private EditTextet_body;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
et_phone = (EditText) findViewById(R.id.et_phone);
et_body = (EditText) findViewById(R.id.et_body);
Button bt_sms = (Button) findViewById(R.id.bt_sms);
bt_sms.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
String phone=et_phone.getText().toString().trim();
String body=et_body.getText().toString().trim();
if(TextUtils.isEmpty(phone)||TextUtils.isEmpty(body)){
//注意在这里传参找对正确的界面
Toast.makeText(smsActivity.this,"电话号码或短信内容不能为空", 0).show();
}else{
//else可以省略,但是下面执行的语句只能是一句
SmsManager sms =SmsManager.getDefault();
sms.sendTextMessage(phone,null, body,null,null);
}
}
});
}
}
打电话界面中:
public class phoneActivityextends Activity {
private EditTextet_ph;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
et_ph = (EditText) findViewById(R.id.et_ph);
Button call=(Button) findViewById(R.id.bt_call);
call.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String phone=et_ph.getText().toString().trim();
if("".equals(phone)){
//注意传第一个参数的对象,容易出错
Toast.makeText(phoneActivity.this,"电话号码不能为空", 0).show();
}else{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel://"+phone));
startActivity(intent);
}
}
});
}
}
获取内存界面中:
public class sdCardActivityextends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forth);
File dataFile = Environment.getDataDirectory();
File sdCard = Environment.getExternalStorageDirectory();
long dataSpace = dataFile.getTotalSpace();
long sdcardSpace = sdCard.getTotalSpace();
TextView tv=(TextView) findViewById(R.id.tv);
tv.setText("内部存储:"+Formatter.formatFileSize(this, dataSpace)+"\n"+"外部sd卡:"+Formatter.formatFileSize(this, sdcardSpace));
}
}
注意事项:
* 一,不要忘记添加权限
* 二,不要忘记在清单文件中注册Activity
* 三,Toast时传第一个参数时要注意对象
* 四,分清跳转页与初始页的顺序
- Android中多界面跳转的一个简单应用
- Android 跳转到一个应用安装的详情界面的方法
- Android 跳转到一个应用安装的详情界面的方法
- 界面的简单跳转
- android fragment新手简单应用(实现界面之间的跳转)
- 创建一个简单的javaweb工程实现界面跳转
- android中利用代理模式简单实现界面之间的跳转与传值
- Android 界面跳转问题:开发实现连续跳转几个界面后在最后一个界面完美跳回最初的界面
- Android跳转到应用安装界面的方法
- Android实现跳转到外部应用的某个Activity界面
- android 简单地设置Activity界面的跳转动画
- Android 应用界面简单的输入法代码
- Android 应用载入界面的简单实现
- Android:一个简单查询界面的实现
- Android写一个简单的欢迎界面
- Android 一个简单的登录界面
- 一个简单的Android引导界面
- android画一个简单的界面
- c语言_指针引用数组元素并打印输出
- Java内存区域---深入理解Java虚拟机
- Eclipse插件安装4种方法
- POJ-----1328
- 由两个栈组成的队列
- Android中多界面跳转的一个简单应用
- 交流对一个人成长的重要性
- hdoj 2818 Building Block(加权并查集)
- 二叉树的后序序列
- php学习--5数组
- Java开发中的23种设计模式详解(转)
- POJ2528 Mayor's posters(线段树+离散化)
- POJ1492——Paths on a Grid(组合数公式)
- 支配集,覆盖集,独立集与匹配