位置: 编程技术 - 正文
推荐整理分享Android入门:Intent入门(android入门基础知识),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:android入门下载,android入门下载,android入门项目,android入门项目,android入门小项目,android入门项目,android入门书籍推荐,android入门书籍推荐,内容如对您有帮助,希望把文章链接给更多的朋友!
0.创建新的Activity步骤
在应用中创建新的Acitivity很常见;
步骤1:创建一个class继承Activity
步骤2:在Android-manifest.xml中添加<activity>元素
比如创建了一个名为SubActivity的Activity,则需要声明如下:
<activity android:name=".SubActivity"></activity>
1.最简单的跳转到新的Activity程序描述:MainActivity点击按钮后跳转到SubActivity,MainActivity传递一个(name,xiazdong)给SubActivity,并获得显示;
效果如下:
点击按钮后:
IntentActivity.javapackage org.xiazdong;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class IntentActivity extends Activity {private Button btn1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn1 = (Button)this.findViewById(R.id.brn1); btn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setClass(IntentActivity.this, SubActivity.class); //从IntentActivity跳转到SubActivityintent.putExtra("name", "xiazdong"); //放入数据startActivity(intent); //开始跳转}}); }}
SubActivity.java
package org.xiazdong;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class SubActivity extends Activity{private TextView tv1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.setContentView(R.layout.sub);tv1 = (TextView)this.findViewById(R.id.tv1);Intent intent = this.getIntent(); //获得当前的Intent Bundle bundle = intent.getExtras(); //获得全部数据String value = bundle.getString("name"); //获得名为name的tv1.setText(value);}}
2.带返回的跳转
跳转到新的Activity并在结束后将返回传给原Activity
程序描述:MainActivity跳转到SubActivity后,SubActivity执行完后返回一个结果码给IntentActivity,执行对应的过程;
程序效果:
点击跳转后执行完第二个Activity后再跳回第一个Activity
IntentActivity.javapackage org.xiazdong;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class IntentActivity extends Activity {private Button btn1;private TextView tv1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn1 = (Button)this.findViewById(R.id.brn1); tv1 = (TextView)this.findViewById(R.id.tv2); btn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setClass(IntentActivity.this, SubActivity.class);startActivityForResult(intent, ); //requestcode=}}); }@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if(requestCode==&&resultCode==){Bundle bundle = data.getExtras();String response = bundle.getString("response");tv1.setText(response);}} }
SubActivity.java
package org.xiazdong;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class SubActivity extends Activity{private TextView tv1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.setContentView(R.layout.sub);tv1 = (TextView)this.findViewById(R.id.tv1);Intent intent = new Intent(); //创建一个Intentintent.putExtra("response", "来自2");setResult(,intent); //返回码为finish();}}
Android取消标题栏&强制设置横(竖)屏方法 一、Android取消标题栏方法1.效果演示2.实现方法(1)将自定义Activity继承于Activity而不是ActionActivity;(2)在Activity的onCreate方法中的setContentView(myview)调用之前
[安卓]手机管家(七)防盗之左右划屏 下面来实现通过左右滑动来切换页面第一步是切换画面,通常来说,在滑动过程中,会有一部分页面进入当前的activity比如1向左平移出去,2从右边平移
[安卓]手机管家(八)防盗之业务逻辑 一.绑定SIM卡获取SIM卡信息并保存起来找到控件、处理click事件、选中则绑定layout里改一下id,具体点,是绑定simcom.rjl.mobilephonemanager.ui.SettingItemandroid:id=@+
标签: android入门基础知识
本文链接地址:https://www.jiuchutong.com/biancheng/383213.html 转载请保留说明!友情链接: 武汉网站建设