Android ClickListeners未从PopupWindow触发

时间:2014-12-01 19:31:10

标签: android onclicklistener popupwindow

我有一个从主赛事的活动栏触发的popupWindow。弹出窗口中的按钮不会在showPopup()中触发其各自的侦听器。当从片段启动时,这个popupWindow结构的大部分工作正常。我无法确定这个的根本原因。有什么建议?感谢。

public class MainActivity extends Activity{
private final static String TAB_KEY_INDEX = "TAB_KEY";
public static Context appContext;
private PopupWindow popupWindow;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    appContext = getApplicationContext();
    //put Actionbar in tab mode     
    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);            
    //set titles for tabs
    ActionBar.Tab tab1 = actionBar.newTab().setText("Tab1");
    //create instances of each of the fragments
    Fragment tab1Fragment = new Tab1Fragment();
    //attach those fragment instances to their respective tabs
    tab1.setTabListener(new MyTabsListener(tab1Fragment));
    //add each tab to the ActionBar
    actionBar.addTab(tab1);
    if (savedInstanceState == null){//...do nothing                     
    }else if (savedInstanceState != null){
    actionBar.setSelectedNavigationItem(savedInstanceState.getInt(TAB_KEY_INDEX,0));
    }
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.menuitem_popup:
            showPopup();
            return true;
    }return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}
@Override
protected void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putInt(TAB_KEY_INDEX, getActionBar().getSelectedNavigationIndex());
}   
private void showPopup() { 
    Button btnDismiss, btnSaveRecord;
    try { 
    LayoutInflater layoutInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
    View layout = layoutInflater.inflate(R.layout.popup_layout, null);
    popupWindow = new PopupWindow(layout, 580, 500, true); 
    popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 40);

    btnDismiss=(Button)MainActivity.this.findViewById(R.id.btnDismissxml);
    btnDismiss.setOnClickListener(new OnClickListener() {
        //@Override
        public void onClick(View arg0) {
            popupWindow.dismiss();
        }
    });
    btnSaveRecord=(Button)MainActivity.this.findViewById(R.id.btnSaveRecordxml);
    btnSaveRecord.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            saveRecord();
        }
    });
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    }
public void saveRecord(){
    Toast.makeText(MainActivity.this.getApplicationContext(), "Event saveRecord() triggered.", Toast.LENGTH_SHORT).show();      
}
}   

这是每个请求的popup_layout.xml。感谢。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="@drawable/customborder"> 
<TableRow>
<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="This is Fragment3" />
</TableRow>

<TableRow>  
 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Species:" />
<EditText 
        android:id="@+id/textboxSpeciesxml"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"   
                    android:layout_span="3"
        android:text="(table is empty)"/> 

  </TableRow> 

    <TableRow>
          <Button
            android:id="@+id/btnSaveRecordxml"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="SAVE" />
          <Button
            android:id="@+id/btnUpdateRecordxml"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="UPDATE" />
          <Button
            android:id="@+id/btnDeleteRecordxml"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="DELETE" />
          <Button
            android:id="@+id/btnClearFormxml"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="CLEAR" />
         </TableRow>
    <TableRow>
          <Button
            android:id="@+id/btnFirstRecordxml"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/pszFirstRecordButton"/>
          <Button
            android:id="@+id/btnPreviousRecordxml"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/pszPreviousRecordButton"/>
          <Button
            android:id="@+id/btnNextRecordxml"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/pszNextRecordButton"/>
          <Button
            android:id="@+id/btnLastRecordxml"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/pszLastRecordButton"/>
         </TableRow>
    <TableRow>
        <Button
            android:id="@+id/btnPurgeTablexml"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text="Purge Table!" />
          <Button
            android:id="@+id/btnDismissxml"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
                        android:layout_column="3"
            android:text="Dismiss" />
    </TableRow>
</TableLayout>    

1 个答案:

答案 0 :(得分:0)

如果btnDismissxmlbtnSaveRecordxml是弹出式窗口中的按钮,则可以使用layout.findViewById而不是MainActivity.this.findViewById找到它们。使用以下代码:

View layout = (TableLayout) layoutInflater.inflate(R.layout.popup_layout, null);
// ...

btnDismiss=(Button) layout.findViewById(R.id.btnDismissxml);
btnDismiss.setOnClickListener(new OnClickListener() {
    // ...
});
btnSaveRecord=(Button) layout.findViewById(R.id.btnSaveRecordxml);
btnSaveRecord.setOnClickListener(new OnClickListener() {
    // ...
});