<p>MainActivity:点击一个Button会跳转到另一个NextActivity</p><pre class="brush:java;toolbar:false">packagecom.example; importjava.util.ArrayList; importandroid.os.Bundle; importandroid.app.Activity; importandroid.content.Intent; importandroid.view.Menu; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; publicclassMainActivityextendsActivity{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Buttonbutton=(Button)findViewById(R.id.but1); button.setOnClickListener(newOnClickListener(){ @Override publicvoidonClick(Viewv){ //TODOAuto-generatedmethodstub Intentintent=newIntent(MainActivity.this, NextActivity.class); /* *第二种 *Intentintent2=newIntent(); *intent2.setClass(MainActivity.this,NextActivity.class); *第三种: *Intentintent3=newIntent(&quot;com.example.NextActivity&quot;); *注意:此方法一定要在Manifest清单文件中配置intent-filter标签中配置action属性 * *等价于: *Intentintent3=newIntent(&quot;com.example.NextActivity&quot;); *intent3.setAction(com.example.NextActivity&quot;); * *最后不要忘记有个:startActivity(intent)方法哦! */ intent.putExtra(&quot;name&quot;,&quot;zhangsan&quot;); intent.putExtra(&quot;age&quot;,20); ArrayList&lt;String&gt;arrayList=newArrayList&lt;String&gt;(); arrayList.add(&quot;1&quot;); arrayList.add(&quot;2&quot;); arrayList.add(&quot;4&quot;); intent.putStringArrayListExtra(&quot;unknow&quot;,arrayList); startActivity(intent); } }); } @Override publicbooleanonCreateOptionsMenu(Menumenu){ //Inflatethemenu;thisaddsitemstotheactionbarifitispresent. getMenuInflater().inflate(R.menu.main,menu); returntrue; } }</pre><p>AndroidManifest文件:</p><pre class="brush:xml;toolbar:false">&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;utf-8&quot;?&gt; &lt;manifestxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; package=&quot;com.example&quot; android:versionCode=&quot;1&quot; android:versionName=&quot;1.0&quot;&gt; &lt;uses-sdk android:minSdkVersion=&quot;8&quot; android:targetSdkVersion=&quot;17&quot;/&gt; &lt;application android:allowBackup=&quot;true&quot; android:icon=&quot;@drawable/ic_launcher&quot; android:label=&quot;@string/app_name&quot; android:theme=&quot;@style/AppTheme&quot;&gt; &lt;activity android:name=&quot;com.example.MainActivity&quot; android:label=&quot;@string/app_name&quot;&gt; &lt;intent-filter&gt; &lt;actionandroid:name=&quot;android.intent.action.MAIN&quot;/&gt; &lt;categoryandroid:name=&quot;android.intent.category.LAUNCHER&quot;/&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activityandroid:name=&quot;.NextActivity&quot;&gt; &lt;intent-filter&gt; &lt;actionandroid:name=&quot;com.example.NextActivity&quot;/&gt; &lt;categoryandroid:name=&quot;android.intent.category.DEFAULT&quot;/&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt;</pre><p>在NextActivity中得到由MainActivity传过来的数据:</p><pre class="brush:bash;toolbar:false">packagecom.example; importjava.util.ArrayList; importandroid.app.Activity; importandroid.content.Intent; importandroid.os.Bundle; importandroid.util.Log; publicclassNextActivityextendsActivity{ privatefinalStringTAG=&quot;NextActivity&quot;; @Override protectedvoidonCreate(BundlesavedInstanceState){ //TODOAuto-generatedmethodstub super.onCreate(savedInstanceState); setContentView(R.layout.activity_next); Intentintent=getIntent(); Stringname=intent.getStringExtra(&quot;name&quot;); Log.i(TAG,&quot;--name--&gt;&gt;&quot;+name); intage=intent.getIntExtra(&quot;age&quot;,0); Log.i(TAG,&quot;--age--&gt;&gt;&quot;+age); ArrayList&lt;String&gt;arrayList=intent.getStringArrayListExtra(&quot;unknow&quot;); for(Strings:arrayList){ Log.i(TAG,s); } } }</pre>
RangeTime:0.009568s
RangeMem:207.6 KB
返回顶部 留言