package com.example.second;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
public class MainActivity extends Activity {
private Button login1;//1.创建登录b对象,并导入所在的包
private EditText name;//
private EditText word;
private Button callphone;//创建打电话b2对象
private Button message;//发短信对象
//1.定义菜单项,类型为全局静态的整数
final static int menu_forecast=Menu.FIRST;
final static int menu_history=Menu.FIRST+1;
final static int menu_setting=Menu.FIRST+2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//2.对b对象进行实例化
login1=(Button)findViewById(R.id.login);
callphone=(Button)findViewById(R.id.callh);
message=(Button)findViewById(R.id.sendsms);
name=(EditText)findViewById(R.id.name1);
word=(EditText)findViewById(R.id.pass);
//3.创建b单击事件,参数传递匿名内部数
login1.setOnClickListener(new OnClickListener(){//导入并add
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//System.out.println("hello word!........");//向控制台输出
//成功跳转用Intent对象。Intent必须有两个参数:
//一个是原类,即出发点,以.this结尾;另一个是要跳转到的类,即终止点,以.class结尾
//Intent in=new Intent(MainActivity.this,SecondActivity.class);
//startActivity(in);
//使登录信息正确时,才登录
String username=name.getText().toString();
String password=word.getText().toString();
if(username.equals("zhangsan")&&password.equals("123456")){
Intent in=new Intent(MainActivity.this,SecondActivity.class);
startActivity(in);
}//if
else{
Intent in=new Intent(MainActivity.this,ThirdActivity.class);
startActivity(in);
}
}});//b.setOnClickListener
//调用系统电话服务
callphone.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in2=new Intent();//创建in2意图对象
in2.setAction(Intent.ACTION_CALL);//指定意图动作,电话动作
in2.setData(Uri.parse("tel:18363855523"));//指定电话号码,tel 为电话协议
startActivity(in2);//启动意图
}});//b2
//短信服务
message.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in3=new Intent();
in3.setAction(Intent.ACTION_SENDTO);
in3.setData(Uri.parse("smsto:18363866667"));
in3.putExtra("sms_body", "hello android");//短信内容
//MainActivity.this.startActivity(in3);//
startActivity(in3);//的另一种写法
}});//b3
}//onCreate
@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);
//把每一个菜单项加入到菜单对象menu中
menu.add(0,menu_forecast, 0, "天气预报").setIcon(android.R.drawable.alert_light_frame);
//加系统中(android.R)的图片
menu.add(0, menu_history, 1, "历史数据").setIcon(android.R.drawable.ic_lock_silent_mode);
menu.add(0, menu_setting, 2, "系统设置").setIcon(android.R.drawable.btn_dropdown);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
//return super.onOptionsItemSelected(item);
switch(item.getItemId()){
case menu_forecast:
System.out.println("menu_forecast");
Intent in4=new Intent(MainActivity.this,ForecastActivity.class);
startActivity(in4);
return true;
case menu_history:
System.out.println("menu_history");
Intent in5=new Intent(MainActivity.this,HistoryActivity.class);
startActivity(in5);
return true;
case menu_setting:
System.out.println("menu_setting");
Intent in6=new Intent(MainActivity.this,SettingActivity.class);
startActivity(in6);
return true;
}//switch
return false;
}
}