2015-12-08 60 views
0

我在手機上有1個按鈕的應用程序。我想通過Android Wear來觸發此按鈕。我怎樣才能做到這一點?如何在可穿戴設備上按一下按鈕並觸發掌上電腦的另一個按鈕?

java代碼仍然是由Android Studio創建的標準,我會壓制它們。

磨損XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.wearable.view.BoxInsetLayout 
    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/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="br.com.teste.wearbuttonclick.WearMainActivity" 
    tools:deviceIds="wear" 
    android:background="#5c6bc0" 
    > 

    <TextView 
     android:id="@+id/clock" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|start" 
     android:textColor="@android:color/white" 
     app:layout_box="all" /> 

    **<Button 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_gravity="center" 
     android:text="Click" 
     android:background="#5c6bc0" 
     android:textColor="#ffffff" 
     android:id="@+id/wearbutton" 
     />** 


</android.support.wearable.view.BoxInsetLayout> 

手機的xml:

<?xml version="1.0" encoding="utf-8"?> 
<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="br.com.teste.wearbuttonclick.MobileMainActivity"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Target Button" 
     android:id="@+id/mobilebutton" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 
</RelativeLayout> 
+0

我建議您閱讀Wear開發以及如何在磨損設備和co之間發送消息無論你的想法是什麼,儘可能地完成你的想法,然後再回來一些具體的編碼問題。就目前而言,您正在要求人們爲您編寫應用程序。在較高的層面上,您可以捕獲穿戴應用程序的點擊,向連接的手機應用程序發送消息,在手機端捕獲該消息並觸發點擊。 –

+0

Ali Naddaf, 我不是要你爲我編寫應用程序。我已經有一個功能齊全的應用程序。我只需要在Android Wear中實現這個附加功能。你的回答含糊不清。我希望別人想幫助我。 我研究了android磨損文檔,無法使其工作。 –

回答

0

上werable應用按鈕的點擊招呼發送消息,並在onMessageReceived手持設備()調用此方法以您的按鈕名稱: -

private void makeClick() { 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_LONG).show(); 
     } 
    }); 
    button.performClick(); 
} 
相關問題