android开发实例,欢迎界面,打电话,发短信

来源:互联网 发布:数据库建立一对一关系 编辑:程序博客网 时间:2024/06/05 21:52

本示例实现了人欢迎页跳转,拨号、发送短信及长按出现菜单选项的操作

1.Android项目结构图,主要操作红框内的文件 

2.布局代码如下

a. activity_main.xml 文件 实现的的欢迎界面的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <TextView         android:text="android班制作"        android:background="@drawable/dayday"        android:layout_width="match_parent"        android:layout_height="match_parent"        style="@style/basiz"        android:gravity="center"/></LinearLayout>

b.   jump.xml文件   案例的主要布局界面,listview的布局显示
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView         android:id="@+id/jumptv"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Oh My God"        android:textColor="#f65"        android:textSize="30dp"        android:gravity="center"/>    <ListView        android:id="@+id/lv"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="1" >    </ListView></LinearLayout>
c.    list_item.xml 文件  listitem的布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:orientation="horizontal" >    <ImageView        android:id="@+id/imgview"        android:layout_width="100dp"        android:layout_height="100dp"        android:layout_centerVertical="true"        android:layout_marginLeft="5px"        android:layout_marginRight="5px" />    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >        <TextView            android:id="@+id/tv1"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="姓名:"            android:textSize="35px" />                <TextView            android:id="@+id/tv2"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="电话"            android:textSize="35px" />            </LinearLayout></LinearLayout>

3.menu上下文菜单控制文件

<menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    tools:context="com.example.kaoshi.MainActivity" >    <item        android:id="@+id/item1"        android:title="拨号">    </item>    <item        android:id="@+id/item2"        android:title="信息">    </item></menu>

4.java代码实现主要功能

a.  MainActivity.java 文件 实现欢迎界面到主界面的跳转

package com.example.kaoshi;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.content.Intent;import android.os.Bundle;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final Intent intent = new Intent();Timer timer = new Timer();TimerTask task = new TimerTask() {@Overridepublic void run() {// TODO Auto-generated method stubintent.setClass(MainActivity.this, jump.class);MainActivity.this.startActivity(intent);}};timer.schedule(task, 1000*3);}}

b. jump.java 文件   该文件实现了主要的功能

/** *  */package com.example.kaoshi;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.ContextMenu;import android.view.ContextMenu.ContextMenuInfo;import android.view.MenuInflater;import android.view.MenuItem;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemLongClickListener;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.Toast;/** * @author 张 *  */public class jump extends Activity {private static final Uri SMSToUri = null;private ListView lv;private TextView tv1, tv2;private List<Map<String, Object>> list;String xm1 = "";String dh1 = "";protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.jump);tv1 = (TextView) findViewById(R.id.tv1);tv2 = (TextView) findViewById(R.id.tv2);lv = (ListView) findViewById(R.id.lv);SimpleAdapter adapter = new SimpleAdapter(this, getData(),R.layout.list_item, new String[] { "imgview", "tv1", "tv2" },new int[] { R.id.imgview, R.id.tv1, R.id.tv2 });// setListAdapter(adapter);lv.setAdapter(adapter);registerForContextMenu(lv);//长按获取listview中的数据 实现拨号lv.setOnItemLongClickListener(new OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {// TODO Auto-generated method stubHashMap<String, Object> map = (HashMap<String, Object>) lv.getItemAtPosition(arg2);xm1 = String.valueOf(map.get("tv1").toString());dh1 = String.valueOf(map.get("tv2").toString());System.out.println(tv1);System.out.println(tv2);return false;}});}private List<? extends Map<String, ?>> getData() {// TODO Auto-generated method stubList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();Map<String, Object> map = new HashMap<String, Object>();map.put("imgview", R.drawable.zpyzpy);map.put("tv1", "张");map.put("tv2", "17853496522");list.add(map);map = new HashMap<String, Object>();map.put("imgview", R.drawable.hyf);map.put("tv1", "韩");map.put("tv2", "17853496714");list.add(map);map = new HashMap<String, Object>();map.put("imgview", R.drawable.lk);map.put("tv1", "鹿");map.put("tv2", "15275710175");list.add(map);map = new HashMap<String, Object>();map.put("imgview", R.drawable.qym);map.put("tv1", "全");map.put("tv2", "17853491140");list.add(map);map = new HashMap<String, Object>();map.put("imgview", R.drawable.wf);map.put("tv1", "王");map.put("tv2", "13053438923");list.add(map);map = new HashMap<String, Object>();map.put("imgview", R.drawable.lk);map.put("tv1", "鹿");map.put("tv2", "15275710175");list.add(map);map = new HashMap<String, Object>();map.put("imgview", R.drawable.qym);map.put("tv1", "全");map.put("tv2", "17853491140");list.add(map);map = new HashMap<String, Object>();map.put("imgview", R.drawable.wf);map.put("tv1", "王");map.put("tv2", "13053438923");list.add(map);return list;}@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {// TODO Auto-generated method stubMenuInflater mf = new MenuInflater(jump.this);mf.inflate(R.menu.main, menu);menu.setHeaderIcon(R.drawable.message);menu.setHeaderTitle("请选择");}@Overridepublic boolean onContextItemSelected(MenuItem item) {// TODO Auto-generated method stubint id = item.getItemId();switch (id) {case R.id.item1:Intent intent = new Intent();intent.setAction(intent.ACTION_CALL);intent.setData(Uri.parse("tel:" + dh1));startActivity(intent);break;case R.id.item2:Intent intent1 = new Intent();intent1.setAction(intent1.ACTION_SENDTO);intent1.setData(Uri.parse("smsto:" + dh1));intent1.putExtra("sms_body", "");startActivity(intent1);break;}return super.onContextItemSelected(item);}}

以上就是全部代码,小白初入编程大军!

附上运行截图



0 0
原创粉丝点击