将 onclick 分配给主 activity 中的 PopupWindow 内容
Assigning onclick to PopupWindow contents in the main activity
我正在尝试实现流播放器。除了显示的 activity_main
布局之外,还有一个弹出式布局。但是,如果我尝试单击弹出窗口中的 ImageView
,则不会触发单击事件。
下面是我的弹出 window 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#000000"
android:orientation="vertical"
android:padding="10sp" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="POPUP"
android:textColor="#be2525"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:layout_marginTop="9dp" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:background="#d2f4d4"
android:layout_marginLeft="10dp"
android:text="Close"
android:layout_gravity="right"
android:layout_marginRight="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/button_stream_one"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="5dp"
android:src="@drawable/logo_deutsches_musik_radio"/>
<ImageView
android:id="@+id/button_stream_two"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="5dp"
android:src="@drawable/logo_deutsches_musik_radio"/>
</LinearLayout>
</LinearLayout>
我的主要布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.amplitude.tron.popupwindows.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageButton
android:id="@+id/playerControl"
android:layout_width="75dp"
android:layout_height="75dp"
android:scaleType="centerCrop"
android:background="@null"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"/>
<Button
android:id="@+id/btn_create_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="POPUP" />
</LinearLayout>
</RelativeLayout>
这就是我在弹出窗口中处理点击操作的方式 window
public class MainActivity extends AppCompatActivity {
Button btnClosePopup;
Button btnCreatePopup;
ImageButton playControlButton;
MediaPlayer mediaPlayer = new MediaPlayer();
private PopupWindow pwindo;
ImageView streamOne,streamTwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// myMediaPlayer = new MediaPlayer();
btnCreatePopup = (Button) findViewById(R.id.btn_create_popup);
btnCreatePopup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initiatePopupWindow();
}
});
playControlButton = (ImageButton) findViewById(R.id.playerControl);
if (!mediaPlayer.isPlaying())
{
playControlButton.setImageResource(R.drawable.button_play);
}
else
{
playControlButton.setImageResource(R.drawable.button_stop);
}
playControlButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mediaPlayer.isPlaying()){
playControlButton.setImageResource(R.drawable.button_play);
playStream("http://136.243.133.81:8000/live");
}
else
{
playControlButton.setImageResource(R.drawable.button_play);
mediaPlayer.stop();
mediaPlayer.reset();
}
}
});
}
public void playStream(String url)
{
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
playControlButton = (ImageButton) findViewById(R.id.playerControl);
playControlButton.setImageResource(R.drawable.button_stop);
mp.start();
}
});
}
//POPUP WINDOW
private void initiatePopupWindow() {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int w = dm.widthPixels;
int h = dm.heightPixels;
int newWidth = w-50;
int newHeight=h-300;
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_window, (ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout,newWidth, newHeight, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
pwindo.setFocusable(true);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
streamOne = (ImageView) layout.findViewById(R.id.button_stream_two);
streamOne.setOnClickListener(availableStreams);
streamOne = (ImageView) layout.findViewById(R.id.button_stream_two);
streamTwo.setOnClickListener(availableStreams);
} catch (Exception e) {
e.printStackTrace();
}
}
private View.OnClickListener cancel_button_click_listener = new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
};
private View.OnClickListener availableStreams = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_stream_one:
mediaPlayer.reset();
playStream("http://136.243.133.81:8000/live");
break;
case R.id.button_stream_two:
mediaPlayer.reset();
playStream("http://mp3channels.webradio.antenne.de/top-40.aac");
break;
}
}
};
}
奇怪的是,我认为默认情况下 PopupWindow 不可聚焦。
pwindo.setFocusable(true);
应该允许您的按钮工作
您没有为 button_stream_one
和 button_stream_two
设置 onClick
操作。
// Declare these private variables
private ImageView btnStreamOne;
private ImageView btnStreamTwo;
然后在你的popup初始化函数中。
btnStreamOne = (ImageView) layout.findViewById(R.id.button_stream_one);
btnStreamOne.setOnClickListener(streamOneClick);
btnStreamTwo = (ImageView) layout.findViewById(R.id.button_stream_two);
btnStreamTwo.setOnClickListener(streamTwoClick);
并且您需要在布局中设置 ImageView
可点击。
<ImageView
android:id="@+id/button_stream_one"
android:layout_width="100dp"
android:layout_height="100dp"
android:clickable="true"
android:layout_marginLeft="5dp"
android:src="@drawable/logo_deutsches_musik_radio"/>
<ImageView
android:id="@+id/button_stream_two"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="5dp"
android:clickable="true"
android:src="@drawable/logo_deutsches_musik_radio"/>
更新
尝试为这两个 ImageView
写两个不同的 onClickListener
。
private View.OnClickListener streamOneClick = new View.OnClickListener() {
public void onClick(View v) {
mediaPlayer.reset();
playStream("http://136.243.133.81:8000/live");
}
};
private View.OnClickListener streamTwoClick = new View.OnClickListener() {
public void onClick(View v) {
mediaPlayer.reset();
playStream("http://mp3channels.webradio.antenne.de/top-40.aac");
}
};
更新 2
我克隆了您的项目,发现您为 ImageView
引用了错误的视图。我无法将代码更改推送到您的 Github 存储库,因为我没有权限。但无论如何,我将更改的部分粘贴在这里。尝试替换代码中的以下函数。
//POPUP WINDOW
private void initiatePopupWindow() {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int w = dm.widthPixels;
int h = dm.heightPixels;
int newWidth = w - 50;
int newHeight = h - 300;
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_window, (ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, newWidth, newHeight, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
pwindo.setFocusable(true);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
streamOne = (ImageView) layout.findViewById(R.id.button_stream_one);
streamOne.setOnClickListener(streamOneClick);
streamTwo = (ImageView) layout.findViewById(R.id.button_stream_two);
streamTwo.setOnClickListener(streamTwoClick);
} catch (Exception e) {
e.printStackTrace();
}
}
private View.OnClickListener cancel_button_click_listener = new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
Toast.makeText(MainActivity.this, "Cancel button clicked", Toast.LENGTH_LONG).show();
}
};
private View.OnClickListener streamOneClick = new View.OnClickListener() {
public void onClick(View v) {
mediaPlayer.reset();
playStream("http://136.243.133.81:8000/live");
Toast.makeText(MainActivity.this, "Stream one button clicked", Toast.LENGTH_LONG).show();
pwindo.dismiss();
}
};
private View.OnClickListener streamTwoClick = new View.OnClickListener() {
public void onClick(View v) {
mediaPlayer.reset();
playStream("http://mp3channels.webradio.antenne.de/top-40.aac");
Toast.makeText(MainActivity.this, "Stream two button clicked", Toast.LENGTH_LONG).show();
pwindo.dismiss();
}
};
解决了。由于 OOM exception.The 错误未在 Logcat 中显示,因此图像视图未被初始化,但是在应用程序的设计级别我收到了异常警报,因此在打开详细信息后我得到了以下 OOM。
在 android.widget.ImageView.
为此,我在清单中添加了以下内容:
android:hardwareAccelerated="true"
android:largeHeap="true"
可能解决导致 OOM 异常的图像
我正在尝试实现流播放器。除了显示的 activity_main
布局之外,还有一个弹出式布局。但是,如果我尝试单击弹出窗口中的 ImageView
,则不会触发单击事件。
下面是我的弹出 window 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#000000"
android:orientation="vertical"
android:padding="10sp" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="POPUP"
android:textColor="#be2525"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:layout_marginTop="9dp" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:background="#d2f4d4"
android:layout_marginLeft="10dp"
android:text="Close"
android:layout_gravity="right"
android:layout_marginRight="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/button_stream_one"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="5dp"
android:src="@drawable/logo_deutsches_musik_radio"/>
<ImageView
android:id="@+id/button_stream_two"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="5dp"
android:src="@drawable/logo_deutsches_musik_radio"/>
</LinearLayout>
</LinearLayout>
我的主要布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.amplitude.tron.popupwindows.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageButton
android:id="@+id/playerControl"
android:layout_width="75dp"
android:layout_height="75dp"
android:scaleType="centerCrop"
android:background="@null"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"/>
<Button
android:id="@+id/btn_create_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="POPUP" />
</LinearLayout>
</RelativeLayout>
这就是我在弹出窗口中处理点击操作的方式 window
public class MainActivity extends AppCompatActivity {
Button btnClosePopup;
Button btnCreatePopup;
ImageButton playControlButton;
MediaPlayer mediaPlayer = new MediaPlayer();
private PopupWindow pwindo;
ImageView streamOne,streamTwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// myMediaPlayer = new MediaPlayer();
btnCreatePopup = (Button) findViewById(R.id.btn_create_popup);
btnCreatePopup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initiatePopupWindow();
}
});
playControlButton = (ImageButton) findViewById(R.id.playerControl);
if (!mediaPlayer.isPlaying())
{
playControlButton.setImageResource(R.drawable.button_play);
}
else
{
playControlButton.setImageResource(R.drawable.button_stop);
}
playControlButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mediaPlayer.isPlaying()){
playControlButton.setImageResource(R.drawable.button_play);
playStream("http://136.243.133.81:8000/live");
}
else
{
playControlButton.setImageResource(R.drawable.button_play);
mediaPlayer.stop();
mediaPlayer.reset();
}
}
});
}
public void playStream(String url)
{
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
playControlButton = (ImageButton) findViewById(R.id.playerControl);
playControlButton.setImageResource(R.drawable.button_stop);
mp.start();
}
});
}
//POPUP WINDOW
private void initiatePopupWindow() {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int w = dm.widthPixels;
int h = dm.heightPixels;
int newWidth = w-50;
int newHeight=h-300;
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_window, (ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout,newWidth, newHeight, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
pwindo.setFocusable(true);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
streamOne = (ImageView) layout.findViewById(R.id.button_stream_two);
streamOne.setOnClickListener(availableStreams);
streamOne = (ImageView) layout.findViewById(R.id.button_stream_two);
streamTwo.setOnClickListener(availableStreams);
} catch (Exception e) {
e.printStackTrace();
}
}
private View.OnClickListener cancel_button_click_listener = new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
};
private View.OnClickListener availableStreams = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_stream_one:
mediaPlayer.reset();
playStream("http://136.243.133.81:8000/live");
break;
case R.id.button_stream_two:
mediaPlayer.reset();
playStream("http://mp3channels.webradio.antenne.de/top-40.aac");
break;
}
}
};
}
奇怪的是,我认为默认情况下 PopupWindow 不可聚焦。
pwindo.setFocusable(true);
应该允许您的按钮工作
您没有为 button_stream_one
和 button_stream_two
设置 onClick
操作。
// Declare these private variables
private ImageView btnStreamOne;
private ImageView btnStreamTwo;
然后在你的popup初始化函数中。
btnStreamOne = (ImageView) layout.findViewById(R.id.button_stream_one);
btnStreamOne.setOnClickListener(streamOneClick);
btnStreamTwo = (ImageView) layout.findViewById(R.id.button_stream_two);
btnStreamTwo.setOnClickListener(streamTwoClick);
并且您需要在布局中设置 ImageView
可点击。
<ImageView
android:id="@+id/button_stream_one"
android:layout_width="100dp"
android:layout_height="100dp"
android:clickable="true"
android:layout_marginLeft="5dp"
android:src="@drawable/logo_deutsches_musik_radio"/>
<ImageView
android:id="@+id/button_stream_two"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="5dp"
android:clickable="true"
android:src="@drawable/logo_deutsches_musik_radio"/>
更新
尝试为这两个 ImageView
写两个不同的 onClickListener
。
private View.OnClickListener streamOneClick = new View.OnClickListener() {
public void onClick(View v) {
mediaPlayer.reset();
playStream("http://136.243.133.81:8000/live");
}
};
private View.OnClickListener streamTwoClick = new View.OnClickListener() {
public void onClick(View v) {
mediaPlayer.reset();
playStream("http://mp3channels.webradio.antenne.de/top-40.aac");
}
};
更新 2
我克隆了您的项目,发现您为 ImageView
引用了错误的视图。我无法将代码更改推送到您的 Github 存储库,因为我没有权限。但无论如何,我将更改的部分粘贴在这里。尝试替换代码中的以下函数。
//POPUP WINDOW
private void initiatePopupWindow() {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int w = dm.widthPixels;
int h = dm.heightPixels;
int newWidth = w - 50;
int newHeight = h - 300;
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_window, (ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, newWidth, newHeight, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
pwindo.setFocusable(true);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
streamOne = (ImageView) layout.findViewById(R.id.button_stream_one);
streamOne.setOnClickListener(streamOneClick);
streamTwo = (ImageView) layout.findViewById(R.id.button_stream_two);
streamTwo.setOnClickListener(streamTwoClick);
} catch (Exception e) {
e.printStackTrace();
}
}
private View.OnClickListener cancel_button_click_listener = new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
Toast.makeText(MainActivity.this, "Cancel button clicked", Toast.LENGTH_LONG).show();
}
};
private View.OnClickListener streamOneClick = new View.OnClickListener() {
public void onClick(View v) {
mediaPlayer.reset();
playStream("http://136.243.133.81:8000/live");
Toast.makeText(MainActivity.this, "Stream one button clicked", Toast.LENGTH_LONG).show();
pwindo.dismiss();
}
};
private View.OnClickListener streamTwoClick = new View.OnClickListener() {
public void onClick(View v) {
mediaPlayer.reset();
playStream("http://mp3channels.webradio.antenne.de/top-40.aac");
Toast.makeText(MainActivity.this, "Stream two button clicked", Toast.LENGTH_LONG).show();
pwindo.dismiss();
}
};
解决了。由于 OOM exception.The 错误未在 Logcat 中显示,因此图像视图未被初始化,但是在应用程序的设计级别我收到了异常警报,因此在打开详细信息后我得到了以下 OOM。 在 android.widget.ImageView.
为此,我在清单中添加了以下内容:
android:hardwareAccelerated="true"
android:largeHeap="true"
可能解决导致 OOM 异常的图像