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时传第一个参数时要注意对象

 * 四,分清跳转页与初始页的顺序

 

 

 

1 0
原创粉丝点击