实验二Activity及UIb.docx
《实验二Activity及UIb.docx》由会员分享,可在线阅读,更多相关《实验二Activity及UIb.docx(15页珍藏版)》请在冰豆网上搜索。
实验二Activity及UIb
天津理工大学
计算机与通讯工程学院
实验报告
2012至2013学年第二学期
实验一AndroidActivity及UI设计
课程名称
软件主流开发平台与工具
学号
学生姓名
年级
专业
教学班号
实验地点
实验时间
主讲教师
辅导教师
实验
(一)
实验名称
Activity及UI
软件环境
Windows
Ecllipse&AndroidSDK
硬件环境
PC
实验目的:
完成Android应用程序建立及界面处理技术
完成Android应用程序建立及界面处理技术,包括以下内容:
●Android应用层程序建立
●界面布局及控件添加
●菜单添加——选项菜单及上下文菜单
●消息处理
●ArrayAdapter使用
实验内容(应包括实验题目、实验要求、实验任务等)
实验要求:
完成“toDoList”功能
参考步骤:
1.建立Android应用程序框架
2.添加控件——EditText负责输入“工作项目”;ListView负责显示“工作列表”
3.定义ArrayList,并通过ArrayAdapter将ListView与ArrayList相关联
4.添加选项菜单,(参见演示软件)
a.当EditText控件Visible时,有两个菜单项,“添加项目”和“取消添加”。
其中添加项目将EditText内容添加到ListView中,“取消项目”将EditText隐藏
b.当EditText控件隐藏时,有一个菜单项,及“添加菜单”项,当按下该菜单时,EditText由隐藏状态改为显示状态
5.为EditText添加按键事件(D-pad键)监听器,当按下该键时,EditText内容添加到“工作列表”中
6.为ListView添加上下文菜单,当长按ListView某一项时,跳出上下文菜单,提示用户可以删除该项目
附:
安装apk:
adbinstall路径/toDoList.apk
实验过程与实验结果(可包括实验实施的步骤、算法描述、流程、结论等)
实现方法:
用Intent实现两个Activity(分别为LoginActivity和usrandpasswd)之间的切换和数据传递。
用onActivityResult()函数实现在父Activity中处理子Activity的返回值。
LoginActivity的布局在main.XML中;usrandpasswd的布局在passwd.XML中。
实验步骤:
首先在AndroidManifest.xml中注册这两个Activity,代码如下:
xmlversion="1.0"encoding="utf-8"?
>
android="package="com.gaofeifei.login"android:versionCode="1"android:versionName="1.0">minSdkVersion="8"/>android:icon="@drawable/ic_launcher"android:label="@string/app_name">android:label="@string/app_name"android:name=".LoginActivity">name="android.intent.action.MAIN"/>name="android.intent.category.LAUNCHER"/>android:label="@string/app_name"android:name=".usrandpasswd">其次在main.XML中对LoginActivity写布局,在passwd.XML中对usrandpasswd。代码如下:xmlversion="1.0"encoding="utf-8"?>android="android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical">android:id="@+id/showResult"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/>android:id="@+id/login"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="登陆"/> xmlversion="1.0"encoding="utf-8"?>android="android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical">android:id="@+id/usrnameTextView"android:layout_width="90dip"android:layout_height="wrap_content"android:textSize="18dip"android:text="用户名:"/>android:id="@+id/usrnameEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/usrnameTextView"android:text=""/>android:id="@+id/passwdTextView"android:layout_width="90dip"android:layout_height="wrap_content"android:layout_below="@id/usrnameEditText"android:textSize="18dip"android:text="密码:"/>android:id="@+id/passwdEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/passwdTextView"android:text=""/>android:id="@+id/passwdButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/passwdEditText"android:layout_marginTop="15dip"android:textSize="18dip"android:text="确认"/>最后,实现主界面上有一个“登录”按钮,单击“登录”按钮后打开一个新的Activity;新的Activity上有“输入用户名”和“密码”控件,输入后单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中classLoginActivity中:finalintACTIVITY1=1;privateTextViewshowResult;showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});该方法为Button按钮设了一个监听器,Intent显示启动usrandpasswdActivity。protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}该方法实现新的Activity上的用户名和密码,在单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中。classusrandpasswd中:privateEditTextusrname;privateEditTextpasswd;usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});此方法是Intent隐式启动。截图如下: 源代码:packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;publicclassLoginActivityextendsActivity{finalintACTIVITY1=1;privateTextViewshowResult;/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});} protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}} packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;import.Uri;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.*;publicclassusrandpasswdextendsActivity{privateEditTextusrname;privateEditTextpasswd;protectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);setContentView(R.layout.passwd);usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});}}实验感受:
android="
package="com.gaofeifei.login"
android:
versionCode="1"
versionName="1.0">
minSdkVersion="8"/>android:icon="@drawable/ic_launcher"android:label="@string/app_name">android:label="@string/app_name"android:name=".LoginActivity">name="android.intent.action.MAIN"/>name="android.intent.category.LAUNCHER"/>android:label="@string/app_name"android:name=".usrandpasswd">其次在main.XML中对LoginActivity写布局,在passwd.XML中对usrandpasswd。代码如下:xmlversion="1.0"encoding="utf-8"?>android="android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical">android:id="@+id/showResult"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/>android:id="@+id/login"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="登陆"/> xmlversion="1.0"encoding="utf-8"?>android="android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical">android:id="@+id/usrnameTextView"android:layout_width="90dip"android:layout_height="wrap_content"android:textSize="18dip"android:text="用户名:"/>android:id="@+id/usrnameEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/usrnameTextView"android:text=""/>android:id="@+id/passwdTextView"android:layout_width="90dip"android:layout_height="wrap_content"android:layout_below="@id/usrnameEditText"android:textSize="18dip"android:text="密码:"/>android:id="@+id/passwdEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/passwdTextView"android:text=""/>android:id="@+id/passwdButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/passwdEditText"android:layout_marginTop="15dip"android:textSize="18dip"android:text="确认"/>最后,实现主界面上有一个“登录”按钮,单击“登录”按钮后打开一个新的Activity;新的Activity上有“输入用户名”和“密码”控件,输入后单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中classLoginActivity中:finalintACTIVITY1=1;privateTextViewshowResult;showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});该方法为Button按钮设了一个监听器,Intent显示启动usrandpasswdActivity。protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}该方法实现新的Activity上的用户名和密码,在单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中。classusrandpasswd中:privateEditTextusrname;privateEditTextpasswd;usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});此方法是Intent隐式启动。截图如下: 源代码:packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;publicclassLoginActivityextendsActivity{finalintACTIVITY1=1;privateTextViewshowResult;/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});} protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}} packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;import.Uri;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.*;publicclassusrandpasswdextendsActivity{privateEditTextusrname;privateEditTextpasswd;protectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);setContentView(R.layout.passwd);usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});}}实验感受:
minSdkVersion="8"/>
android:icon="@drawable/ic_launcher"android:label="@string/app_name">android:label="@string/app_name"android:name=".LoginActivity">name="android.intent.action.MAIN"/>name="android.intent.category.LAUNCHER"/>android:label="@string/app_name"android:name=".usrandpasswd">
icon="@drawable/ic_launcher"
label="@string/app_name">
android:label="@string/app_name"android:name=".LoginActivity">name="android.intent.action.MAIN"/>name="android.intent.category.LAUNCHER"/>
label="@string/app_name"
name=".LoginActivity">
name="android.intent.action.MAIN"/>name="android.intent.category.LAUNCHER"/>
name="android.intent.action.MAIN"/>
name="android.intent.category.LAUNCHER"/>
android:label="@string/app_name"android:name=".usrandpasswd">
name=".usrandpasswd"
其次在main.XML中对LoginActivity写布局,在passwd.XML中对usrandpasswd。
代码如下:
android="android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical">android:id="@+id/showResult"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/>android:id="@+id/login"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="登陆"/> xmlversion="1.0"encoding="utf-8"?>android="android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical">android:id="@+id/usrnameTextView"android:layout_width="90dip"android:layout_height="wrap_content"android:textSize="18dip"android:text="用户名:"/>android:id="@+id/usrnameEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/usrnameTextView"android:text=""/>android:id="@+id/passwdTextView"android:layout_width="90dip"android:layout_height="wrap_content"android:layout_below="@id/usrnameEditText"android:textSize="18dip"android:text="密码:"/>android:id="@+id/passwdEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/passwdTextView"android:text=""/>android:id="@+id/passwdButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/passwdEditText"android:layout_marginTop="15dip"android:textSize="18dip"android:text="确认"/>最后,实现主界面上有一个“登录”按钮,单击“登录”按钮后打开一个新的Activity;新的Activity上有“输入用户名”和“密码”控件,输入后单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中classLoginActivity中:finalintACTIVITY1=1;privateTextViewshowResult;showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});该方法为Button按钮设了一个监听器,Intent显示启动usrandpasswdActivity。protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}该方法实现新的Activity上的用户名和密码,在单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中。classusrandpasswd中:privateEditTextusrname;privateEditTextpasswd;usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});此方法是Intent隐式启动。截图如下: 源代码:packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;publicclassLoginActivityextendsActivity{finalintACTIVITY1=1;privateTextViewshowResult;/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});} protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}} packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;import.Uri;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.*;publicclassusrandpasswdextendsActivity{privateEditTextusrname;privateEditTextpasswd;protectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);setContentView(R.layout.passwd);usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});}}实验感受:
layout_width="fill_parent"
layout_height="fill_parent"
orientation="vertical">
android:id="@+id/showResult"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/>android:id="@+id/login"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="登陆"/> xmlversion="1.0"encoding="utf-8"?>android="android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical">android:id="@+id/usrnameTextView"android:layout_width="90dip"android:layout_height="wrap_content"android:textSize="18dip"android:text="用户名:"/>android:id="@+id/usrnameEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/usrnameTextView"android:text=""/>android:id="@+id/passwdTextView"android:layout_width="90dip"android:layout_height="wrap_content"android:layout_below="@id/usrnameEditText"android:textSize="18dip"android:text="密码:"/>android:id="@+id/passwdEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/passwdTextView"android:text=""/>android:id="@+id/passwdButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/passwdEditText"android:layout_marginTop="15dip"android:textSize="18dip"android:text="确认"/>最后,实现主界面上有一个“登录”按钮,单击“登录”按钮后打开一个新的Activity;新的Activity上有“输入用户名”和“密码”控件,输入后单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中classLoginActivity中:finalintACTIVITY1=1;privateTextViewshowResult;showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});该方法为Button按钮设了一个监听器,Intent显示启动usrandpasswdActivity。protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}该方法实现新的Activity上的用户名和密码,在单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中。classusrandpasswd中:privateEditTextusrname;privateEditTextpasswd;usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});此方法是Intent隐式启动。截图如下: 源代码:packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;publicclassLoginActivityextendsActivity{finalintACTIVITY1=1;privateTextViewshowResult;/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});} protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}} packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;import.Uri;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.*;publicclassusrandpasswdextendsActivity{privateEditTextusrname;privateEditTextpasswd;protectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);setContentView(R.layout.passwd);usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});}}实验感受:
id="@+id/showResult"
layout_height="wrap_content"
text="@string/hello"/>
android:id="@+id/login"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="登陆"/> xmlversion="1.0"encoding="utf-8"?>android="android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical">android:id="@+id/usrnameTextView"android:layout_width="90dip"android:layout_height="wrap_content"android:textSize="18dip"android:text="用户名:"/>android:id="@+id/usrnameEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/usrnameTextView"android:text=""/>android:id="@+id/passwdTextView"android:layout_width="90dip"android:layout_height="wrap_content"android:layout_below="@id/usrnameEditText"android:textSize="18dip"android:text="密码:"/>android:id="@+id/passwdEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/passwdTextView"android:text=""/>android:id="@+id/passwdButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/passwdEditText"android:layout_marginTop="15dip"android:textSize="18dip"android:text="确认"/>最后,实现主界面上有一个“登录”按钮,单击“登录”按钮后打开一个新的Activity;新的Activity上有“输入用户名”和“密码”控件,输入后单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中classLoginActivity中:finalintACTIVITY1=1;privateTextViewshowResult;showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});该方法为Button按钮设了一个监听器,Intent显示启动usrandpasswdActivity。protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}该方法实现新的Activity上的用户名和密码,在单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中。classusrandpasswd中:privateEditTextusrname;privateEditTextpasswd;usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});此方法是Intent隐式启动。截图如下: 源代码:packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;publicclassLoginActivityextendsActivity{finalintACTIVITY1=1;privateTextViewshowResult;/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});} protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}} packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;import.Uri;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.*;publicclassusrandpasswdextendsActivity{privateEditTextusrname;privateEditTextpasswd;protectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);setContentView(R.layout.passwd);usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});}}实验感受:
id="@+id/login"
text="登陆"
/>
android="android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical">android:id="@+id/usrnameTextView"android:layout_width="90dip"android:layout_height="wrap_content"android:textSize="18dip"android:text="用户名:"/>android:id="@+id/usrnameEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/usrnameTextView"android:text=""/>android:id="@+id/passwdTextView"android:layout_width="90dip"android:layout_height="wrap_content"android:layout_below="@id/usrnameEditText"android:textSize="18dip"android:text="密码:"/>android:id="@+id/passwdEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/passwdTextView"android:text=""/>android:id="@+id/passwdButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/passwdEditText"android:layout_marginTop="15dip"android:textSize="18dip"android:text="确认"/>最后,实现主界面上有一个“登录”按钮,单击“登录”按钮后打开一个新的Activity;新的Activity上有“输入用户名”和“密码”控件,输入后单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中classLoginActivity中:finalintACTIVITY1=1;privateTextViewshowResult;showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});该方法为Button按钮设了一个监听器,Intent显示启动usrandpasswdActivity。protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}该方法实现新的Activity上的用户名和密码,在单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中。classusrandpasswd中:privateEditTextusrname;privateEditTextpasswd;usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});此方法是Intent隐式启动。截图如下: 源代码:packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;publicclassLoginActivityextendsActivity{finalintACTIVITY1=1;privateTextViewshowResult;/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});} protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}} packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;import.Uri;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.*;publicclassusrandpasswdextendsActivity{privateEditTextusrname;privateEditTextpasswd;protectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);setContentView(R.layout.passwd);usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});}}实验感受:
layout_width="fill_parent"android:
layout_height="fill_parent"android:
android:id="@+id/usrnameTextView"android:layout_width="90dip"android:layout_height="wrap_content"android:textSize="18dip"android:text="用户名:"/>android:id="@+id/usrnameEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/usrnameTextView"android:text=""/>android:id="@+id/passwdTextView"android:layout_width="90dip"android:layout_height="wrap_content"android:layout_below="@id/usrnameEditText"android:textSize="18dip"android:text="密码:"/>android:id="@+id/passwdEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/passwdTextView"android:text=""/>android:id="@+id/passwdButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/passwdEditText"android:layout_marginTop="15dip"android:textSize="18dip"android:text="确认"/>最后,实现主界面上有一个“登录”按钮,单击“登录”按钮后打开一个新的Activity;新的Activity上有“输入用户名”和“密码”控件,输入后单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中classLoginActivity中:finalintACTIVITY1=1;privateTextViewshowResult;showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});该方法为Button按钮设了一个监听器,Intent显示启动usrandpasswdActivity。protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}该方法实现新的Activity上的用户名和密码,在单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中。classusrandpasswd中:privateEditTextusrname;privateEditTextpasswd;usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});此方法是Intent隐式启动。截图如下: 源代码:packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;publicclassLoginActivityextendsActivity{finalintACTIVITY1=1;privateTextViewshowResult;/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});} protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}} packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;import.Uri;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.*;publicclassusrandpasswdextendsActivity{privateEditTextusrname;privateEditTextpasswd;protectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);setContentView(R.layout.passwd);usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});}}实验感受:
id="@+id/usrnameTextView"
layout_width="90dip"
textSize="18dip"
text="用户名:
"/>
android:id="@+id/usrnameEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/usrnameTextView"android:text=""/>android:id="@+id/passwdTextView"android:layout_width="90dip"android:layout_height="wrap_content"android:layout_below="@id/usrnameEditText"android:textSize="18dip"android:text="密码:"/>android:id="@+id/passwdEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/passwdTextView"android:text=""/>android:id="@+id/passwdButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/passwdEditText"android:layout_marginTop="15dip"android:textSize="18dip"android:text="确认"/>最后,实现主界面上有一个“登录”按钮,单击“登录”按钮后打开一个新的Activity;新的Activity上有“输入用户名”和“密码”控件,输入后单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中classLoginActivity中:finalintACTIVITY1=1;privateTextViewshowResult;showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});该方法为Button按钮设了一个监听器,Intent显示启动usrandpasswdActivity。protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}该方法实现新的Activity上的用户名和密码,在单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中。classusrandpasswd中:privateEditTextusrname;privateEditTextpasswd;usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});此方法是Intent隐式启动。截图如下: 源代码:packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;publicclassLoginActivityextendsActivity{finalintACTIVITY1=1;privateTextViewshowResult;/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});} protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}} packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;import.Uri;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.*;publicclassusrandpasswdextendsActivity{privateEditTextusrname;privateEditTextpasswd;protectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);setContentView(R.layout.passwd);usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});}}实验感受:
id="@+id/usrnameEditText"
layout_width="120dip"
layout_below="@id/usrnameTextView"
text=""/>
android:id="@+id/passwdTextView"android:layout_width="90dip"android:layout_height="wrap_content"android:layout_below="@id/usrnameEditText"android:textSize="18dip"android:text="密码:"/>android:id="@+id/passwdEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/passwdTextView"android:text=""/>android:id="@+id/passwdButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/passwdEditText"android:layout_marginTop="15dip"android:textSize="18dip"android:text="确认"/>最后,实现主界面上有一个“登录”按钮,单击“登录”按钮后打开一个新的Activity;新的Activity上有“输入用户名”和“密码”控件,输入后单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中classLoginActivity中:finalintACTIVITY1=1;privateTextViewshowResult;showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});该方法为Button按钮设了一个监听器,Intent显示启动usrandpasswdActivity。protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}该方法实现新的Activity上的用户名和密码,在单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中。classusrandpasswd中:privateEditTextusrname;privateEditTextpasswd;usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});此方法是Intent隐式启动。截图如下: 源代码:packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;publicclassLoginActivityextendsActivity{finalintACTIVITY1=1;privateTextViewshowResult;/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});} protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}} packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;import.Uri;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.*;publicclassusrandpasswdextendsActivity{privateEditTextusrname;privateEditTextpasswd;protectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);setContentView(R.layout.passwd);usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});}}实验感受:
id="@+id/passwdTextView"
layout_below="@id/usrnameEditText"
text="密码:
android:id="@+id/passwdEditText"android:layout_width="120dip"android:layout_height="wrap_content"android:layout_below="@id/passwdTextView"android:text=""/>android:id="@+id/passwdButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/passwdEditText"android:layout_marginTop="15dip"android:textSize="18dip"android:text="确认"/>最后,实现主界面上有一个“登录”按钮,单击“登录”按钮后打开一个新的Activity;新的Activity上有“输入用户名”和“密码”控件,输入后单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中classLoginActivity中:finalintACTIVITY1=1;privateTextViewshowResult;showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});该方法为Button按钮设了一个监听器,Intent显示启动usrandpasswdActivity。protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}该方法实现新的Activity上的用户名和密码,在单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中。classusrandpasswd中:privateEditTextusrname;privateEditTextpasswd;usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});此方法是Intent隐式启动。截图如下: 源代码:packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;publicclassLoginActivityextendsActivity{finalintACTIVITY1=1;privateTextViewshowResult;/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});} protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}} packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;import.Uri;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.*;publicclassusrandpasswdextendsActivity{privateEditTextusrname;privateEditTextpasswd;protectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);setContentView(R.layout.passwd);usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});}}实验感受:
id="@+id/passwdEditText"
layout_below="@id/passwdTextView"
android:id="@+id/passwdButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/passwdEditText"android:layout_marginTop="15dip"android:textSize="18dip"android:text="确认"/>最后,实现主界面上有一个“登录”按钮,单击“登录”按钮后打开一个新的Activity;新的Activity上有“输入用户名”和“密码”控件,输入后单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中classLoginActivity中:finalintACTIVITY1=1;privateTextViewshowResult;showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});该方法为Button按钮设了一个监听器,Intent显示启动usrandpasswdActivity。protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}该方法实现新的Activity上的用户名和密码,在单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中。classusrandpasswd中:privateEditTextusrname;privateEditTextpasswd;usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});此方法是Intent隐式启动。截图如下: 源代码:packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;publicclassLoginActivityextendsActivity{finalintACTIVITY1=1;privateTextViewshowResult;/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);showResult=(TextView)findViewById(R.id.showResult);Buttonlogin=(Button)findViewById(R.id.login);login.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(LoginActivity.this,usrandpasswd.class);startActivityForResult(intent,ACTIVITY1);}});} protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseACTIVITY1:if(resultCode==RESULT_OK)showResult.setText(data.getData().toString());break;}}} packagecom.gaofeifei.login;importandroid.app.Activity;importandroid.content.Intent;import.Uri;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.*;publicclassusrandpasswdextendsActivity{privateEditTextusrname;privateEditTextpasswd;protectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);setContentView(R.layout.passwd);usrname=(EditText)findViewById(R.id.usrnameEditText);passwd=(EditText)findViewById(R.id.passwdEditText);Buttonbutton=(Button)findViewById(R.id.passwdButton);button.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstubStringmsg="用户名:"+usrname.getText().toString()+"\n"+"密码:"+passwd.getText();Intentresult=newIntent(null,Uri.parse(msg));setResult(RESULT_OK,result);finish();}});}}实验感受:
id="@+id/passwdButton"
layout_width="wrap_content"
layout_below="@id/passwdEditText"
layout_marginTop="15dip"
text="确认"/>
最后,实现主界面上有一个“登录”按钮,单击“登录”按钮后打开一个新的Activity;新的Activity上有“输入用户名”和“密码”控件,输入后单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中
classLoginActivity中:
finalintACTIVITY1=1;
privateTextViewshowResult;
showResult=(TextView)findViewById(R.id.showResult);
Buttonlogin=(Button)findViewById(R.id.login);
login.setOnClickListener(newView.OnClickListener(){
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
Intentintent=newIntent();
intent.setClass(LoginActivity.this,usrandpasswd.class);
startActivityForResult(intent,ACTIVITY1);
}
});
该方法为Button按钮设了一个监听器,Intent显示启动usrandpasswdActivity。
protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
super.onActivityResult(requestCode,resultCode,data);
switch(requestCode)
{
caseACTIVITY1:
if(resultCode==RESULT_OK)
showResult.setText(data.getData().toString());
break;
该方法实现新的Activity上的用户名和密码,在单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中。
classusrandpasswd中:
privateEditTextusrname;
privateEditTextpasswd;
usrname=(EditText)findViewById(R.id.usrnameEditText);
passwd=(EditText)findViewById(R.id.passwdEditText);
Buttonbutton=(Button)findViewById(R.id.passwdButton);
button.setOnClickListener(newView.OnClickListener(){
Stringmsg="用户名:
"+usrname.getText().toString()+"\n"+"密码:
"+passwd.getText();
Intentresult=newIntent(null,Uri.parse(msg));
setResult(RESULT_OK,result);
finish();
此方法是Intent隐式启动。
截图如下:
源代码:
packagecom.gaofeifei.login;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.TextView;
publicclassLoginActivityextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
import.Uri;
importandroid.widget.*;
publicclassusrandpasswdextendsActivity{
protectedvoidonCreate(BundlesavedInstanceState){
setContentView(R.layout.passwd);
实验感受:
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1