0

ボタンの束を持つカスタム ダイアログ設定があります。各ボタンは、アラームを設定するときの時計アプリケーションのような数字を表します。ダイアログ設定で各ボタンの onClick を取得するにはどうすればよいですか? これは私のダイアログ レイアウト (time_picker) の一部です:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<LinearLayout 
    android:id="@+id/showTimer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="_ _ : _ _"
        android:textSize="20dp"
        android:textStyle="bold" />

</LinearLayout>

<RelativeLayout
    android:id="@+id/Buttons" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_below="@+id/showTimer"
    android:layout_marginTop="10dp">

    <Button
    android:id="@+id/button1"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button5"
    android:layout_alignBottom="@+id/button5"
    android:layout_alignLeft="@+id/button3"
    android:layout_centerInParent="true"
    android:text="6"
    android:textStyle="bold"
    android:textSize="20dp"
    android:tag="1"
    android:background="@android:color/transparent" />

<Button
    android:id="@+id/button2"
    android:layout_width="100dp"
    ...

これは、ダイアログ設定を起動する設定レイアウトです。

<com.example.test.TimerForNoti 
    android:key = "pref_sync_noti_timer"
    android:title = "@string/pref_sync_noti_timer"
    android:summary = "@string/pref_sync_noti_timer_summ"
    android:dialogMessage = "@string/pref_sync_noti_timer">
</com.example.test.TimerForNoti>

これはダイアログ設定のクラスです (TimerForNoti):

public class TimerForNoti extends DialogPreference 
{

public TimerForNoti(Context context, AttributeSet attrs) {
    super(context, attrs);

    setDialogLayoutResource(R.layout.time_picker);
    setPositiveButtonText(R.string.ok);
    setNegativeButtonText(R.string.cancel);

    Dialog di = new Dialog(context);
    di.setContentView(R.layout.time_picker);

    OnClickListener test = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String tag = v.getTag().toString();
            if(tag == "1"){
                Log.v("onOne", "ok");
            }
                            // .....

        }
    };

    Button btn1 = (Button) di.findViewById(R.id.button1);
    btn1.setOnClickListener(test);
}

}

4

2 に答える 2

0

あなたの質問が正しく理解できれば、各ボタンにタグを設定OnClickListenerし、すべてのボタンにタグを設定できます。

<Button
    android:id="@+id/button1"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button5"
    android:layout_alignBottom="@+id/button5"
    android:layout_alignLeft="@+id/button3"
    android:layout_centerInParent="true"
    android:text="6"
    android:textStyle="bold"
    android:textSize="20dp"
    android:tag="6"
    android:background="@android:color/transparent" />

コード:

        OnClickListener buttonsListener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                String buttonText = v.getTag().toString();
                // your logic here
            }
        };

        Button btn1 = (Button)findViewById(R.id.button1);
        btn1.setOnClickListener(buttonsListener);

        Button btn2 = (Button)findViewById(R.id.button2);
        btn2.setOnClickListener(buttonsListener);

        Button btn3 = (Button)findViewById(R.id.button3);
        btn3.setOnClickListener(buttonsListener);

        // your buttons here

OnClickListenerまた、どのボタンがクリックされたかを判断するためにv.getId() を使用することもできます

于 2014-01-26T15:32:02.923 に答える