學習了一個星期了,總算學會了TextView、EditView、Button等基本的控制項,還有一點點布局的知識,然後佳哥(我的師傅)就叫我寫一個打電話的東西,感覺很神奇,不過經過他的一句代碼,我覺得超容易啊~~~下面我來說下具體操作:
首先看下我的成果:
1、控制項的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" tools:context=".MainActivity" > <TextView android:id="@+id/txv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tishi" android:textColor="#D2691E" android:textSize="20dp" /> <LinearLayout android:id="@+id/linOut1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/txv" android:orientation="horizontal" > <Button android:id="@+id/me" android:layout_width="60dp" android:layout_height="60dp" android:layout_marginLeft="10dp" android:layout_marginRight="20dp" android:layout_marginTop="20dp" android:layout_weight="1" android:background="@drawable/niub" /></LinearLayout><LinearLayout android:id="@+id/linOut_txt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/linOut1" android:orientation="horizontal" > <TextView android:id="@+id/txv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="10dp" android:layout_weight="1" android:text="@string/txv1" /></LinearLayout>
2.代碼實現:
package com.example.phone;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.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {private Button niu;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);onclick(); niu.setOnClickListener(new BtnOnlick()); } private void onclick() { niu = (Button) findViewById(R.id.niu); } private class BtnOnlick implements View.OnClickListener {public void onClick(View v) {switch (v.getId()) { case R.id.niu:getIntent("##########");break; default:break; } } } private void getIntent(String str) {Intent i_ling = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + str));startActivity(i_ling);}}
這裡只用了一個Button 和 TextView,具體的大家自己添加哦~~~
3.設定許可權
開啟AndroidManifest.xml -----在application標籤上或者下面添加這麼一句代碼:<uses-permission android:name="android.permission.CALL_PHONE"/>
4.打包(有些要填的隨便填)
右鍵點擊項目-----Export-----Andriod-----Export...-----next---next-----
5.放在手機安裝運行。如想和我們進一步學習交流可加我的Andriod新手學習群:26040687,歡迎大家一起學習交流~~~~哈哈哈哈