【问题标题】:onclick event in dialog preference对话框首选项中的 onclick 事件
【发布时间】:2014-01-26 15:11:40
【问题描述】:

我有一个自定义对话框首选项,其中包含一堆按钮。每个按钮代表一个数字。当您想设置闹钟时,类似于时钟应用程序。如何在我的对话框首选项中获得每个按钮的 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);
}

}

【问题讨论】:

  • 您是想通过点击的数字来设置闹钟还是时间?能不能说的清楚一点。
  • 是的,我想要这样的东西

标签: android button onclick dialog-preference


【解决方案1】:

如果我正确理解您的问题,您可以为每个按钮设置标签并为所有按钮设置一个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() 来确定单击了哪个按钮

【讨论】:

  • 是崩溃还是其他问题?
  • 用代码和 xml 更新您的帖子,请问您如何尝试我的解决方案
  • 您正在使用 == 比较字符串,请改用 equals:if(tag.equals("1")){
【解决方案2】:

就我理解你的问题而言。您希望在单击的每个按钮上调用一个方法。 为此,将其添加到 xml 中的每个按钮元素中:

<Button 
    android:onClick="OnClick"
    ..                       />

将此方法添加到您要执行任务的 .java 文件中:

public void OnClick(View v){
    // do something....
}

或者你可以直接在按钮对象处调用setOnClickListener

button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // do something....
        }
});

【讨论】:

  • 我尝试了这种方法,但它不起作用,我不知道为什么......可能是因为这是对话框首选项而不是活动
  • 可能,但可能不起作用,因为系统会将 OnClick 方法搜索到活动中。看看这个:stackoverflow.com/questions/4243704/…
猜你喜欢
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多