Android个人笔记 - Google Android揭秘_第三章:用户界面

来源:互联网 发布:js美化 编辑:程序博客网 时间:2024/05/16 09:55
Android概念:活动(Activity),视图(View),资源(Resource)
Android不允许任何应用程序从用户或者其他应用程序获得对设备的控制,而是使用明确定义的生命周期根据需要来管理进程。

3.1 创建活动

    android.app.Activity功能:为应用程序提供上下文;使用Android生命周期的方法;为框架提供一个钩子,以启动和运行你的应用程序;并且提供一个可放置View元素的容器。当包括必要的视图的Activity启动以后,该Activity的生命周期接管其进程并调用onCreat()方法,该方法是Activity类提供一系列重要的生命周期方法之一。

    cuisine:选择列表组件,Android称之为:Spinner - private Spinner cuisine;

   

    @Override    public boolean onCreateOptionsMenu(Menu menu) {        super.onCreateOptionsMenu(menu);        menu.add(0, ReviewCriteria.MENU_GET_REVIEWS, 0, R.string.menu_get_reviews).setIcon(            android.R.drawable.ic_menu_more);        return true;    }    //创建菜单项,屏幕下面的菜单

    @Override    public boolean onMenuItemSelected(int featureId, MenuItem item) {        switch (item.getItemId()) {            case MENU_GET_REVIEWS:                handleGetReviews();                return true;        }        return super.onMenuItemSelected(featureId, item);    }    //当菜单项被选中时作出响应

    private void handleGetReviews() {        if (!validate()) {            return;        }        // use the "Application" to store global state (can go beyond primitives and Strings -        // beyond extras - if needed) 使用Application对象存储状态,活动之间传递全局变量        RestaurantFinderApplication application = (RestaurantFinderApplication) getApplication();        application.setReviewCriteriaCuisine(this.cuisine.getSelectedItem().toString());        application.setReviewCriteriaLocation(this.location.getText().toString());        // call next Activity, VIEW_LIST        Intent intent = new Intent(Constants.INTENT_ACTION_VIEW_LIST);        startActivity(intent);//启动活动,调用下一个屏幕    }

    使用startActivity还是startActivityForResult不同之处在于:相同之处都是将程序控制权交给另一个Activity,第二个不同是:当被调用的Activity结束后该方法向当前Activity返回一个值。

    // validate form fields    private boolean validate() {        boolean valid = true;        StringBuilder validationText = new StringBuilder();        if ((this.location.getText() == null) || this.location.getText().toString().equals("")) {            validationText.append(getResources().getString(R.string.location_not_supplied_message));            valid = false;        }        if (!valid) {//Builder模式,下面的this使用,因为每个方法(AlertDialog)都会返回一个自身的引用(this),这样可以嵌套方法调用            new AlertDialog.Builder(this).setTitle(getResources().getString(R.string.alert_label)).setMessage(                validationText.toString()).setPositiveButton("Continue",                new android.content.DialogInterface.OnClickListener() {                    public void onClick(DialogInterface dialog, int arg1) {                        // in this case, don't need to do anything other than close alert                    }                }).show();            validationText = null;        }        return valid;    }

3.12 探讨活动的生命周期


0 0
原创粉丝点击