【发布时间】: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