[android]代码库
package com.example.demo_menu20151002474;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView gender, hobby;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gender = (TextView) this.findViewById(R.id.gender);
hobby = (TextView) this.findViewById(R.id.hobby);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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();
switch (item.getItemId()) {
case R.id.male:
case R.id.female:
item.setChecked(true);
gender.setText(item.getTitle());
break;
case R.id.sport:
case R.id.sing:
case R.id.trip:
item.setChecked(!item.isChecked());
if (item.isChecked())
hobby.setText(hobby.getText().toString() + item.getTitle());
break;
}
return super.onOptionsItemSelected(item);
}
}
[源代码打包下载]
by: 发表于:2018-05-31 11:04:05 顶(0) | 踩(0) 回复
??
回复评论