【问题标题】:Android ClickListeners not firing from a PopupWindowAndroid ClickListeners 未从 PopupWindow 触发
【发布时间】:2014-12-02 03:58:07
【问题描述】:

我有一个从主事件中的活动栏触发的弹出窗口。弹出窗口中的按钮不会在 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>    

【问题讨论】:

  • btnDismissxmlbtnSaveRecordxml 是弹出窗口中的按钮吗?

标签: android onclicklistener popupwindow


【解决方案1】:

如果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() {
    // ...
});

【讨论】:

  • @portsample 请添加您的弹出布局的xml
  • @portsample 答案已被编辑。请将您的布局转换为TableLayout 并让我知道结果
  • 知道了。谢谢您的帮助。最好的问候。
猜你喜欢
  • 2014-06-20
  • 1970-01-01
  • 2016-01-27
  • 2014-01-16
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多