Android PopupWindow和SeekBar监听器没有响应

时间:2016-05-11 15:53:59

标签: java android android-layout android-studio android-popupwindow

有谁知道为什么我的搜索栏监听器没有从搜索栏中获取任何数据?我有所有设置和工作,但它没有更新textviews,我认为它与弹出窗口有关,使用不同的活动布局到它所在的活动

public class Environment extends Activity implements OnSeekBarChangeListener
{

private ImageButton lockButton;


SeekBar bar;
TextView textProgress, textAction;
private PopupWindow pwindo;


@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.setup_environment);

    lockButton = (ImageButton) findViewById(R.id.lock);
    lockButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            initiatePopupWindow();

        }
    });

}


private void initiatePopupWindow() {
    try {
        LayoutInflater inflater = (LayoutInflater) Environment.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.annotation,
                (ViewGroup) findViewById(R.id.popup_element));
        pwindo = new PopupWindow(layout, 1000, 1200, true);
        pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

        bar = (SeekBar)findViewById(R.id.seekBar1); // make seekbar object
        bar.setOnSeekBarChangeListener(this);
        textProgress = (TextView)findViewById(R.id.textViewProgress);

        textAction = (TextView)findViewById(R.id.textViewAction);

    } catch (Exception e) {
        e.printStackTrace();
    }
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
                              boolean fromUser) {
    // TODO Auto-generated method stub

    textProgress.setText("The value is: "+progress);

    textAction.setText("changing");
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    textAction.setText("starting to track touch");

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    seekBar.setSecondaryProgress(seekBar.getProgress());
    textAction.setText("ended tracking touch");
}

} 每当我使用滑块时,它应该使用int值0-100更新,具体取决于滑块的位置,如果我创建一个活动并立即使用它,它可以工作,但是当我把它放在一个弹出窗口时,它什么都不做? / p>

annotation.xml

<LinearLayout android:id="@+id/popup_element"
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="#65bea9"
          android:orientation="vertical"
          android:padding="10sp">

<EditText
    android:layout_width="170dp"
    android:layout_height="250dp"
    android:id="@+id/editText2"
    android:layout_gravity="center_horizontal"
    android:background="#ffffff"
    android:textColor="#000000"
    android:hint="@string/hint_annotation"
    android:scrollIndicators="right"
    android:gravity="top"
    android:inputType="textMultiLine"
    android:textSize="8pt"/>

<TextView
    android:id="@+id/textViewProgress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="progress displayed here"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_gravity="center"
    android:textSize="6pt"></TextView>

<TextView
    android:id="@+id/textViewAction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textViewProgress"
    android:layout_marginTop="2dp"
    android:text="action displayed here"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_gravity="center"
    android:textSize="6pt"></TextView>

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="5dp"
    android:max="100"
    android:layout_gravity="center"></SeekBar>

<Button
    android:id="@+id/btn_close_popup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/btn_submit"/>

设置环境xml

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/root"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".Activities.Environment">


    <ImageButton
        android:src="@mipmap/unlocked_syn"
        android:layout_width="48dp"
        android:layout_height="45dp"
        android:id="@+id/lock"
        android:layout_row="1"
        android:layout_column="11" />

   </LinearLayout>

1 个答案:

答案 0 :(得分:1)

试试这个,您应该通过布局中的视图初始化您的Seekbar等。只需执行findViewById即可搜索活动中的视图

private void initiatePopupWindow() {
    try {
        LayoutInflater inflater = (LayoutInflater) Environment.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.annotation,
                (ViewGroup) findViewById(R.id.popup_element));
        pwindo = new PopupWindow(layout, 1000, 1200, true);
        pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

        bar = (SeekBar)layout.findViewById(R.id.seekBar1); // make seekbar object
        bar.setOnSeekBarChangeListener(this);
        textProgress = (TextView)layout.findViewById(R.id.textViewProgress);

        textAction = (TextView)layout.findViewById(R.id.textViewAction);

    } catch (Exception e) {
        e.printStackTrace();
    }
}
相关问题