现在的位置: 首页 > 移动开发 > 正文

Android学习笔记06—电话拨号器的制作:项目结构深化

2019年09月21日 移动开发 ⁄ 共 7406字 ⁄ 字号 评论关闭

13.电话拨号器的制作:
   a.窗口上有显示文字的Textview控件
   b.用于显示文本输入框的
   c.和button控件
---------------------------------------------
14.创建Android项目
   a.applicationName:电话拨号器
   b.Package name:com.credream.call
   c.Create Activity:MainActivity
   d.Min SDK Version:8
---------------------------------------
15.完成该软件界面:
   a.打开main.xml文件:编辑:
     打开引用的strings.xml编辑需要的文字
   b.点击layout,看看预览画面,可以选择标屏查看:ADPI
   c.strings.xml
      <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">电话拨号器</string>
    <string name="mobile">请输入电话号:</string>
    <string name="button">开始拨号</string>
</resources>
   d.main.xml
     <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/mobile"
    />
 <EditText 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"
 />
 <Button
  android:layout_width="fill_parent" 
 android:layout_height="wrap_content"
 android:text="@string/button"
 />
</LinearLayout>
----------------------------------------------------
16.给button添加业务逻辑:
    <Button
  android:layout_width="fill_parent" 
 android:layout_height="wrap_content"
 android:text="@string/button"
 android:id="@+id/button"//在R文件中id内部类里添加一个常量,并且使用button,作为
  //常量的名字 public static final int button=0x7f040003;
 />
  先在main.xml中写入;
   <Button
  android:layout_width="fill_parent" 
 android:layout_height="wrap_content"
 android:text="@string/button"
 android:id="@+id/button"//在R文件中id内部类里添加一个常量,并且使用button,作为
  //常量的名字 public static final int button=0x7f040003;
 />
-------------------------------------------------------------
17.a.在android中:不管是Button,EditText,还是TextVew都有公共父类:View,所以他们都属     于View类型
      Button button=(Button) this.findViewById(R.id.button);//根据id查找某一个显      示控件
   b.android源代码下载方法:网上有android源码下载工具
     使用本工具要先安装windows版本git,可到以下下载:
     http://code.google.com/p/msysgit/downloads/list
----------------------------------------------------------
18.a.<user-permission android:name="android.permission.CALL.PHONE"/>//在清单文件       中出示这个权限,拨号权限;,出示了这个权限之后,在用户使用拨号的时候,会弹出安全
     警告信息.
-------------------------------------------------------------
19.拨号器的作用,比如在百合网,搜索出来美女后,可以直接点击拨号,来拨打对方电话
----------------------------------------------------------------
2013-03-03
1.上面的拨号器每次都要寻找输入框,性能低
  可以把
    button.setOnClickListener(new ButtonClickListener());
  放到onCreate方法里面,这时候,只需要在创建的时候寻找一次就可以了
---------------------------------------------------------------------      
2.a.还有一种写法:
   但是这样的写法虽然可以实现同样的同能,但可读性不好,所以不建议,
   如果大量使用匿名内部类的话那么可读性会很差   
  b.定义成内部类,比写成一个单独的类,性能要好很多,所以在android中,大量使用了
    很多匿名内部类,因为当写成一个单独的类的时候,加载的时候会单独的加载,那么
    所加载的文件就会变多,这样的话,就降低了性能
-----------------------------------------------------------------------
1.电话拨号器的所有代码:
  cn.credream.phone
  MainActivity.java的最佳方法:
  package cn.itcast.phone;
  

import android.app.Activity;
  
import android.content.Inent;
  
import android.net.Uri;

  import android.os.Bundle;
import android.view.View;
  
import android.widget.Button;
  
import android.widget.EditText;
  

public class MainActivity extends Activity {
  
  private EditText mobileText;
   
 
    @Override
  
    public void onCreate(Bundle savedInstanceState) {
   
     super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
 
       mobileText = (EditText) findViewById(R.id.mobile);
   
     Button button = (Button) this.findViewById(R.id.button);
        
   button.setOnClickListener(new ButtonClickListener());
   
 }
 
   
 private final class ButtonClickListener implements View.OnClickListener{

       public void onClick(View v) {

 String number = mobileText.getText().toString();

Intent intent = new Intent();

intent.setAction("android.intent.action.CALL");

intent.setData(Uri.parse("tel:"+ number));

startActivity(intent);//方法内部会自动为Intent添加类别:  android.intent.category.DEFAULT

 }
   
}
}
-----------------------------------------------------------------------------------------
 MainActivity.java的另一种方法:
package com.credream.call;

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

public class MainActivity extends Activity {
   
 private EditText mobileText;
    
  
  @Override
    
public void onCreate(Bundle savedInstanceState) {
    
    super.onCreate(savedInstanceState);
      
  setContentView(R.layout.main);
  
      mobileText = (EditText) findViewById(R.id.mobile);
      
  Button button = (Button) this.findViewById(R.id.button);
      
  button.setOnClickListener(new ButtonClickListener(){
     
    public void onClick(View v) {
   
String number = mobileText.getText().toString();
   
Intent intent = new Intent();
   
intent.setAction("android.intent.action.CALL");
   
intent.setData(Uri.parse("tel:"+ number));
   
startActivity(intent);//方法内部会自动为Intent添加类别:android.intent.category.DEFAULT
   
}
      
  });
   
 }
--------------------------------------------------
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="@string/mobile"
   />
<EditText
android:layout_width="fill_parent" 
   android:layout_height="wrap_content"
   android:id="@+id/mobile"
   />
<Button
android:layout_width="wrap_content" 
   android:layout_height="wrap_content"
   android:text="@string/button"
   android:id="@+id/button"
   />
</LinearLayout>
-----------------------------------------------------
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">电话拔号器</string>
    <string name="mobile">请输入手机号</string>
    <string name="button">拔号</string>
</resources>
---------------------------------------------
 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.itcast.phone"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".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>

    </application>
    <uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest> 
-----------------------------------------------------------
3.Adb connection Error:远程主机强迫关闭了一个现有的连接。
  问题出现的原因:这是ddms调用adb引发的. 经过一番搜索, 发现这是windows环境下, adb的一个限制, 也可以说是bug.  当手机上同时运行的进程数大于64时, 就会引发adb奔溃. 更深层

次的原因, 就是windows API的WaitForMultipleObjects所支持的最大句柄数是MAXIMUM_WAIT_OBJECTS, 即64.
   下载相应的adb.exe文件,用它替换掉你机器上的sdk的platform/tools目录下面的adb.exe。这样就可以完美解决问题。
------------------------------------------------------------------------
4.Unexpected error while launching logcat. Try reselecting the device. 
  今天在向模拟器部署运行项目时碰到下面的异常:
  [2012-02-15 13:22:14 - DeviceMonitor] Adb connection Error:An existing   connection was forcibly closed by the remote host

[2012-02-15 13:22:14 - Unexpected error while launching logcat. Try reselecting the device.] An existing connection was forcibly closed by the remote host

java.io.IOException: An existing connection was forcibly closed by the remote host

两种解决办法:
1、在命令行下执行如下命令:adb kill-server、adb kill-server
2、在任务管理器中,强制关闭adb.exe,然后重启模拟器
-----------------------------------------------------------------

抱歉!评论已关闭.