【问题标题】:Assigning onclick to PopupWindow contents in the main activity将 onclick 分配给主活动中的 PopupWindow 内容
【发布时间】:2017-01-16 16:04:02
【问题描述】:

我正在尝试实现一个流播放器。除了显示的activity_main 布局之外,还有一个弹出布局。但是,如果我尝试单击弹出窗口内的ImageView,则不会触发单击事件。

下面是我的弹出窗口布局

<?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>

这就是我在弹出窗口中处理点击操作的方式

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;
            }
        }
    };
}

【问题讨论】:

    标签: android android-layout onclicklistener popupwindow


    【解决方案1】:

    奇怪的是,我认为 PopupWindow 默认情况下是不可聚焦的。

    pwindo.setFocusable(true);
    

    应该让你的按钮工作

    【讨论】:

    • 上述解决方案没有发生任何事情,调试器永远不会进入onclick事件:|
    【解决方案2】:

    解决了。由于 OOM 异常,imageview 没有被初始化。Logcat 中没有显示错误,但是在应用程序的设计级别,我收到了异常警报,因此在打开详细信息后,我得到了以下 OOM。 在 android.widget.ImageView.

    为此,我在清单中添加了以下内容:

    • android:hardwareAccelerated="true"
    • android:largeHeap="true"

    可能解决导致 OOM 异常的图像

    【讨论】:

      【解决方案3】:

      您没有为button_stream_onebutton_stream_two 设置onClick 操作。

      // Declare these private variables
      private ImageView btnStreamOne;
      private ImageView btnStreamTwo;
      

      然后在你的弹出初始化函数中。

      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();
          }
      };
      

      【讨论】:

      • same result 什么也没发生,实际上我已经按照您的建议初始化了 imageView,但是我撤消了我的代码,这就是为什么它最终出现在我在上面问题中附加的代码中。反正我已经更新了上面的代码!
      • 你让它们也可以点击了吗?
      • 是的,这让我很开心,关闭按钮有效,但 imageView 无效:D
      • 您也尝试写两个不同的onClickListener,就像更新后的答案一样?如果它也不起作用,请在单击关闭按钮上添加一个 toast,以查看关闭按钮是否实际单击。
      • 我已将以上内容添加到 git 中,如果您有疑问可以查看 repo
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 2013-10-28
      • 2010-11-24
      • 2023-03-21
      • 1970-01-01
      相关资源
      最近更新 更多