以下程序有錯誤绅作,閃退 先mark下对湃,回頭改
————————Activity03————————
package com.example.activity03;
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.Button;
import android.widget.EditText;
import android.widget.TextView;
//1温治、 在Activity03中什聲明四個控件:2個EditText(數(shù)字輸入框)扯罐,1個TextView(乘以)陆赋,1個Button(計算)。-->修改activity03_layout鸠信。xml文件
//2优烧、要為其中的兩個控件(1個TextView(乘以)主经、1個Button(計算))設(shè)置顯示的值
//3杉编、創(chuàng)建一個監(jiān)聽器類,用來監(jiān)聽按鈕按下的動作
//4蛙卤、將監(jiān)聽器類的對象綁定在按鈕對象上
public class Activity03 extends Activity {
//1)在cativity03_layout.xml中聲明好了四個控件過后,
//2)在Activity中將這四個控件取出來:-->聲明代表這四個控件的對象-->用findViewById()方法取出控件
private EditText factorone;
private EditText factortwo;
private TextView symbol;
private Button calculate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity03_layout);
//3)根據(jù)控件的ID來取得代表控件的對象-->findViewById(R.id.**)
factorone=(EditText)findViewById(R.id.factorone);
factortwo=(EditText)findViewById(R.id.factortwo);
symbol=(TextView)findViewById(R.id.symbol);
calculate=(Button)findViewById(R.id.calculate);
//4)為symbol和calculate設(shè)置顯示的值
//方法一:直接寫入字符串“乘以”/“計算”寥茫。有點:簡單方便挠进;缺點:考慮到范圍可能會有多種語言,直接賦值的方式辕坝,修改起來非常麻煩
//symbol.setText("乘以");
//calculate.setText("計算");
// 方法二:1)在string中配置字符串窍奋,然后由Activity03調(diào)用----推薦!
//()內(nèi)不再是字符串酱畅,而是對應(yīng)的Id.優(yōu)點:應(yīng)用程序中并無相應(yīng)的值琳袄,只有其Id,真正的值在strings.xml中纺酸,如需修改窖逗,只需要在strings.xml中修改
symbol.setText(R.string.symbol);
calculate.setText(R.string.calculate);
//11)將監(jiān)聽器對象綁定到按鈕(calculate)對象上面去
calculate.setOnClickListener(new CalculateListener());
}
//20)添加Menu菜單,回調(diào)函數(shù)餐蔬,當(dāng)在手機上電極Menu按鈕時就會執(zhí)行該函數(shù)---Menu于前面EditText碎紊,Button等相比,不需要在布局文件中進(jìn)行配置樊诺,直接復(fù)寫onCreatextMenuClosed(方法)即可
//該方法的作用是仗考,當(dāng)客戶點擊Menu按鈕時候,就會調(diào)用該方法
@Override
public void onContextMenuClosed(Menu menu) {
menu.add(0, 1, 1, R.string.exit);
menu.add(0,2,2,R.string.about);
// TODO Auto-generated method stub
//return super.onContextMenuClosed(menu);
super.onContextMenuClosed(menu);
}
//21)給退出/關(guān)于添加動作--->當(dāng)點擊Item時就會有動作词爬,并把點擊的值傳進(jìn)(item)去
//當(dāng)客戶點擊菜單中的某一個選項時秃嗜,會調(diào)用該方法,并把點擊的選項最為參數(shù)傳進(jìn)去
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==1);
finish();
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
//5)編寫應(yīng)用程序的監(jiān)聽器(內(nèi)部類顿膨,比較常見的锅锨,eg:在多線程程序中使用內(nèi)部類來隱藏run函數(shù);在編寫模板回調(diào)模式時使用匿名內(nèi)部類作為回調(diào)接口的實現(xiàn))
//1恋沃、在內(nèi)部類中橡类,可以直接使用外部類的成員變量,但不能擁有芽唇;2顾画、可以再內(nèi)部類中使用外部類的成員函數(shù)/對象
//如果對Listener不太了解,可以Google“Observer設(shè)計模式”-->有助于了解Listener
class CalculateListener implements OnClickListener{
@Override
public void onClick(View v) {
//6)取得兩個EditText控件的值
String factoroneStr = factorone.getText().toString();
String factortwoStr = factortwo.getText().toString();
//7)將這兩個值存放到Intent對象當(dāng)中
Intent intent = new Intent();
//8)調(diào)用intent匆笤。put()方法將鍵值對傳進(jìn)intent中去
intent.putExtra("one",factoroneStr);
intent.putExtra("two",factortwoStr);
//9)設(shè)置intent啟動哪一個Activity
intent.setClass(Activity03.this, ResultActivity.class);
//10)使用這個Intent對象來啟動ResultActivity-->調(diào)用現(xiàn)在的Activity的startActivity()方法研侣,以啟動跳轉(zhuǎn)
Activity03.this.startActivity(intent);
}
}
}
——————activity03_layout.xml——————
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.activity03.Activity03" >
<EditText
android:id="@+id/factorone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/symbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/>
<Editext
android:id="@+id/factortwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
—————————ResultActivity————————
package com.example.activity03;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
//接收從Activity03中所傳入的值
//計算兩個值得乘積
//將計算結(jié)果顯示在Activity上-->要想顯示,最少應(yīng)該有一個TextView控件用來顯示-->要在ResultActivity的布局文件當(dāng)中配置一個TestView
//------以下步驟基本和Activity03類似------
//12)在result炮捧。xml中配置一個TextView
public class ResultActivity extends Activity{
//13)聲明TextView控件的對象
private TextView resultView;
//14)導(dǎo)入OnCreate()方法的復(fù)寫文件
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//15)使該Activity使用result庶诡。xml的布局
setContentView(R.layout.result);
//16)根據(jù)控件的Id來得到控件對象
resultView= (TextView)findViewById(R.id.result);
//得到intent對象中的值
//1、先得到intent對象
Intent intent = getIntent();
//2咆课、把Activity03中傳入的鍵再傳入intent末誓。getStringExtra()方法中扯俱,以通過鍵得到值
String factoroneStr = intent.getStringExtra("one");
String factortwoStr = intent.getStringExtra("two");
//17)通過鍵得出的值都是String類型,無法相乘喇澡,所以還得轉(zhuǎn)化成整型-->用Integer迅栅。parseInt()方法抽取一下
int factoroneInt = Integer.parseInt(factoroneStr);
int factortwoInt = Integer.parseInt(factortwoStr);
//18)計算兩個值得乘積
int result = factoroneInt * factortwoInt;
//19)在result的TextView中(resultView中)將結(jié)果顯示出來(注意:setText()的輸出結(jié)果為String類型,所以要把結(jié)果轉(zhuǎn)化為字符串類型晴玖,其方法為:根據(jù)Java語法:連接符兩端有一端是字符串读存,另外一段也將會是字符串,然后顯示呕屎,所以相乘結(jié)果也是String類型)
resultView.setText(result + "");
}
}
—————————result.xml—————————
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
—————————string.xml——————————
<resources>
<string name="app_name">Activity03</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="symbol">乘以</string>
<string name="calculate">計算</string>
<string name="exit">退出</string>
<string name="about">關(guān)于</string>
</resources>
——————Activity03Manifest.xml———————
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activity03"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Activity03"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
——————————R.java———————————
/* AUTO-GENERATED FILE. DO NOT MODIFY.
- This class was automatically generated by the
- aapt tool from the resource data it found. It
- should not be modified by hand.
*/
package com.example.activity03;
public final class R {
public static final class attr {
}
public static final class dimen {
/** Default screen margins, per the Android Design guidelines.
Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).
*/
public static final int activity_horizontal_margin=0x7f040000;
public static final int activity_vertical_margin=0x7f040001;
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int action_settings=0x7f080005;
public static final int calculate=0x7f080003;
public static final int factorone=0x7f080000;
public static final int factortwo=0x7f080002;
public static final int result=0x7f080004;
public static final int symbol=0x7f080001;
}
public static final class layout {
public static final int activity03_layout=0x7f030000;
public static final int result=0x7f030001;
}
public static final class menu {
public static final int activity03=0x7f070000;
}
public static final class string {
public static final int about=0x7f050006;
public static final int action_settings=0x7f050002;
public static final int app_name=0x7f050000;
public static final int calculate=0x7f050004;
public static final int exit=0x7f050005;
public static final int hello_world=0x7f050001;
public static final int symbol=0x7f050003;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
API 11 theme customizations can go here.
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
API 14 theme customizations can go here.
/
public static final int AppBaseTheme=0x7f060000;
/* Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f060001;
}
}
以下是新寫代碼让簿,但bug仍未解決,估計是Activity03中,caiculate.setOnClickListener((android.view.view.OnClickListener)new calculateListener());方法錯誤秀睛,
原文中是:
caiculate.setOnClickListener(new calculateListener());
但我在Eclipse中這樣寫時調(diào)試報錯尔当。
————————Activity03————————
package com.example.actyvity03;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Activity03 extends Activity {
private EditText factorOne;
private EditText factorTwo;
private TextView symbol;
private Button calculate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
factorOne=(EditText)findViewById(R.id.factorOne);
factorTwo=(EditText)findViewById(R.id.factorTwo);
symbol=(TextView)findViewById(R.id.symbol);
calculate=(Button)findViewById(R.id.calculate);
symbol.setText(R.string.symbol);
calculate.setText(R.string.calculate);
//下行標(biāo)記-->學(xué)習(xí)observe設(shè)計模式
calculate.setOnClickListener( (android.view.View.OnClickListener) new calculateListener());
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.add(0, 1, 1, R.string.exit);
menu.add(0,2,2,R.string.about);
// TODO Auto-generated method stub
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
class calculateListener implements OnClickListener{
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String factorOneStr = factorOne.getText().toString();
String factorTwoStr = factorTwo.getText().toString();
Intent intent = new Intent();
intent.putExtra("one", factorOneStr);
intent.putExtra("two", factorTwoStr);
intent.setClass(Activity03.this,ResultActivity.class);
Activity03.this.startActivity(intent);
}
}
}
——————activity03_layout(main).xml——————
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.actyvity03.Activity03" >
<EditText
android:id="@+id/factorOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/symbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<EditText
android:id="@+id/factorTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
—————————ResultActivity————————
package com.example.actyvity03;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ResultActivity extends Activity {
private TextView resultView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
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 + "");
}
}
—————————result.xml—————————
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
——————Activity03Manifest.xml———————
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.actyvity03"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Activity03"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<ResultActivity
android:name=".ResultActivity"
android:label="@string/app_name"
/>
</application>
</manifest>
—————————string.xml——————————
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Actyvity03</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="symbol">乘以</string>
<string name="calculate">計算</string>
<string name="exit">退出</string>
<string name="about">關(guān)于</string>
</resources>
——————————R.java———————————
/* AUTO-GENERATED FILE. DO NOT MODIFY.
- This class was automatically generated by the
- aapt tool from the resource data it found. It
- should not be modified by hand.
*/
package com.example.actyvity03;
public final class R {
public static final class attr {
}
public static final class dimen {
/** Default screen margins, per the Android Design guidelines.
Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).
*/
public static final int activity_horizontal_margin=0x7f040000;
public static final int activity_vertical_margin=0x7f040001;
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int action_settings=0x7f080005;
public static final int calculate=0x7f080003;
public static final int factorOne=0x7f080000;
public static final int factorTwo=0x7f080002;
public static final int result=0x7f080004;
public static final int symbol=0x7f080001;
}
public static final class layout {
public static final int main=0x7f030000;
public static final int result=0x7f030001;
}
public static final class menu {
public static final int activity03=0x7f070000;
}
public static final class string {
public static final int about=0x7f050006;
public static final int action_settings=0x7f050002;
public static final int app_name=0x7f050000;
public static final int calculate=0x7f050004;
public static final int exit=0x7f050005;
public static final int hello_world=0x7f050001;
public static final int symbol=0x7f050003;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
API 11 theme customizations can go here.
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
API 14 theme customizations can go here.
/
public static final int AppBaseTheme=0x7f060000;
/* Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f060001;
}
}