【问题标题】:Android OnClickListner is not called on Button clickAndroid OnClickListener 不会在按钮单击时调用
【发布时间】:2011-10-02 02:04:19
【问题描述】:

当用户单击通知项时,服务会触发 Activity (RluSvcMenu) 的意图。然后,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 中填充什么漏洞?出于所有意图和目的,按下按钮时“什么都没有”发生。

【问题讨论】:

    标签: android button dialog listener


    【解决方案1】:

    嗯,我怀疑对话框在不同的线程中运行?无论如何,如果您使用一个警报对话框,他们有 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();
        }
        }
    
    }
    

    【讨论】:

    • 很好,@Idistic,但显然这不是唯一的问题。我非常感谢您的回应,现在我相信我更近了一步。谢谢。
    • 因此,为了确定这是服务问题还是其他问题,我将问题 Activity 分解为一个超简单的独立应用程序,它遭受了同样的命运。但现在我可以分享...完整的 Eclipse 项目位于link,供那些想亲眼看看的人使用。我非常感谢任何帮助,因为我没有看到原因。
    • @Dennis 错过了这是一个对话。将其更改为一个类,它应该可以将您的代码添加回上面的原始答案,它会遇到断点
    • 简洁中的优雅!非常感谢,感谢我,感谢有头发和发红迹象的墙壁。 :)
    • @Dennis 没问题,也许对我的答案投赞成票并接受它?顺便说一句,头上的视觉效果很好
    猜你喜欢
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多