2014-12-01 59 views
0

我有一个popupWindow,它是从主事件中的活动条触发的。弹出窗口中的按钮不会在showPopup()中触发它们各自的侦听器。从片段启动时,这个popupWindow结构大部分工作正常。我找不到这个的根本原因。有什么建议么?谢谢。Android ClickListeners没有从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>  
+0

在弹出窗口中是否有'btnDismissxml'和'btnSaveRecordxml'按钮? – 2014-12-01 19:36:15

+0

是的,他们是... – portsample 2014-12-01 19:40:09

回答

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() { 
    // ... 
}); 
+0

没有变化。这两个事件都没有被触发。不错,尽管尝试... – portsample 2014-12-01 19:47:26

+0

@portsample请添加您的弹出式布局的'xml' – 2014-12-01 19:48:09

+0

@portsample答案已被编辑。请将您的布局设置为“TableLayout”并让我知道结果 – 2014-12-01 19:59:20