单击按钮时不会调用 Android OnClickListener

标签 android button dialog listener

当用户单击通知项时,服务会触发 Activity (RluSvcMenu) 的 Intent 。然后,RluSvcMenu 成功地显示了一个带有三个选项按钮(复制/下一步;下一步;退出)的自定义对话框。一切都很好,只是当单击三个按钮中的任何一个时,似乎没有调用 Listener 函数。以下是 RluSvcMenu 代码的相关部分:

package com.lovelady.android.RluApp;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class RluSvcMenu extends Activity implements OnClickListener {
    private int action;
    private Button btnCopyNext;
private Button btnNext;
    private Button btnQuit;
private final int SVC_MENU_DIALOG = 1;
    private Dialog dialog;
    private final String MY_TAG = "RluSvcMenu";
    private final String MESSAGE1 = "onCreate message";
    private final String MESSAGE2 = "onPrepare message";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

  showDialog(SVC_MENU_DIALOG);
  Log.d(MY_TAG, "showDialog() has completed.");
}


    protected Dialog onCreateDialog(int id) {
        dialog = new Dialog(this);

        dialog.setContentView(R.layout.rlu_service_menu);

        dialog.setTitle("RLU_APP");

        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText(MESSAGE1);

        ImageView image = (ImageView) dialog.findViewById(R.id.icon);
    image.setImageResource(R.drawable.ic_rlu);

    btnCopyNext = (Button) dialog.findViewById(R.id.BtnCopyNext);
    btnCopyNext.setOnClickListener(this);

        btnNext = (Button) dialog.findViewById(R.id.BtnNext);
    btnNext.setOnClickListener(this);

    btnQuit = (Button) dialog.findViewById(R.id.BtnQuit);
    btnQuit.setOnClickListener(this);
    return dialog;
}

public void onClick(View v) {
    action = v.getId() ;
    Toast.makeText(getApplicationContext()
            , "Button " + action + " clicked."
            , Toast.LENGTH_SHORT).show();
    Log.d(MY_TAG, "Button " + action + " clicked.");
    dialog.dismiss();
}


protected void onPrepareDialog(int id, Dialog dialog) {

    dialog.setContentView(R.layout.rlu_service_menu);

    TextView text = (TextView) dialog.findViewById(R.id.text);
    text.setText(MESSAGE2);
    ImageView image = (ImageView) dialog.findViewById(R.id.icon);
    image.setImageResource(R.drawable.ic_rlu_app);
}
}

构建“服务菜单”的 XML 是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/layout_root"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:padding="4dp"
          >
<ImageView android:id="@+id/icon"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginRight="10dp"
           />
<TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_toRightOf="@+id/icon"
          android:layout_height="wrap_content"
          android:layout_marginBottom="20dp"
          android:textColor="#FFF"
          />
<Button android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_toRightOf="@+id/icon"
        android:layout_width="wrap_content"
        android:id="@+id/BtnCopyNext"
        android:text="Copy/Next"></Button>
<Button android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/BtnCopyNext"
        android:layout_below="@+id/text"
        android:layout_width="wrap_content"
        android:id="@+id/BtnNext"
        android:text="Next"></Button>
<Button android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/BtnNext"
        android:layout_below="@+id/text"
        android:layout_width="wrap_content"
        android:id="@+id/BtnQuit"
        android:text="Quit"></Button>
</RelativeLayout>

...最后,AndroidManifest.xml 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.lovelady.android.AppName"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

    <application android:label="@string/app_name" 
                 android:icon="@drawable/ic_mainIcon">
        <activity android:name=".RluSvcMenu"></activity>
        <activity android:name=".RluOverview"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <!-- enable the search dialog to send searches to RluSearch -->
            <meta-data android:name="android.app.default_searchable"
                       android:value=".RluSearch" />
        </activity>
        <activity android:name=".RluDetails"></activity>
        <activity android:name=".RluImportFile"></activity>
        <activity android:name=".RluImportFilteredFile"></activity>
        <activity android:name=".RluSearch">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <meta-data android:name="android.app.searchable"
                       android:resource="@xml/searchable" />
        </activity>
        <service android:name=".RluManager"
                 android:exported="false"
                 android:label="@string/service_name">
        </service>
    </application>
</manifest>

什么可能导致触摸不被传送到 Activity ?他们会去服务吗?如果是这样,我该怎么做才能集中注意力? (我以为我会通过设置对话框来获得焦点,但我从 logcat 得到的唯一线索表明:

07-09 21:04:20.060 I/InputDispatcher(  302): Delivering touch to current input \
                   target: action: 0, channel '409079b8 Rluapp (server)'

(其中“Rluapp”是@string/my_app 的值,以及包的名称)

...或者我忘记填充我的 Listener 的什么漏洞?就所有 Intent 和目的而言,按下按钮时“什么都没有”发生。

最佳答案

嗯,我怀疑对话框在不同的线程中运行?无论如何,如果您使用一个警报对话框,他们有 DialogInterface.onClickListener 类来处理该问题,可能有更好的方法来解决这个问题,但是将代码更改为和实际的自定义对话框可以解决这个问题。

这是使用该修复程序修改的原始代码,无论如何这可能会更好,因为您可以将对话框放在它自己的文件/类等中。

public class BtnNoWorky extends Activity {
    private int action;
    private Button btnCopyNext;
    private Button btnNext;
    private Button btnQuit;
    private final int SVC_MENU_DIALOG = 1;
    private final String MY_LABEL = "BtnNoWorky";
    private final String MESSAGE1 = "Msg from onCreateDialog";
    private final String MESSAGE2 = "Msg from onPrepare";
    private myDialog D = null; 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(MY_LABEL, "onCreate()");
        super.onCreate(savedInstanceState);

        Log.d(MY_LABEL, "Show dialog") ;
        //showDialog(SVC_MENU_DIALOG);
        D = new myDialog(this);
        D.show();

        Log.d(MY_LABEL, "showDialog() has completed.");
    }

    public class myDialog extends Dialog implements OnClickListener {

    public myDialog(Context context) {
            super(context,0);
    }

    public myDialog(Context context, int theme) {
            super(context, theme);
    }

    protected myDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
            super(context, cancelable, cancelListener);
            // TODO Auto-generated constructor stub
        }

    public void onCreate(Bundle saved) {
        //Log.d(MY_LABEL, "onCreateDialog - Creating dialog");

        //Log.d(MY_LABEL, "onCreateDialog - setContentView");
        setContentView(R.layout.bnw_dialog);

        //Log.d(MY_LABEL, "onCreateDialog - setTitle");
       setTitle("BtnNoWorky");

        //Log.d(MY_LABEL, "onCreateDialog - setText");
        TextView text = (TextView) findViewById(R.id.text);
        text.setText(MESSAGE1);

        //Log.d(MY_LABEL, "onCreateDialog - setImageResource");
        ImageView image = (ImageView) findViewById(R.id.icon);
        image.setImageResource(R.drawable.icon);

        //Log.d(MY_LABEL, "onCreateDialog - Instantiate btnCopyNext");
        btnCopyNext = (Button)findViewById(R.id.BtnCopyNext);
        btnCopyNext.setOnClickListener(this);

        //Log.d(MY_LABEL, "onCreateDialog - Instantiate btnNext");
        btnNext = (Button) findViewById(R.id.BtnNext);
        btnNext.setOnClickListener(this);

        //Log.d(MY_LABEL, "onCreateDialog - Instantiate btnQuit");
        btnQuit = (Button)findViewById(R.id.BtnQuit);
        btnQuit.setOnClickListener(this);

        //Log.d(MY_LABEL, "onCreateDialog - Returning...");


    }

    @Override
    public void onClick(View v) {
        action = v.getId() ;
        Toast.makeText(getApplicationContext()
                , "Button " + action + " clicked."
                , Toast.LENGTH_SHORT).show();
        Log.d(MY_LABEL, "Button " + action + " clicked.");
        dismiss();
    }
    }

}

关于单击按钮时不会调用 Android OnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6638559/

相关文章:

android - Crashlytics Android SDK - 自定义 UncaughtExceptionHandler

android - Activity 如何与 Android 中的 ListFragment 通信?

react-native - 在 React Native 中向 onPress 函数添加多个事件

c++ - DialogBoxIndirect 创建的对话框比要求的大

android - 在 webview 中添加进度对话框

android - 安装失败并显示消息无法提交安装 session

android - 如何自动获取检测到的条形码/二维码

jquery - 需要可扩展的多个开/关按钮

android - android音乐播放器项目中的前进、后退按钮

android - 当前时间选择器