安卓:Intent实现页面跳转传输数据
1、登录界面
<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" >
<TextView
android:id="@+id/top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="68dp"
android:layout_marginLeft="128dp"
android:textStyle="bold|italic"
android:text="欢迎XXX使用此系统"
/>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#DE5045"
android:layout_marginTop="328dp"
android:layout_marginLeft="128dp"
android:textStyle="bold|italic"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textColor="#ff00ff"
android:layout_marginTop="128dp"
android:layout_marginLeft="128dp"
/>
<EditText
android:id="@+id/et"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:layout_gravity="right"
android:textColorHint="#ff00ff"
android:selectAllOnFocus="true"
android:inputType="text"
android:layout_marginTop="120dp"
android:layout_marginLeft="178dp"
/>
<TextView
android:id="@+id/tv"
android:text="密码:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff00ff"
android:layout_marginTop="170dp"
android:layout_marginLeft="128dp"
/>
<EditText
android:id="@+id/et1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:layout_gravity="right"
android:textColorHint="#ff00ff"
android:selectAllOnFocus="true"
android:inputType="textPassword"
android:layout_marginTop="160dp"
android:layout_marginLeft="178dp"
/>
<Button
android:id="@+id/bt_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:layout_marginTop="223dp"
android:layout_marginLeft="218dp"
/>
<Button
android:id="@+id/bt_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="223dp"
android:layout_marginLeft="138dp"
android:text="cancel" />
<ImageView
android:id="@+id/image"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="428dp"
android:layout_marginLeft="128dp"
/>
</RelativeLayout>测试:

2、登录界面的Activity:
public class MainActivity extends Activity implements OnClickListener {
private Button mButtonOK, mButtonCancel;
private TextView mTextView;
private EditText mEditText, myEditText;
private TextView name, username,top;
private ImageView image=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// 调用父类方法,完成系统工作
super.onCreate(savedInstanceState);
// 加载界面布局文件
setContentView(R.layout.activity_main);
// 获取界面控件
getViewItem();
// 设置事件监听
setLiistenets();
}
/**
* 获取界面控件
*/
private void getViewItem() {
name = (TextView) findViewById(R.id.name);
top = (TextView) findViewById(R.id.top);
// 根据控件id获取界面上的Button控件
mButtonOK = (Button) findViewById(R.id.bt_ok);
// 根据控件id获取界面上的Button控件
mButtonCancel = (Button) findViewById(R.id.bt_clear);
// 根据控件id获取界面上的TextView控件
mTextView = (TextView) findViewById(R.id.tv);
// 根据控件id获取界面上的EditText控件
mEditText = (EditText) findViewById(R.id.et);
myEditText = (EditText) findViewById(R.id.et1);
}
/**
* 设置事件监听
*/
private void setLiistenets() {
// 给按钮设置点击事件
mButtonOK.setOnClickListener(this);
// 给按钮设置点击事件
mButtonCancel.setOnClickListener(this);
}
/**
* 复写监听器对象的onClick方法,完成点击后的事件处理,参数为被点击的按钮
*/
@Override
public void onClick(View v) {
// 根据按钮控件的id区分不同按钮的点击
switch (v.getId()) {
case R.id.bt_clear:
// 清空界面控件EditText的文本输入内容
mEditText.setText("");
myEditText.setText("");
break;
case R.id.bt_ok:
EditText et = (EditText)findViewById(R.id.et);
EditText et1 = (EditText)findViewById(R.id.et1);
image=(ImageView)findViewById(R.id.image);
String s1 = et.getText().toString();
String s2 = et1.getText().toString();
if(s1.equals("翟华兵") & s2.equals("654321")) {
name.setText("登录成功");
image.setImageResource(R.drawable.i1);
Intent intent=new Intent(getApplicationContext(),SecondActivity.class);
intent.putExtra("username", s1);
startActivity(intent);
}else {
et.setText("");
et1.setText("");
image.setImageResource(1);
name.setText("用户名或密码有误,请重新输入!!");
}
break;
}
}
class MyListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
}在Activity中对提交和清除按钮设置事件,其中,提交按钮点击后要对输入的用户名和密码进行字符串比较,如果相等则登录成功并将用户名放入到intern对象中。
3、设置要跳转的页面
<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" >
<TextView
android:id="@+id/view_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="68dp"
android:layout_marginLeft="128dp"
android:textStyle="bold|italic"
android:text=""
/>
</RelativeLayout>此页面需要从另外一个Activity中获取数据
4、设置跳转到的页面的Activity:
public class SecondActivity extends Activity {
private TextView view_user=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
view_user=(TextView) findViewById(R.id.view_user);
Intent it=getIntent();
String username=it.getStringExtra("username");
view_user.setText(username+"欢迎你");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}根据键,获取值,并将获取到的值赋给要跳转到的页面

5、总结
在layout中要有两个配置文件,同时也要有两个Activity,分别对应登录界面和要跳转的页面,在添加第二个配置文件后要对其进行注册(在AndroidManifest.xml中):
项目结构:

注册第二个配置文件:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.edittext.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.edittext.SecondActivity">
</activity>
</application> 相关推荐
xfcyhades 2020-11-20
Michael 2020-11-03
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28