MainActivity.java
package com.example.smile;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private Button btn;//创建一个按钮对象,要导入按钮对象所在的包
private EditText et1;
private EditText et2;
private Button btn2;
private Button btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.login);//创建btn的单击事件,参数要传递匿名的内部类
et1=(EditText)findViewById(R.id.username);
et2=(EditText)findViewById(R.id.password);
btn2=(Button)findViewById(R.id.callm);
btn3=(Button)findViewById(R.id.sms);
btn3.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in4=new Intent();
in4.setAction(Intent.ACTION_SENDTO);
in4.setData(Uri.parse("smsto:1367777777"));
in4.putExtra("sms_body","hello,android sms");
startActivity(in4);
}});
btn2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in2=new Intent();
in2.setAction(Intent.ACTION_CALL);
in2.setData(Uri.parse("tel:13567823232"));
startActivity(in2);
}});
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//要成功的进行跳转要用到intent这个对象
//第一个参数是原来的类,出发站,要以this结尾
//第二个参数是要跳转的类,终点站,要以class结尾
//Intent意图对象,指定了起始和终点
//Intent in=new Intent(MainActivity.this,SecActivity.class);
//startActivity(in);
String usernam=et1.getText().toString();
//System.out.println(username);
String pwd=et2.getText().toString();
if(usernam.equals("z3")&&pwd.equals("z3")){
Intent in=new Intent(MainActivity.this,SecActivity.class);
startActivity(in);
}
}});
}
@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;
}
}
SecActivity.java
package com.example.smile;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class SecActivity extends Activity {
//第二步,获得listview对象
private ListView userlv;
List<String> list; //创建集合对象
final static int menu_index=Menu.FIRST; //定义菜单项
final static int menu_content=Menu.FIRST+1;
final static int menu_last=Menu.FIRST+2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sec);
userlv = (ListView)findViewById(R.id.lst1);
//重点:listview的实现要依赖适配器,而适配器又依赖数据源
//第三步,创建集合数据源
list=new ArrayList<String>();//初始化集合对象
//向集合对象中加入数据
list.add("tom");
list.add("jerry");
list.add("jack");
list.add("martin");
list.add("harry");
list.add("june");
//第四步,创建适配器对象
ArrayAdapter<String> adapter=
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
//参数1:表示当前适配器的宿主,必须写
//参数2:表示当前适配器的数据呈现形式,用简单列表的形式呈现数据
//参数3:表示数据源
//第五步,把适配器绑定到listview控件中
userlv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.sec, menu);
//此方法表示,菜单项第一次生成的时候,所要执行的内容
//把每个菜单在此方法中,加入到菜单对象menu中
//此方法只执行一次
menu.add(0, menu_index, 0, "首页").setIcon(android.R.drawable.btn_star);
menu.add(0, menu_content, 1, "内容").setIcon(android.R.drawable.btn_star);
menu.add(0, menu_last, 2, "尾页").setIcon(android.R.drawable.btn_star_big_off);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
//return super.onOptionsItemSelected(item);
//此方法在每次选择某个菜单项的时候都会执行
//可在此方法中判断用户选了哪个菜单项,从而做出对应的Activity跳转
switch(item.getItemId()){
case menu_index:
System.out.println("menu_index");
return true;
case menu_content:
System.out.println("menu_content");
return true;
case menu_last:
System.out.println("menu_last");
return true;
}
return false;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
//对应于onCreateOptionsMenu方法
// TODO Auto-generated method stub
return super.onPrepareOptionsMenu(menu);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smile"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.smile.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.smile.SecActivity"
android:label="@string/title_activity_sec" >
</activity>
</application>
</manifest>