这篇文章将给大家介绍TextView EditText Button Menu这几个控件的使用方法,同时在布局文件里面还有一个新的东西,线性布局,以及string.xml的使用,这里的string.xml是用来做国际化用的。
1、创建第一个Activity,ThridLessonDemo1.java
1 package com.example.helloworld; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.Menu; 7 import android.view.MenuItem; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 import android.widget.EditText; 12 import android.widget.TextView; 13 14 15 public class ThridLessonDemo1 extends Activity { 16 private EditText num1; 17 private EditText num2; 18 private TextView myTextView; 19 private Button calculate; 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 // TODO Auto-generated method stub 24 super.onCreate(savedInstanceState); 25 // 根据控件的ID来取得代表控件的对象 26 setContentView(R.layout.thridfirst); 27 num1 = (EditText) findViewById(R.id.Numo); 28 num2 = (EditText) findViewById(R.id.Numt); 29 myTextView = (TextView) findViewById(R.id.myTextView); 30 calculate = (Button) findViewById(R.id.calculate); 31 myTextView.setText(R.string.cy); 32 calculate.setText(R.string.js); 33 calculate.setOnClickListener(new myButtonListener()); 34 } 35 36 class myButtonListener implements OnClickListener { 37 38 public void onClick(View arg0) { 39 // TODO Auto-generated method stub 40 String v1 = num1.getText().toString(); 41 String v2 = num2.getText().toString(); 42 Intent intent = new Intent(); 43 intent.putExtra("num1", v1); 44 intent.putExtra("num2", v2); 45 intent.setClass(ThridLessonDemo1.this, ThridLessonDemo2.class); 46 startActivity(intent); 47 } 48 49 } 50 //当客户点击MENU按钮时调用该方法 51 @Override 52 public boolean onCreateOptionsMenu(Menu menu) { 53 54 menu.add(0, 1, 1, R.string.ext); 55 menu.add(0, 2, 2, R.string.about); 56 57 // TODO Auto-generated method stub 58 return super.onCreateOptionsMenu(menu); 59 } 60 //当客户点击菜单当中的某一个选项时,会调用该方法 61 @Override 62 public boolean onOptionsItemSelected(MenuItem item) { 63 if (item.getItemId() == 1) { 64 finish(); 65 } 66 67 // TODO Auto-generated method stub 68 return super.onOptionsItemSelected(item); 69 } 70 71 }
2、创建第二个Activity,ThridLessonDemo2.java
1 package com.example.helloworld; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.widget.TextView; 7 8 9 public class ThridLessonDemo2 extends Activity { 10 11 private TextView resultView; 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 // TODO Auto-generated method stub 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.thridsecond); 18 resultView = (TextView) findViewById(R.id.resultView); 19 Intent intent = getIntent(); 20 String num1 = intent.getStringExtra("num1"); 21 String num2 = intent.getStringExtra("num2"); 22 int _num1 = Integer.parseInt(num1); 23 int _num2 = Integer.parseInt(num2); 24 int result = _num1 * _num2; 25 resultView.setText(result + ""); 26 } 27 28 }
3、创建第一个Activity的布局文件,thridfirst.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <EditText 8 android:id="@+id/Numo" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:inputType="number" /> 12 13 <TextView 14 android:id="@+id/myTextView" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" /> 17 18 <EditText 19 android:id="@+id/Numt" 20 android:layout_width="fill_parent" 21 android:layout_height="wrap_content" 22 android:inputType="number" /> 23 24 <Button 25 android:id="@+id/calculate" 26 android:layout_width="fill_parent" 27 android:layout_height="wrap_content" /> 28 29 </LinearLayout>
4、创建第二个Activity的布局文件,thridsecond.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/resultView" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" /> 11 12 </LinearLayout>
5、修改res/values/strings.xml
1 <resources> 2 3 <string name="app_name">HelloWorld</string> 4 <string name="hello_world">Hello world!</string> 5 <string name="menu_settings">Settings</string> 6 <string name="title_activity_main">MainActivity</string> 7 <string name="other">otherActivity</string> 8 <string name="ok">确定</string> 9 <string name="cy">乘以</string> 10 <string name="js">计算</string> 11 <string name="ext">退出</string> 12 <string name="about">关于</string> 13 </resources>
6、注册这两个Activity
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.example.helloworld" 3 android:versionCode="1" 4 android:versionName="1.0" > 5 6 <uses-sdk 7 android:minSdkVersion="4" 8 android:targetSdkVersion="15" /> 9 10 <application 11 android:icon="@drawable/ic_launcher" 12 android:label="@string/app_name" 13 android:theme="@style/AppTheme" > 14 <activity 15 android:name=".MainActivity" 16 android:label="@string/title_activity_main" > 17 </activity> 18 <activity 19 android:name=".SecondLessonDemo1" 20 android:label="@string/other" > 21 </activity> 22 <activity 23 android:name=".SecondLessonDemo2" 24 android:label="dd" > 25 </activity> 26 <activity 27 android:name=".ThridLessonDemo1" 28 android:label="dd2" > 29 <intent-filter> 30 <action android:name="android.intent.action.MAIN" /> 31 32 <category android:name="android.intent.category.LAUNCHER" /> 33 </intent-filter> 34 </activity> 35 <activity 36 android:name=".ThridLessonDemo2" 37 android:label="dd3" > 38 </activity> 39 </application> 40 41 </manifest>
--------------------------------------------------------------------------------------------------------------------------------------------
顺势而为
顺势而为