您的位置:首页 > 移动开发 > Android开发

Android学习笔记(二)

2011-11-10 10:13 267 查看
       昨天只是打了打基础今天开始翻阅android官方文档,一开始省事下载了一些中文文档观看,但是发现这些文档不全,翻译有些硬伤,譬如说是把表面的意思翻译出来了但是有些可以扩展的知识点没有点明。属性的含义介绍清楚了,但是属性值没有给出,很是纠结,没有办法只能用我那蹩脚的英语去看官方文档,但是还是官方那个文档给出的说明详细,不懂得可以金山来帮忙,俗话说实践才是硬道理,今天把官方文档中的一些我感觉比较有用的例子拿到这里分析一下,(水平有限能力有限,如有不对的地方,还请多多指教。)

案例一:

ReceiveResult.java

/**
 * Shows how an activity can send data to its launching activity when done.y.
 * <p>This can be used, for example, to implement a dialog alowing the user to
pick an e-mail address or image -- the picking activity sends the selected
data back to the originating activity when done.</p>

<p>The example here is composed of two activities: ReceiveResult launches
the picking activity and receives its results; SendResult allows the user
to pick something and sends the selection back to its caller.  Implementing
this functionality involves the
{@link android.app.Activity#setResult setResult()} method for sending a
result and
{@link android.app.Activity#onActivityResult onActivityResult()} to
receive it.</p>
**/
/**
本人翻译如下:
首先该例子展示的是一个activity当他完成搜集信息的工作室怎样向他需要的已经打开的activity的时候发送数据。
这个很有用,例如继承一个dialog类,它可以提供用户选择是发送email和发送图片的选择,当用户选择完毕时,该对话框的activity将用户选择的信息返回给源activity
(我理解为能够处理该信息的activity)。
本例子中向我们展示的有:首先案例中有两个activity,一个是提供用户选择信息的activity,另一个是接受结果并对其进行相应的activity,选择信息的activity可以允许用
户选择要做的选择或要发送的信息给处理的activity,实现这个函数包括下面两个(下面的我就不做翻译了,同志们都懂得)贴上源程序:
**/

public class ReceiveResult extends Activity {
    /**
     * Initialization of the Activity after it is first created.  Must at least
     * call {@link android.app.Activity#setContentView setContentView()} to
     * describe what is to be displayed in the screen.
     */
    @Override
        protected void onCreate(Bundle savedInstanceState) {
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);

        // See assets/res/any/layout/hello_world.xml for this(查看结构文档中的定义内容,关于定义布局什么的都在那里)
        // view layout definition, which is being set here as
        // the content of our screen.
        setContentView(R.layout.receive_result);

        // Retrieve the TextView widget that will display results.(将TextView Widget放置在面板上)
        mResults = (TextView)findViewById(R.id.results);

        // This allows us to later extend the text buffer.(这个可以允许我们稍后继承textbuffer)
        mResults.setText(mResults.getText(), TextView.BufferType.EDITABLE);

        // Watch for button clicks.
        Button getButton = (Button)findViewById(R.id.get);
        getButton.setOnClickListener(mGetListener);
    }

    /**
     * This method is called when the sending activity has finished, with the
     * result it supplied.
     *
     * @param requestCode The original request code as given to
     *                    startActivity().
     * @param resultCode From sending activity as per setResult().
     * @param data From sending activity as per setResult().
     */
    @Override
        protected void onActivityResult(int requestCode, int resultCode,
                Intent data) {
        // You can use the requestCode to select between multiple child
        // activities you may have started.  Here there is only one thing
        // we launch.
        if (requestCode == GET_CODE) {

            // We will be adding to our text.
            Editable text = (Editable)mResults.getText();

            // This is a standard resultCode that is sent back if the
            // activity doesn't supply an explicit result.  It will also
            // be returned if the activity failed to launch.
            if (resultCode == RESULT_CANCELED) {
                text.append("(cancelled)");

            // Our protocol with the sending activity is that it will send
            // text in 'data' as its result.
            } else {
                text.append("(okay ");
                text.append(Integer.toString(resultCode));
                text.append(") ");
                if (data != null) {
                    text.append(data.getAction());
                }
            }

            text.append("\n");
        }
    }

    // Definition of the one requestCode we use for receiving resuls.
    static final private int GET_CODE = 0;

    private OnClickListener mGetListener = new OnClickListener() {
        public void onClick(View v) {
            // Start the activity whose result we want to retrieve.  The
            // result will come back with request code GET_CODE.
            Intent intent = new Intent(ReceiveResult.this, SendResult.class);
            startActivityForResult(intent, GET_CODE);
        }
    };

    private TextView mResults;
}

(extView.BufferType.EDITABLE 和 TextView.BufferType.SPANNABLE

有什么区别呢?我们透过UML瞧一下, Editable 类似于StringBuilder可追加字符,

也就是说getText后可调用append方法设置文本内容。Spannable 则可在给定的字符区域使用样式。有意思的是 Editable 继承了 Spannable 所以具备较多的功能。)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息