下記XMLのようにボタンがいくつかあり、
C#で1つ目のボタンを押した時、コンソールに出力するようにしています。
(Resource.Id.button1)のところを、for文でbutton1、button2、button3...という風に値を増やしていけば、簡潔に記述できるのではと考えていますが、実際そんな事できるのでしょうか?
xml
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 <Button 7 android:id="@+id/button1" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:text="Play01" /> 11 <Button 12 android:id="@+id/button2" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:text="Play02" /> 16 <Button 17 android:id="@+id/button3" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:text="Play03" /> 21</LinearLayout>
C#
1//ボタンをクリックした時のイベント処理 2FindViewById<Button>(Resource.Id.button1).Click += (sender, e) => { 3 4 System.Console.WriteLine("ボタン1です"); 5 6};
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/27 10:07