AndroidWear開發之HelloWorld篇

來源:互聯網
上載者:User

通過前一篇的學習,我們把環境都搭建好了,這下我們就可以正真的開發了。

一、建立Wear項目

通過項目建立嚮導一步一步下去就可以建立好一個Wear項目:

1.建立項目,一次填入應用程式名稱字,應用程式套件名,項目位置,搞android都很熟悉的。

2.這裡我們選擇Wear SDK

3.建立一個WearActivity,啟動Activity入口

4.起好名字,我們預設就可以了。

二、修正錯誤

稍等片刻as就幫你把項目建立好了,不過你會發現報錯了

系統找不到WatchActivity,這裡我google了一把,得到了答案wear support library是沒有這個WatchActivity,只有InsetActivity。

所以只要我們把WatchActivity換成InsetActivity,然後就會提示你要ovveride這個onReadyForContent方法,然後把onCreate裡的剪下到這裡就可以運行了

三、編輯代碼

代碼如下MyActivity.java

package com.example.bvin.androidwear_helloworld;import android.os.Bundle;import android.support.wearable.activity.InsetActivity;import android.support.wearable.view.WatchViewStub;import android.util.Log;import android.widget.TextView;public class MyActivity  extends InsetActivity {    private TextView mTextView;    private static String TAG = "MyActivity";    @Override    public void onReadyForContent() {        setContentView(R.layout.activity_my);        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {            @Override            public void onLayoutInflated(WatchViewStub stub) {                mTextView = (TextView) stub.findViewById(R.id.text);                Log.d(TAG, "TextView: " + mTextView.getText() + " view=" + mTextView);            }        });    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);    }}

activity_my.xml

<?xml version="1.0" encoding="utf-8"?><android.support.wearable.view.WatchViewStub    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/watch_view_stub"    android:layout_width="match_parent"    android:layout_height="match_parent"    app:rectLayout="@layout/rect_activity_my"    app:roundLayout="@layout/round_activity_my"    tools:context=".MyActivity"    tools:deviceIds="wear"></android.support.wearable.view.WatchViewStub>

注意到這裡有一個rectLayout一個roundLayout,一個圓形表的布局,一個方形表的布局。

四、建立模擬器

這樣就沒問題,可以直接運行啟動模擬器,這模擬器蠻好玩的。

建立一個圓形的模擬器

啟動,開機一系列教程,這介面美死了

可以雙擊開啟這個介面點start可以找到自己的程式

五、運行

嗯哼,模擬器是方的?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.