###前提・実現したいこと
以前、StackOverflowでOnTouchでボタンの長押しをした時の処理の書き方を教えてもらい、その内容を元に、
同様の動きをするボタンが複数あり、ボタンごとに別の変数のカウントアップorダウンができるように作り変えたいと思っています。
現在のプログラムは以下の通りです。
###該当のソースコード
using System; using System.Diagnostics; using Android.App; using Android.OS; using Android.Views; using Android.Widget; namespace test2 { [Activity(Label = "test2", MainLauncher = true, Icon = "@mipmap/icon")] public class MainActivity : Activity, View.IOnTouchListener { System.Threading.CancellationTokenSource cancellationTokenSource; int count = 1; private Button button; private Button button2; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); // Get our button from the layout resource, // and attach an event to it button = FindViewById<Button>(Resource.Id.myButton); button2 = FindViewById<Button>(Resource.Id.myButton2); TextView text = new TextView(this); //text = FindViewById<TextView>(Resource.Id.Text); button.SetOnTouchListener(this); button2.SetOnTouchListener(this); } public bool OnTouch(View v, MotionEvent e) { switch (e.Action) { case MotionEventActions.Down: button.Text = string.Format("{0} clicks!", v.Id); if(cancellationTokenSource != null) break; cancellationTokenSource = new System.Threading.CancellationTokenSource(); var token = cancellationTokenSource.Token; System.Threading.Tasks.Task.Factory.StartNew(() => { var stopwatch = Stopwatch.StartNew(); while (stopwatch.ElapsedMilliseconds < 1000 && token.IsCancellationRequested == false) { System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(10)).Wait(); } while (token.IsCancellationRequested == false) { RunOnUiThread(() => { button.Text = string.Format("{0} clicks!", count++); }); System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(100)).Wait(); } }, token) .ContinueWith(_ => { cancellationTokenSource.Dispose(); cancellationTokenSource = null; }); break; case MotionEventActions.Up: if (cancellationTokenSource != null) { cancellationTokenSource.Cancel(); } break; } return true; } } }
現在、button1とbutton2を作りswitchでそれぞれのボタンの分岐処理を作ろうと考えておりますが、どのようにしてそれぞれのボタンの違いを認識させればよいかわからず詰まっています。
色々と試し、記載されているプログラムを実行し、button1を押すと「2131034112 CLICKS!」button2を押すと「2131034113 CLICKS!」とボタンに表示されるところまではできました
(本来であればTextViewなどに表示させた方がわかりやすいのですが、「text.Text = v.Id.ToString();」と書こうとすると何故かエラーが出てしまうのでボタンに表示させています)。
このIdなのですが、現在ボタンを押したときに得られるIdというのは不変なのでしょうか?
switchで分岐させる場合は
switch (v.Id) { case 2131034112: ............. break; } switch (v.Id) { case 2131034113: ............. break; }
こんな感じでいいのでしょうか?
サンプルコードとか色々見ましたが変数名とかが記載してあるのがほとんどだったと思うので気になり投稿しました。
###追記(12/15)
回答ありがとうございます。
教えていただいたコードを使用させていただきました。
しかし、通常のtouch動作であれば作成できましたが、
MotionEvent を検知できないようで、長押しができなくなってしまいました。
そのためonTouchから入れて試しましたが、どこに}を足してもエラーが出て詰まってしまいます。
Xamarin Studioのクイック修正を使用しエラーを潰しましたが、取りきれないエラーが数多くあり、動作確認ができない状況です。
###追記2(12/15)
コメントありがとうございます。
e.Event.Actionを使用することでe.Actionと同様の動きができました。
それにより、実現したい動作を作成することができました。ありがとうございました!
修正後の内容は以下のコードです。
using System; using System.Diagnostics; using Android.App; using Android.OS; using Android.Views; using Android.Widget; namespace test2 { [Activity(Label = "test2", MainLauncher = true, Icon = "@mipmap/icon")] public class MainActivity : Activity { System.Threading.CancellationTokenSource cancellationTokenSource; int count = 1; int count2 = 1; private Button button; private Button button2; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); // Get our button from the layout resource, // and attach an event to it button = FindViewById<Button>(Resource.Id.myButton); button2 = FindViewById<Button>(Resource.Id.myButton2); TextView text = new TextView(this); //text = FindViewById<TextView>(Resource.Id.Text); button.Touch += (object sender, View.TouchEventArgs e) => { switch (e.Event.Action) { case MotionEventActions.Down: button.Text = string.Format("{0} clicks!",count++); if (cancellationTokenSource != null) break; cancellationTokenSource = new System.Threading.CancellationTokenSource(); var token = cancellationTokenSource.Token; System.Threading.Tasks.Task.Factory.StartNew(() => { var stopwatch = Stopwatch.StartNew(); while (stopwatch.ElapsedMilliseconds < 1000 && token.IsCancellationRequested == false) { System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(10)).Wait(); } while (token.IsCancellationRequested == false) { RunOnUiThread(() => { button.Text = string.Format("{0} clicks!", count++); }); System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(100)).Wait(); } }, token) .ContinueWith(_ => { cancellationTokenSource.Dispose(); cancellationTokenSource = null; }); break; case MotionEventActions.Up: if (cancellationTokenSource != null) { cancellationTokenSource.Cancel(); } break; } }; button2.Touch += (object sender, View.TouchEventArgs e) => { switch (e.Event.Action) { case MotionEventActions.Down: button2.Text = string.Format("{0} clicks!",count2++); if (cancellationTokenSource != null) break; cancellationTokenSource = new System.Threading.CancellationTokenSource(); var token = cancellationTokenSource.Token; System.Threading.Tasks.Task.Factory.StartNew(() => { var stopwatch = Stopwatch.StartNew(); while (stopwatch.ElapsedMilliseconds < 1000 && token.IsCancellationRequested == false) { System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(10)).Wait(); } while (token.IsCancellationRequested == false) { RunOnUiThread(() => { button2.Text = string.Format("{0} clicks!", count2++); }); System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(100)).Wait(); } }, token) .ContinueWith(_ => { cancellationTokenSource.Dispose(); cancellationTokenSource = null; }); break; case MotionEventActions.Up: if (cancellationTokenSource != null) { cancellationTokenSource.Cancel(); } break; } }; } } }
※文字数制限に引っかかったので前回のコードは消しました
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/15 10:27
退会済みユーザー
2016/12/15 11:06
2016/12/15 15:08