intent的一个应用--activity的跳转!
其实Intent的应用很广,今天我把前两天学的Intent的一个应用共享一下,很容易。
基本步骤就是
Intent intent = new Intent();//新建一个activity
intent.setClass(TestActivity.this, login.class);//从本类的activity跳转到目标activity。
startActivity(intent);//执行目标activity
TestActivity.this.finish();//退出第一个activity
注:还要在androidMainfest.xml注册一下目标的activity
还要在
src/空间里新建一个目标的类
res/layout/目录下新建一个对应的布局文件xml。
具体源码:
TestActivity.JAVA
package vc.com;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
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 TestActivity extends Activity {
/** Called when the activity is first created. */
Button bt;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt=(Button)findViewById(R.id.bt1);
final EditText et1=(EditText)findViewById(R.id.un);
final EditText et2=(EditText)findViewById(R.id.pwd);
final Builder al=new AlertDialog.Builder(this);
bt.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
if(et1.getText().length()==0 || et1.getText()==null)
{ al.setTitle("错误信息");
al.setMessage("提交内容不能为空!");
al.setCancelable(true);
al.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
al.setNeutralButton("取消",null );
al.create().show();
}else{
Toast.makeText(TestActivity.this, et1.getText().toString().trim(),Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClass(TestActivity.this, login.class);
startActivity(intent);
}
}
});
}
}
//对应的xml布局
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/bg" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户:"
android:id="@+id/t1"
android:layout_x="50px"
android:layout_y="69px"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:id="@+id/t2"
android:layout_x="50px"
android:layout_y="150px"
/>
<EditText
android:layout_width="150dip"
android:layout_height="wrap_content"
android:id="@+id/un"
android:layout_x="114px"
android:layout_y="57px"
/>
<EditText
android:layout_width="150dip"
android:layout_height="wrap_content"
android:id="@+id/pwd"
android:layout_x="114px"
android:layout_y="142px"
/>
<Button
android:layout_x="90px"
android:layout_y="230px"
android:layout_height="wrap_content"
android:layout_width="140px"
android:text="登陆"
android:id="@+id/bt1"
/>
<LinearLayout
android:layout_y="400px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="网管信息"
android:linksClickable="true"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="关于我们"
android:linksClickable="true"
/>
</LinearLayout>
</AbsoluteLayout>
//login.java
package vc.com;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class login extends Activity {
/**
* @param args
*/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
Button b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
/* newIntentclass */
Intent intent = new Intent();
intent.setClass(login.this, TestActivity.class);
/* Activity */
startActivity(intent);
/* Activity */
login.this.finish();
}
});
}
}
//对应的布局文件
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/ic_launcher"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/button2"
android:layout_width="118px"
android:layout_height="wrap_content"
android:layout_x="100px"
android:layout_y="82px"
android:text="Go to Activity1"
>
</Button>
</AbsoluteLayout>
//androidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="vc.com"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="3" />
<application
android:icon="@drawable/earth"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".TestActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".login">
</activity>
</application>
</manifest>
界面: