我正在经历一个非常奇怪的问题,即Button没有响应点击事件。我有一个显示的对话框,然后当用户点击对话框中的肯定按钮时,它会打开一个包含视频,文本框和按钮的布局。每当我点击布局上的按钮时,它都没有响应!请帮忙。
布局文件:
<?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="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/roundedcorners"
android:orientation="vertical"
android:padding="0dp" >
<LinearLayout
android:id="@+id/video_panel"
android:layout_width="match_parent"
android:layout_height="175dp"
android:layout_above="@+id/bottom_panel"
android:layout_alignParentTop="true"
android:layout_gravity="top"
android:gravity="top"
android:orientation="vertical" >
</LinearLayout>
<RelativeLayout
android:id="@+id/bottom_panel"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_gravity="bottom"
android:background="@color/milky_white"
android:gravity="bottom"
android:orientation="vertical" >
<TextView
android:id="@+id/agentname"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="@color/video_username"
android:layout_alignParentTop="true"
android:textStyle="bold"
android:text="name"/>
<Button
android:id="@+id/disconn"
android:layout_width="510dp"
android:layout_height="37dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:clickable="true"
android:text="@string/disconnect" />
</RelativeLayout>
</RelativeLayout>
onClick的代码如下:
alertVideoChatBuilder.setMessage(message);
alertVideoChatBuilder.setPositiveButton("Answer", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StatusJson sj = new StatusJson("User accepted.",1);
outCmd.sendJsonCommand(getNodeId(), "videoChatStatus", sj);
handler.post(new Runnable() {
@Override
public void run() {
try {
pc = pcf.createPeerConnection(iceServers, pcConstraints, new PCObserver(nodeId));
} catch (UnsupportedOnThisDeviceException e) {
Log.e("MeshAgent", "Video chat not supported");
return;
}
MediaConstraints audio = new MediaConstraints();
audio.mandatory.add(new KeyValuePair("googEchoCancellation", "true"));
audio.mandatory.add(new KeyValuePair("googAutoGainControl", "true"));
//audio.mandatory.add(new KeyValuePair("noiseSuppression", "false"));
MediaStream ms = pcf.createLocalMediaStream("device");
AudioSource as = pcf.createAudioSource(audio);
AudioTrack at = pcf.createAudioTrack("devicea0",as);
ms.addTrack(at);
pc.addStream(ms, audio);
videoView = new VideoStreamsView(context, R.drawable.tech_mute, R.drawable.tech_connect);
LayoutInflater li = LayoutInflater.from(context);
outerVideoView = (RelativeLayout) li.inflate(R.layout.chat_float, null);
LinearLayout vView = (LinearLayout) outerVideoView.findViewById(R.id.video_panel);
TextView agent_name = (TextView) outerVideoView.findViewById(R.id.agentname);
agent_name.setText(name);
//RelativeLayout bottomView = (RelativeLayout) outerVideoView.findViewById(R.id.bottom_panel);
Button discon_but = (Button) outerVideoView.findViewById(R.id.disconn);
discon_but.setClickable(true);
discon_but.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//System.out.println("mesh: button clicked disconnect!!");
Toast.makeText(context, "Video Connection disconnected", Toast.LENGTH_LONG).show();
outCmd.sendJsonCommand(getNodeId(), "videoChatStatus", new StatusJson("User disconnected the call.",1));
coord.disconnect();
}
});
vView.addView(videoView);
wm.addView(outerVideoView, params);
答案 0 :(得分:0)
请检查您的代码,在正面按钮的onclick方法中,您正在为对话框中未设置的新视图进行充气。它不会参考对话框上的任何视图。您可以使用在对话框中设置的相同视图来设置侦听器。对话框上的视图集与您在其上设置onclick侦听器的视图不同。我认为问题是,你是在onClick of Positive按钮内设置onClick监听器。因此,除非单击对话框的“正向”按钮,否则不会设置断开连接按钮上的单击侦听器。 尝试在onClick正面按钮之外的断开按钮上设置onclick监听器。如下面的示例代码所示。希望这有助于!
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("test title");
builder.setMessage("test message");
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.test_layout, null);
builder.setView(view);
Button testButton = (Button) view.findViewById(R.id.test_button);
testButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(context, "Hello", Toast.LENGTH_SHORT).show();
};
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Logic on negative button click
}
});
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// logic on positive button click
}
});
AlertDialog dialog = builder.create();
dialog.show();
如何在点击肯定按钮时设置onclick侦听器:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("test title");
builder.setMessage("test message");
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// This is the view we are setting on dialog. You can use only this view
// to access view present on dialog. Using this reference, we can set
// onclick listener in positive button's onclick event
final View view = inflater.inflate(R.layout.test_layout, null);
builder.setView(view);
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Logic on negative button click
}
});
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// logic on positive button click
// Set onclick listener whenever positive button is clicked.
Button testButton = (Button) view
.findViewById(R.id.test_button);
testButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(context, "Hello", Toast.LENGTH_SHORT)
.show();
};
});
}
});
AlertDialog dialog = builder.create();
dialog.show();
答案 1 :(得分:0)
videoView = new VideoStreamsView(context, R.drawable.tech_mute, R.drawable.tech_connect);
LayoutInflater li = LayoutInflater.from(context);
outerVideoView = (RelativeLayout) li.inflate(R.layout.chat_float, null);
LinearLayout vView = (LinearLayout) outerVideoView.findViewById(R.id.video_panel);
TextView agent_name = (TextView) outerVideoView.findViewById(R.id.agentname);
agent_name.setText(name);
//RelativeLayout bottomView = (RelativeLayout) outerVideoView.findViewById(R.id.bottom_panel);
Button discon_but = (Button) outerVideoView.findViewById(R.id.disconn);
discon_but.setClickable(true);
discon_but.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//System.out.println("mesh: button clicked disconnect!!");
Toast.makeText(context, "Video Connection disconnected", Toast.LENGTH_LONG).show();
outCmd.sendJsonCommand(getNodeId(), "videoChatStatus", new StatusJson("User disconnected the call.",1));
coord.disconnect();
}
});
所有这些代码都应该在UI线程上运行,您不能将此代码放在run()
中。相反,找到一种方法来使用runOnUiThread()
答案 2 :(得分:0)
最后看起来问题已修复。对不起,我没有发布完整的代码。我将WindowManager参数从TYPE_SYSTEM_OVERLAY更改为TYPE_PHONE并且它有效。 看起来TYPE_SYSTEM_OVERLAY阻止了由于安全问题而收到的任何触摸事件。
谢谢, 乌代