好贷网好贷款

通过计算结果从当前Activity跳转到下一个Activity

发布时间:2016-12-3 19:37:32 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"通过计算结果从当前Activity跳转到下一个Activity",主要涉及到通过计算结果从当前Activity跳转到下一个Activity方面的内容,对于通过计算结果从当前Activity跳转到下一个Activity感兴趣的同学可以参考一下。

package com.example.newactivityapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.TextView; import android.widget.Button; public class NewActivity extends Activity { private TextView symbol; private EditText factorone; private EditText factortwo; private Button calcuate; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置布局文件 setContentView(R.layout.new_activity); //设置文本id symbol = (TextView)findViewById(R.id.symbol); //设置编辑框id factorone = (EditText)findViewById(R.id.factorone); factortwo = (EditText)findViewById(R.id.factortwo); //设置按钮id calcuate = (Button)findViewById(R.id.calcuate); //设置文本框和按钮文字 symbol.setText(R.string.symbol); calcuate.setText(R.string.calcuate); //设置监听器 calcuate.setOnClickListener(new OnClickListenerlmpl()); } //复写菜单函数 public boolean onCreateOptionsMenu(Menu menu) { //调用menu的add方法,第一参数为菜单组名,第二参数为当前菜单id,第三为排序,第四为菜单按钮上显示的文字 menu.add(0,1,1,R.string.exit); menu.add(0,2,2,R.string.about); return super.onCreateOptionsMenu(menu); } //指定菜单按钮动作,item.getItemId() == 1为exit按钮,若为exit按钮退出当前界面 public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == 1) finish(); return super.onOptionsItemSelected(item); } private class OnClickListenerlmpl implements OnClickListener { @Override public void onClick(View v) { //取出编辑框文字转为字符串给相应的变量 String factoronestr = factorone.getText().toString(); String factortwostr = factortwo.getText().toString(); Intent intent = new Intent(); //通过intent对象传入值,第一个参数为值的标记,第二个参数为要传入的值 intent.putExtra("one", factoronestr); intent.putExtra("two", factortwostr); //通过intent的setclass方法设置要跳转的对象,第一个参数为从当前activity开始跳转,第二参数为要跳转到哪个activity intent.setClass(NewActivity.this, ResultActivity.class); //从当前类开始跳转 NewActivity.this.startActivity(intent); } } } package com.example.newactivityapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; public class ResultActivity extends Activity { private TextView resultview; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.result_activity); resultview = (TextView)findViewById(R.id.result); //取得要传入的intent对象 Intent intent = getIntent(); //根据标记"one"取得相应的值 String factoronestr = intent.getStringExtra("one"); String factortwostr = intent.getStringExtra("two"); //将取得的字符串转为整型 int factoroneInt = Integer.parseInt(factoronestr); int factortwoInt = Integer.parseInt(factortwostr); int result = factoroneInt * factortwoInt; //设置积的文本,整型加上字符串后自动转为字符串 resultview.setText(result + ""); } } ResultActivity在mainfest文件中注册 <activity android:name=".ResultActivity"/> <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.newactivityapp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.newactivityapp.NewActivity" 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=".ResultActivity"/> </application> </manifest> 全部代码 package com.example.newactivityapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.TextView; import android.widget.Button; public class NewActivity extends Activity { private TextView symbol; private EditText factorone; private EditText factortwo; private Button calcuate; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.new_activity); symbol = (TextView)findViewById(R.id.symbol); factorone = (EditText)findViewById(R.id.factorone); factortwo = (EditText)findViewById(R.id.factortwo); calcuate = (Button)findViewById(R.id.calcuate); symbol.setText(R.string.symbol); calcuate.setText(R.string.calcuate); calcuate.setOnClickListener(new OnClickListenerlmpl()); } public boolean onCreateOptionsMenu(Menu menu) { menu.add(0,1,1,R.string.exit); menu.add(0,2,2,R.string.about); return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == 1) finish(); return super.onOptionsItemSelected(item); } private class OnClickListenerlmpl implements OnClickListener { @Override public void onClick(View v) { String factoronestr = factorone.getText().toString(); String factortwostr = factortwo.getText().toString(); Intent intent = new Intent(); intent.putExtra("one", factoronestr); intent.putExtra("two", factortwostr); intent.setClass(NewActivity.this, ResultActivity.class); NewActivity.this.startActivity(intent); } } } package com.example.newactivityapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; public class ResultActivity extends Activity { private TextView resultview; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.result_activity); resultview = (TextView)findViewById(R.id.result); Intent intent = getIntent(); String factoronestr = intent.getStringExtra("one"); String factortwostr = intent.getStringExtra("two"); int factoroneInt = Integer.parseInt(factoronestr); int factortwoInt = Integer.parseInt(factortwostr); int result = factoroneInt * factortwoInt; resultview.setText(result + ""); } } <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.newactivityapp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.newactivityapp.NewActivity" 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=".ResultActivity"/> </application> </manifest>      

上一篇:BCMwifi驱动学习
下一篇:Python正则式的基本用法

相关文章

相关评论