当用户单击“通知”项时,将从服务触发活动(RluSvcMenu)的意图。然后,RluSvcMenu成功显示一个带有三个选项按钮的自定义对话框(复制/下一步;下一步;退出)。一切都很好,除了当点击三个按钮中的任何一个时,似乎没有调用监听器功能。以下是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>
什么可能导致触摸无法传递给活动?他们会去服务吗?如果是这样,我该怎么做才能获得焦点? (我以为我会通过建立对话来获得焦点,但我唯一的线索来自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的值,以及包的名称)
......或者我忘了填写听众的洞?对于所有意图和目的,按下按钮时“没有”发生。
答案 0 :(得分: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();
}
}
}