import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class SimpleCompontentActivity extends Activity {
private TextView tv_simple_msg;
private EditText et_main_num;
private Button btn_simple_submit;
private ImageView iv_simple_play;
private CheckBox cb_simple_basketball;
private CheckBox cb_simple_football;
private CheckBox cb_simple_pingpong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_compontent);
//1、TextView
tv_simple_msg = (TextView)findViewById(R.id.tv_simple_msg);
tv_simple_msg.setText("今天天气不错,PM2.5才800~");
//2、EditTex
et_main_num = (EditText)findViewById(R.id.et_main_num);
//3、button
btn_simple_submit = (Button)findViewById(R.id.btn_main_show);
btn_simple_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String number = et_main_num.getText().toString().trim();
Toast.makeText(SimpleCompontentActivity.this,number, Toast.LENGTH_SHORT).show();
}
});
//4.ImageView
iv_simple_play = (ImageView) findViewById(R.id.iv_simple_play);
iv_simple_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//设置背景图片
iv_simple_play.setBackgroundResource(android.R.drawable.alert_light_frame);
//设置前景图片
iv_simple_play.setImageResource(android.R.drawable.ic_media_pause);
}
});
//5.CheckBox
cb_simple_basketball=(CheckBox) findViewById(R.id.cb_simple_basketball);
cb_simple_football=(CheckBox) findViewById(R.id.cb_simple_football);
cb_simple_pingpong=(CheckBox) findViewById(R.id.cb_simple_pingpong);
//给cb_simple_football设置选中状态改变的监听
cb_simple_football.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(SimpleCompontentActivity.this, "选中了足球", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SimpleCompontentActivity.this, "取消选中了足球", Toast.LENGTH_SHORT).show();
}
}
});
//6.
}
public void comfirm(View v){
StringBuffer sb = new StringBuffer();
if(cb_simple_basketball.isChecked()){
sb.append(cb_simple_basketball.getText().toString()).append("");
}
if(cb_simple_football.isChecked()){
sb.append(cb_simple_football.getText().toString()).append("");
}
if(cb_simple_pingpong.isChecked()){
sb.append(cb_simple_pingpong.getText().toString().trim());
}
Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show();
}
}
package com.beicai.test05_component;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MenuActivity extends Activity {
private Button btn_menu_show_cm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
btn_menu_show_cm = (Button) findViewById(R.id.btn_menu_show_cm);
btn_menu_show_cm.setOnCreateContextMenuListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//纯编码方式
menu.add(0,2,0,"添加");
menu.add(0,2,0,"删除");
return super.onCreateOptionsMenu(menu);
}
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// //1、得到菜单加载器对象
// MenuInflater menuInflater = getMenuInflater();
// //2、加载菜单文件
// menuInflater.inflate(R.menu.option_menu, menu);
// return super.onCreateOptionsMenu(menu);
// }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case 2:
Toast.makeText(this, "添加", Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(this, "删除", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
//添加菜单项
menu.add(0,1,0,"添加");
menu.add(0,4,0,"删除");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()){
case 1:
Toast.makeText(this, "添加", Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(this, "删除", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
}