【问题标题】:Why are my buttons in Android not working?为什么我在 Android 中的按钮不起作用?
【发布时间】:2015-08-13 01:41:32
【问题描述】:

我创建了一个带有 3 个按钮(主按钮、历史按钮、注销按钮)的菜单活动。
我不知道为什么所有按钮都不起作用并且有注销功能。
反正我对注销功能一无所知,是正确的功能还是错误的功能?
我是 Android 新手,我们将不胜感激。

这是我的代码:

public class MenuActivity extends ActionBarActivity implements View.OnClickListener {

        Button mainBtn,historyBtn,logoutBtn ;

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

            mainBtn = (Button)findViewById(R.id.button1);
            historyBtn = (Button)findViewById(R.id.button2);
            logoutBtn = (Button)findViewById(R.id.button3);

        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.button1:
                    Intent main = new Intent (this, MainActivity2.class);
                    startActivity(main);
                    break;
                case R.id.button2:
                    Intent history = new Intent(this, HistoryActivity.class);
                    startActivity(history);
                    break;
                case R.id.button3:
                    Intent logout = new Intent(this, LoginActivity.class);
                    keluar.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(logout);
                    this.finish();
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_menu, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }
    }

【问题讨论】:

  • 为按钮注册监听器。

标签: android button switch-statement logout


【解决方案1】:

你做过吗?

mainBtn.setOnClickListener(this);
historyBtn.setOnClickListener(this);
logoutBtn.setOnClickListener(this);

即在代码中或在 xml 文件中为按钮注册 onClickListener。

【讨论】:

  • 它应该被宣布为先生吗?
【解决方案2】:

将侦听器设置为所有按钮,例如..

 mainBtn.setOnClickListener(this);

【讨论】:

    【解决方案3】:

    您需要向点击侦听器注册按钮。用以下代码替换您的代码..

    public class MenuActivity extends ActionBarActivity implements View.OnClickListener {
    
    
        Button mainBtn,historyBtn,logoutBtn ;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_menu);
    
            mainBtn = (Button)findViewById(R.id.button1);
            historyBtn = (Button)findViewById(R.id.button2);
            logoutBtn = (Button)findViewById(R.id.button3);
            // Register your buttons with the click listener
            mainBtn.setOnClickListener(this);
            historyBtn.setOnClickListener(this);
            logoutBtn.setOnClickListener(this);
    
        }
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.button1:
                    Intent main = new Intent (this, MainActivity2.class);
                    startActivity(main);
                    break;
                case R.id.button2:
                    Intent history = new Intent(this, HistoryActivity.class);
                    startActivity(history);
                    break;
                case R.id.button3:
                    Intent logout = new Intent(this, LoginActivity.class);
                    keluar.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(logout);
                    this.finish();
            }
     }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_menu, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
     }
    
    
    }
    

    【讨论】:

      【解决方案4】:

      您没有为按钮设置监听器。

      mainBtn = (Button)findViewById(R.id.button1);
      historyBtn = (Button)findViewById(R.id.button2);
      logoutBtn = (Button)findViewById(R.id.button3);
      
      mainBtn.setOnClickListener(this);
      historyBtn.setOnClickListener(this);
      logoutBtn.setOnClickListener(this);
      

      祝你工作顺利。 :)

      【讨论】:

        【解决方案5】:
        mainBtn.setOnClickListener(this);
        historyBtn.setOnClickListener(this);
        logoutBtn.setOnClickListener(this);
        

        【讨论】:

        • 你需要解释你的代码,否则大多数人不会理解他们应该用它做什么。
        猜你喜欢
        • 1970-01-01
        • 2016-04-19
        • 1970-01-01
        • 1970-01-01
        • 2021-07-21
        • 2010-10-30
        • 2022-01-23
        • 1970-01-01
        相关资源
        最近更新 更多