Helo我正在为我的应用程序添加一个辅助功能,当用户按住主页按钮时,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);
}
}
}
任何帮助都会很棒!
提前致谢:)
答案 0 :(得分:1)
这是一个简单的例子......
Activity
:用户点按一个按钮,我们创建一个Intent
来打开主要表单&#39;基于Activity
并使用&#34;页面添加字符串Extra
&#34;那个用户选择了。
[Activity(Label = "PageSelectionActivity", Theme = "@style/MyTheme", Name = "com.sushhangover.openpage.PageSelectionActivity")]
public class PageSelectionActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.PageSelection);
Button button = FindViewById<Button>(Resource.Id.myButton);
button.Click += delegate
{
var intent = new Intent(ApplicationContext, typeof(MainActivity));
intent.PutExtra("page", "StackOverflowPage");
StartActivity(intent);
};
}
}
MainActivity
以获取Extra(如果有)并将其传递给App .actor:[Activity(Label = "OpenPage.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
var page = Intent.GetStringExtra("page");
LoadApplication(new App(page));
}
}
public class App : Application
{
public App(string pageName = "SomeDefaultMainApplicationEntryPage")
{
switch (pageName)
{
case "StackOverflowPage":
MainPage = new NavigationPage(new StackOverflowPage);
break;
default:
MainPage = new NavigationPage(new SomeDefaultMainApplicationEntryPage);
break;
}
}
}
您可以使用adb打开页面选择活动以启动它:
adb shell am start -n com.sushhangover.openpage/.PageSelectionActivity
注意:您需要处理Xamarin.Form
页面生命周期,因为它可能会变得混乱,具体取决于您的ToolBelt
活动是否已在用户已有您的活动时打开应用程序打开,它处于某些活动的中间,你通过重新启动应用程序来破坏他们的工作....