计算器和Menu
MainActivity.java
1 import android.app.Activity; 2 import android.content.Intent; 3 import android.os.Bundle; 4 import android.view.Menu; 5 import android.view.MenuItem; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 import android.widget.EditText; 10 11 public class MainActivity extends Activity { 12 private EditText EditTextOne; 13 private EditText EditTextTwo; 14 private Button myButton; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.main); 20 EditTextOne = (EditText) findViewById(R.id.EditTextOne); 21 EditTextTwo = (EditText) findViewById(R.id.EditTextTwo); 22 myButton = (Button) findViewById(R.id.myButton); 23 myButton.setOnClickListener(new myButtonListener()); 24 } 25 26 class myButtonListener implements OnClickListener{ 27 public void onClick(View v) { 28 Intent intent = new Intent(); 29 String first = EditTextOne.getText().toString(); 30 String second = EditTextTwo.getText().toString(); 31 32 intent.putExtra("one",first ); 33 intent.putExtra("two", second); 34 35 intent.setClass(MainActivity.this, ResultActivity.class); 36 startActivity(intent); 37 } 38 39 } 40 //当客户点击MENU按钮的时候,调用该方法 41 public boolean onCreateOptionsMenu(Menu menu) { 42 //一个add对应就产生一个选项 43 menu.add(0, 1, 1, R.string.exit); 44 menu.add(0, 2, 2, R.string.about); 45 return super.onCreateOptionsMenu(menu); 46 } 47 //当客户点击菜单当中的某一个选项时,会调用该方法 48 public boolean onOptionsItemSelected(MenuItem item) { 49 if(item.getItemId() == 1){ 50 finish(); 51 } 52 return super.onOptionsItemSelected(item); 53 } 54 55 56 57 }
main.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context=".MainActivity" > 11 12 <EditText 13 android:id="@+id/EditTextOne" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 />" 17 18 <TextView 19 android:layout_width="fill_parent" 20 android:layout_height="wrap_content" 21 android:text="@string/result" 22 /> 23 24 25 <EditText 26 android:id="@+id/EditTextTwo" 27 android:layout_width="fill_parent" 28 android:layout_height="wrap_content" 29 /> 30 31 <Button 32 android:id="@+id/myButton" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:text="@string/calculate" 36 /> 37 </LinearLayout>
ResultActivity.java
1 import android.app.Activity; 2 import android.content.Intent; 3 import android.os.Bundle; 4 import android.widget.TextView; 5 6 public class ResultActivity extends Activity{ 7 private TextView myTextView; 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.result); 11 //获取Intent对象 12 Intent intent = getIntent(); 13 //得到Intent对象当中的值 14 String factorOneStr = intent.getStringExtra("one"); 15 String factorTwoStr = intent.getStringExtra("two"); 16 //将接收到的字符串转换成整数 17 int factorOneInt = Integer.parseInt(factorOneStr); 18 int factorOneTwo = Integer.parseInt(factorTwoStr); 19 //计算两个数的值 20 int result = factorOneInt*factorOneTwo; 21 22 23 myTextView = (TextView) findViewById(R.id.myTextView); 24 myTextView.setText(result); 25 26 } 27 28 }
result.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TexiView 8 android:id="@+id/myTextView" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 /> 12 13 14 </LinearLayout>
AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.mars.widget" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="18" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name="com.mars.widget.MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 <activity android:name="com.mars.widget.ResultActivity" android:label="@string/result"/>" 26 </application> 27 28 </manifest>
string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">EditText控件和menu控件</string> <string name="action_settings">Settings</string> <string name="result">乘以</string> <string name="calculate">计算</string> <string name="exit">退出</string> <string name="about">关于</string> </resources>