在 android 中,第一次单击时按钮侦听器未注册

2024-05-21

因为我是 Android 新手,所以我遇到了按钮监听器的问题 我正在使用 OnClickListener 来处理胸像,但它第一次点击后不执行一旦我单击多个,它就会表现良好,但如何使其在第一次单击时成为可能 这是我的代码:

public class DashbordActivity extends Activity implements OnClickListener{

ImageButton btnLogout, btnSearch, btnOENew, btnAENew,btnSync;
// Session Manager Class
SessionManager session = null;

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

    // Session Manager
    session = new SessionManager(getApplicationContext());

    /* Action Bar Color change on create*/
    ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#FF7F24"));
    ActionBar actionBar = getActionBar();
    actionBar.setBackgroundDrawable(colorDrawable);

    /* get Resources from Xml  file */
    btnOENew = (ImageButton) findViewById(R.id.btnOENew);
    btnAENew = (ImageButton) findViewById(R.id.btnAENew);
    btnSearch = (ImageButton) findViewById(R.id.btnSearch);     
    btnLogout = (ImageButton) findViewById(R.id.btnLogout);
    btnSync = (ImageButton)findViewById(R.id.btnSync);

    addButtonListener();// on click any button
}
    // on click any button
private void addButtonListener() {
    // Find our button and hook-up the click routine
    btnOENew.setOnClickListener(this);
    btnSearch.setOnClickListener(this);     
    btnAENew.setOnClickListener(this);
    btnLogout.setOnClickListener(this);
    btnSync.setOnClickListener(this);
}

// on click any button
@Override
public void onClick(View v) {
    btnOENew.setOnClickListener(new OnClickListener() {         
        public void onClick(View v) {
            Intent intent = new Intent(getBaseContext(),           OceanSalesActivity.class);
            startActivity(intent);
        }
    });

    btnAENew.setOnClickListener(new OnClickListener() {         
        public void onClick(View v) {
            Intent intent = new Intent(getBaseContext(), AirSalesActivity.class);
            startActivity(intent);
        }
    });

    btnSearch.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getBaseContext(), SearchActivity.class);
            startActivity(intent);
        }
    });


    btnLogout.setOnClickListener(new OnClickListener() {            
        public void onClick(View v) {               
            onLogout();
        }
    });

    btnSync.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent( getBaseContext() , SyncActivity.class);                                                
            startActivity(intent);
        }
    });
}

为什么你需要匿名内部类,当你有

 btnOENew.setOnClickListener(this);

你的班级实现了OnClickListener

您所需要的只是切换案例onClick

@Override
public void onClick(View v) {

    switch(v.getId())
     {
       case R.id.btnOENew :
           // button btnOENew clicked
       break;
       case R.id.btnAENew :
            // button btnAENew clicked  
       break;
       ... // similar for other buttons
      }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 android 中,第一次单击时按钮侦听器未注册 的相关文章

随机推荐