【问题标题】:Sub Class for Xamarin.Forms, Event handler for buttonsXamarin.Forms 的子类,按钮的事件处理程序
【发布时间】:2017-09-19 18:56:13
【问题描述】:

我正在尝试为学校项目制作适用于 Android 的蓝牙控制器。见图片:

axml 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:text="0"
    android:textSize="70sp"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_above="@+id/bar"
    android:gravity="center"
    android:id="@+id/text" />
<ProgressBar
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_above="@+id/button_forward"
    android:rotation="90"
    android:progress="100"
    android:id="@+id/bar" />
<Button
    android:text="Forward"
    android:textSize="16dp"
    android:layout_height="120dp"
    android:layout_width="120dp"
    android:layout_above="@+id/button_fire"
    android:layout_centerHorizontal="true"
    android:id="@+id/button_forward" />
<Button
    android:text="Fire"
    android:textSize="16dp"
    android:layout_height="120dp"
    android:layout_width="120dp"
    android:layout_above="@+id/button_backward"
    android:layout_centerHorizontal="true"
    android:id="@+id/button_fire" />
<Button
    android:text="Backward"
    android:textSize="16dp"
    android:layout_height="120dp"
    android:layout_width="120dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/button_backward" />
<Button
    android:text="Left"
    android:textSize="16dp"
    android:layout_height="120dp"
    android:layout_width="120dp"
    android:layout_above="@+id/button_backward"
    android:layout_toLeftOf="@+id/button_fire"
    android:id="@+id/button_left" />
<Button
    android:text="Right"
    android:textSize="16dp"
    android:layout_height="120dp"
    android:layout_width="120dp"
    android:layout_above="@+id/button_backward"
    android:layout_toRightOf="@+id/button_fire"
    android:id="@+id/button_right" />
</RelativeLayout>

现在我需要在按钮上获取 2 个事件处理程序,分别用于按下和释放时。这个想法就像在这篇文章中一样:Xamarin.Forms - Button Pressed & Released Event 但我不知道如何在 Xamarin.Forms 下制作子类。

这是我MainActivity的代码:

using System;
using System.Collections;
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Bluetooth;
using Android.Content;
using Android.Views;

namespace Bluetooth_Controller
{
    [Activity(Label = "Bluetooth_Controller", MainLauncher = true)]
    public class MainActivity : Activity
    {
        BluetoothAdapter BTAdapter;
        Button button_Forward, button_Fire, button_Backward, button_Left, button_Right;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Initialize Components
            Initialize();

            // Get local Bluetooth Adapter
            BTAdapter = BluetoothAdapter.DefaultAdapter; // Default adapter
        }

        public void Initialize()
        {
            button_Forward      = (Button)FindViewById(Resource.Id.button_forward);
            button_Fire         = (Button)FindViewById(Resource.Id.button_fire);
            button_Backward     = (Button)FindViewById(Resource.Id.button_backward);
            button_Left         = (Button)FindViewById(Resource.Id.button_left);
            button_Right        = (Button)FindViewById(Resource.Id.button_right);
        }
    }
}

【问题讨论】:

    标签: c# events bluetooth xamarin.forms xamarin.android


    【解决方案1】:

    修改 OnCreate 以获取对按钮的引用。然后调用 SetOnTouchListener 为触摸事件提供处理程序:

    protected override void OnCreate(Bundle bundle)
    {
       base.OnCreate(bundle);
       SetContentView(Resource.Layout.Main);
       button_Forward = FindViewById<Button>(Resource.Id.button_forward);
       button_Forward.SetOnTouchListener(this);
    }
    

    更改 MainActivity.cs 使其实现 View.IOnTouchListener,并添加接口所需的 OnTouch 方法。添加以下代码以重新定位 Button 以响应在屏幕上移动的触摸:

    public class Activity1 : Activity, View.IOnTouchListener
    {
       public bool OnTouch(View v, MotionEvent e)
       {
           switch (e.Action)
           {
               case MotionEventActions.Down:
                    //Do whatever you want in Down Key
                   break;
               case MotionEventActions.Up:
                  //Do whatever you want in Up Key
                   break;
           }
           return true;
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      相关资源
      最近更新 更多