Android学习(Mars Android)——S01_E06--常见控件的使用方法(计算器)

以下程序有错误,闪退 先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(数字输入框),1个TextView(乘以),1个Button(计算)。-->修改activity03_layout。xml文件
//2、要为其中的两个控件(1个TextView(乘以)、1个Button(计算))设置显示的值
//3、创建一个监听器类,用来监听按钮按下的动作
//4、将监听器类的对象绑定在按钮对象上

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)根据控件的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设置显示的值
//方法一:直接写入字符串“乘以”/“计算”。有点:简单方便;缺点:考虑到范围可能会有多种语言,直接赋值的方式,修改起来非常麻烦
//symbol.setText("乘以");
//calculate.setText("计算");
// 方法二:1)在string中配置字符串,然后由Activity03调用----推荐!
//()内不再是字符串,而是对应的Id.优点:应用程序中并无相应的值,只有其Id,真正的值在strings.xml中,如需修改,只需要在strings.xml中修改
symbol.setText(R.string.symbol);
calculate.setText(R.string.calculate);
//11)将监听器对象绑定到按钮(calculate)对象上面去
calculate.setOnClickListener(new CalculateListener());

}

//20)添加Menu菜单,回调函数,当在手机上电极Menu按钮时就会执行该函数---Menu于前面EditText,Button等相比,不需要在布局文件中进行配置,直接复写onCreatextMenuClosed(方法)即可
//该方法的作用是,当客户点击Menu按钮时候,就会调用该方法
@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)给退出/关于添加动作--->当点击Item时就会有动作,并把点击的值传进(item)去
//当客户点击菜单中的某一个选项时,会调用该方法,并把点击的选项最为参数传进去
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==1);
finish();
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}

//5)编写应用程序的监听器(内部类,比较常见的,eg:在多线程程序中使用内部类来隐藏run函数;在编写模板回调模式时使用匿名内部类作为回调接口的实现)
//1、在内部类中,可以直接使用外部类的成员变量,但不能拥有;2、可以再内部类中使用外部类的成员函数/对象
//如果对Listener不太了解,可以Google“Observer设计模式”-->有助于了解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对象当中
Intent intent = new Intent();
//8)调用intent。put()方法将键值对传进intent中去
intent.putExtra("one",factoroneStr);
intent.putExtra("two",factortwoStr);
//9)设置intent启动哪一个Activity
intent.setClass(Activity03.this, ResultActivity.class);
//10)使用这个Intent对象来启动ResultActivity-->调用现在的Activity的startActivity()方法,以启动跳转
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中所传入的值
//计算两个值得乘积
//将计算结果显示在Activity上-->要想显示,最少应该有一个TextView控件用来显示-->要在ResultActivity的布局文件当中配置一个TestView
//------以下步骤基本和Activity03类似------
//12)在result。xml中配置一个TextView
public class ResultActivity extends Activity{
//13)声明TextView控件的对象
private TextView resultView;
//14)导入OnCreate()方法的复写文件
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//15)使该Activity使用result。xml的布局
setContentView(R.layout.result);
//16)根据控件的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类型,无法相乘,所以还得转化成整型-->用Integer。parseInt()方法抽取一下
int factoroneInt = Integer.parseInt(factoroneStr);
int factortwoInt = Integer.parseInt(factortwoStr);
//18)计算两个值得乘积
int result = factoroneInt * factortwoInt;
//19)在result的TextView中(resultView中)将结果显示出来(注意:setText()的输出结果为String类型,所以要把结果转化为字符串类型,其方法为:根据Java语法:连接符两端有一端是字符串,另外一段也将会是字符串,然后显示,所以相乘结果也是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">关于</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中这样写时调试报错。

————————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);
//下行标记-->学习observe设计模式
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">关于</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;
}
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容