【发布时间】:2019-06-29 11:52:02
【问题描述】:
我正在向我的应用程序添加一个辅助功能,当用户按住主页按钮时,一个 android 活动将打开,用户可以单击该卡片,然后打开相关的 xamarin 表单页面,例如当数学点击卡片 MathPage xamarin 表单页面打开这里是我的 android 布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_tools_home_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:id="@+id/whole"
tools:context="${relativePackage}.${activityClass}"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="0dp">
<LinearLayout
android:layout_width="wrap_content"
android:id="@+id/scroll"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/cardsbg"
app:cardElevation="3dp"
android:clickable="false"
tools:layout_conversion_absoluteX="16dp"
tools:layout_conversion_absoluteY="8dp"
tools:layout_conversion_absoluteWidth="379dp"
tools:layout_conversion_absoluteHeight="540dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/webcard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:foreground="?android:attr/selectableItemBackground"
app:cardBackgroundColor="@color/card_background"
app:cardElevation="3dp"
android:clickable="true"
android:onClick="web">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/webicon"
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_centerVertical="true"
android:padding="5dp"
android:src="@drawable/web" />
<TextView
android:id="@+id/web"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/webicon"
android:text="Internet"
android:paddingLeft="8dp"
android:textColor="@color/cardText"
android:textSize="20sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/mailcard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:layout_marginBottom="4dp"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="4dp"
android:layout_below="@+id/webcard"
app:cardBackgroundColor="@color/card_background"
app:cardElevation="3dp"
android:clickable="true"
android:onClick="email">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/emailicon"
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_centerVertical="true"
android:padding="5dp"
android:src="@drawable/email" />
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/emailicon"
android:text="Email"
android:textColor="@color/cardText"
android:textSize="20sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/mathcard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:layout_marginBottom="4dp"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="4dp"
android:layout_below="@+id/mailcard"
app:cardBackgroundColor="@color/card_background"
app:cardElevation="3dp"
android:clickable="true"
android:onClick="math">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/musicicon"
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_centerVertical="true"
android:padding="5dp"
android:src="@drawable/math" />
<TextView
android:id="@+id/music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/musicicon"
android:text="Math"
android:paddingLeft="8dp"
android:textColor="@color/cardText"
android:textSize="20sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/scicard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="4dp"
android:layout_below="@+id/mathcard"
app:cardBackgroundColor="@color/card_background"
app:cardElevation="3dp"
android:clickable="true"
android:onClick="science">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/playicon"
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_centerVertical="true"
android:padding="8dp"
android:src="@drawable/sci" />
<TextView
android:id="@+id/play"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="8dp"
android:layout_toRightOf="@+id/playicon"
android:text="Science"
android:textColor="@color/cardText"
android:textSize="20sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/englishcard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="4dp"
android:layout_below="@+id/scicard"
app:cardBackgroundColor="@color/card_background"
app:cardElevation="3dp"
android:clickable="true"
android:onClick="english">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/playicon"
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_centerVertical="true"
android:padding="8dp"
android:src="@drawable/handwrittin" />
<TextView
android:id="@+id/play"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="8dp"
android:layout_toRightOf="@+id/playicon"
android:text="Handwriting"
android:textColor="@color/cardText"
android:textSize="20sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/flashcard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="4dp"
android:layout_below="@+id/englishcard"
app:cardBackgroundColor="@color/card_background"
app:cardElevation="3dp"
android:clickable="true"
android:onClick="flashcard">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/playicon"
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_centerVertical="true"
android:padding="8dp"
android:src="@drawable/flashcards" />
<TextView
android:id="@+id/play"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="8dp"
android:layout_toRightOf="@+id/playicon"
android:text="Flashcards"
android:textColor="@color/cardText"
android:textSize="20sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<RelativeLayout
android:id="@+id/bttom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/flashcard"
android:paddingTop="8dp"
android:layout_marginBottom="8dp">
<Button
android:id="@+id/button_2"
android:onClick="cam"
android:foreground="?android:attr/selectableItemBackground"
android:layout_alignParentLeft="true"
android:layout_marginLeft="50dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/settings" />
<Button
android:id="@+id/button_1"
android:onClick="settings"
android:foreground="?android:attr/selectableItemBackground"
android:layout_alignParentRight="true"
android:layout_marginRight="50dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/settings" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<Button
android:layout_width="72dp"
android:layout_height="72dp"
android:id="@+id/button_4"
android:onClick="now"
android:layout_marginTop="-42dp"
android:layout_below="@+id/scroll"
android:foreground="?android:attr/selectableItemBackground"
android:clipToPadding="false"
android:layout_centerHorizontal="true"
android:background="@drawable/books" />
</RelativeLayout>
</RelativeLayout>
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms.Platform.Android;
namespace AppName.Droid
{
[Activity(LaunchMode = LaunchMode.SingleInstance, Theme = "@style/Theme.Transparent")]
[IntentFilter(new[] { Intent.ActionAssist }, Categories = new[] { Intent.CategoryDefault })]
public class ToolBelt : FormsApplicationActivity
{
Button mathbutton;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.ToolBelt);
}
//Code That Opens The Math Part!
[Java.Interop.Export("math")] // The value found in android:onClick attribute.
public void btnOneClick4(View v) // Does not need to match value in above attribute.
{
//code to open the MathPage Forms Page Goes here
}
//Code That Opens The Internet App!
[Java.Interop.Export("web")] // The value found in android:onClick attribute.
public void btnOneClick8(View v) // Does not need to match value in above attribute.
{
var uri = Android.Net.Uri.Parse("http://www.google.com");
var intent = new Intent(Intent.ActionView, uri);
StartActivity(intent);
}
//Code That Opens The Gmail App!
[Java.Interop.Export("email")] // The value found in android:onClick attribute.
public void btnOneClick3(View v) // Does not need to match value in above attribute.
{
var intent = PackageManager.GetLaunchIntentForPackage("com.google.android.gm");
StartActivity(intent);
}
}
}
【问题讨论】:
-
您需要启动 MainLauncher Activity(初始化
Xamarin.Forms的那个)并带有一些 Intent 附加功能,然后将这些附加功能传递到表单的应用程序中,并根据该信息打开适当的基于表单的页面. -
@SushiHangover 感谢您的回复!你能给我一个链接或一些示例代码吗?
标签: c# xamarin xamarin.android xamarin.forms